Skip to content

Commit 89cfd34

Browse files
committed
cli: Apply clippy lints
1 parent ac3fa77 commit 89cfd34

File tree

1 file changed

+12
-21
lines changed
  • json-structural-diff-cli/src

1 file changed

+12
-21
lines changed

json-structural-diff-cli/src/main.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate clap;
33

44
use std::fs::File;
55
use std::io::Write;
6-
use std::path::PathBuf;
6+
use std::path::{Path, PathBuf};
77
use std::process;
88

99
use clap::{App, Arg};
@@ -22,10 +22,10 @@ struct Config {
2222
fn act_on_file(
2323
path1: &PathBuf,
2424
path2: &PathBuf,
25-
output_path: &Option<PathBuf>,
25+
output_path: Option<&PathBuf>,
2626
cfg: &Config,
2727
) -> std::io::Result<()> {
28-
let buffer1 = std::fs::read(&path1).unwrap();
28+
let buffer1 = std::fs::read(path1).unwrap();
2929
let buffer2 = std::fs::read(path2).unwrap();
3030

3131
if let (Ok(json1), Ok(json2)) = (
@@ -43,7 +43,7 @@ fn act_on_file(
4343
if let Some(output_path) = output_path {
4444
let output_filename = path1.file_name().unwrap().to_str().unwrap();
4545
let mut output_file = File::create(output_path.join(output_filename))?;
46-
writeln!(&mut output_file, "{}", json_string)?;
46+
writeln!(&mut output_file, "{json_string}")?;
4747
} else {
4848
let mut term = Term::stdout();
4949
term.write_all(json_string.as_bytes())?;
@@ -57,21 +57,15 @@ fn is_hidden(entry: &DirEntry) -> bool {
5757
entry
5858
.file_name()
5959
.to_str()
60-
.map(|s| s.starts_with('.'))
61-
.unwrap_or(false)
60+
.is_some_and(|s| s.starts_with('.'))
6261
}
6362

64-
fn explore(
65-
path1: &PathBuf,
66-
path2: &PathBuf,
67-
output_path: &Option<PathBuf>,
68-
cfg: &Config,
69-
) -> std::io::Result<()> {
70-
WalkDir::new(&path1)
63+
fn explore(path1: &PathBuf, path2: &PathBuf, output_path: Option<&PathBuf>, cfg: &Config) {
64+
WalkDir::new(path1)
7165
.into_iter()
7266
.filter_entry(|e| !is_hidden(e))
7367
.zip(
74-
WalkDir::new(&path2)
68+
WalkDir::new(path2)
7569
.into_iter()
7670
.filter_entry(|e| !is_hidden(e)),
7771
)
@@ -86,15 +80,12 @@ fn explore(
8680
&& path1_file.extension().unwrap() == "json"
8781
&& path2_file.extension().unwrap() == "json"
8882
{
89-
act_on_file(&path1_file, &path2_file, &output_path, &cfg).unwrap();
83+
act_on_file(&path1_file, &path2_file, output_path, cfg).unwrap();
9084
}
9185
});
92-
93-
Ok(())
9486
}
9587

96-
#[inline(always)]
97-
fn exist_or_exit(path: &PathBuf, which_path: &str) {
88+
fn exist_or_exit(path: &Path, which_path: &str) {
9889
if !(path.exists()) {
9990
eprintln!(
10091
"The {} path `{}` is not correct",
@@ -178,11 +169,11 @@ fn main() {
178169
};
179170

180171
if path1.is_dir() && path2.is_dir() {
181-
explore(&path1, &path2, &output_path, &cfg).unwrap();
172+
explore(&path1, &path2, output_path.as_ref(), &cfg);
182173
} else if (path1.is_dir() && !path2.is_dir()) || (!path1.is_dir() && path2.is_dir()) {
183174
eprintln!("Both paths should be a directory or a file",);
184175
process::exit(1);
185176
} else {
186-
act_on_file(&path1, &path2, &output_path, &cfg).unwrap();
177+
act_on_file(&path1, &path2, output_path.as_ref(), &cfg).unwrap();
187178
}
188179
}

0 commit comments

Comments
 (0)