From 337a63e3ee3c653b8a78e2850bb0e80b02c5481c Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Sun, 29 Sep 2024 12:51:51 +0700 Subject: [PATCH] Fix clippy::manual_map, clippy::redundant_field_names and remove one panic --- file/file.rs | 3 ++- text/diff.rs | 19 ++++++++++--------- text/diff_util/common.rs | 32 ++++++++++++++++++++++++++++++-- text/diff_util/dir_data.rs | 15 ++++++--------- text/diff_util/dir_diff.rs | 8 ++++---- text/diff_util/file_diff.rs | 22 +++++++++------------- 6 files changed, 61 insertions(+), 38 deletions(-) diff --git a/file/file.rs b/file/file.rs index 7280314a..eba0f85a 100644 --- a/file/file.rs +++ b/file/file.rs @@ -211,7 +211,8 @@ impl Value { /// Parses Hexadecimal, Octal and Unsigned Decimal // TODO -#[allow(clippy::needless_match)] +#[allow(clippy::manual_map)] // TODO remove this +#[allow(clippy::needless_match)] // TODO remove this fn parse_number(input: &mut String) -> Option { if let Some(hex_num) = parse_hexadecimal(input) { Some(hex_num) diff --git a/text/diff.rs b/text/diff.rs index 791e24ee..3a5a9d84 100644 --- a/text/diff.rs +++ b/text/diff.rs @@ -108,8 +108,6 @@ fn check_difference(args: Args) -> io::Result { textdomain(PROJECT_NAME)?; bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; - let output_format: OutputFormat = (&args).into(); - let path1 = PathBuf::from(&args.file1); let path2 = PathBuf::from(&args.file2); @@ -124,16 +122,19 @@ fn check_difference(args: Args) -> io::Result { return Ok(DiffExitStatus::Trouble); } + let output_format: OutputFormat = (&args).into(); + + let format_options = FormatOptions::try_new( + args.ignore_eol_space, + output_format, + args.label, + args.label2, + ); + let format_options = format_options.unwrap(); + let path1_is_file = fs::metadata(&path1)?.is_file(); let path2_is_file = fs::metadata(&path2)?.is_file(); - let format_options = FormatOptions { - ignore_trailing_white_spaces: args.ignore_eol_space, - label1: args.label, - label2: args.label2, - output_format: output_format, - }; - if path1_is_file && path2_is_file { return FileDiff::file_diff(path1, path2, &format_options, None); } else if !path1_is_file && !path2_is_file { diff --git a/text/diff_util/common.rs b/text/diff_util/common.rs index f4996efe..f2e3791d 100755 --- a/text/diff_util/common.rs +++ b/text/diff_util/common.rs @@ -1,8 +1,36 @@ pub struct FormatOptions { pub ignore_trailing_white_spaces: bool, pub output_format: OutputFormat, - pub label1: Option, - pub label2: Option, + label1: Option, + label2: Option, +} + +impl FormatOptions { + pub fn try_new( + ignore_trailing_white_spaces: bool, + output_format: OutputFormat, + label1: Option, + label2: Option, + ) -> Result { + if label1.is_none() && label2.is_some() { + return Err("label1 can not be NONE when label2 is available"); + } + + Ok(Self { + ignore_trailing_white_spaces, + output_format, + label1, + label2, + }) + } + + pub fn label1(&self) -> &Option { + &self.label1 + } + + pub fn label2(&self) -> &Option { + &self.label2 + } } #[allow(dead_code)] diff --git a/text/diff_util/dir_data.rs b/text/diff_util/dir_data.rs index 6e916293..2fec62ad 100755 --- a/text/diff_util/dir_data.rs +++ b/text/diff_util/dir_data.rs @@ -14,20 +14,17 @@ pub struct DirData { } impl DirData { - pub fn load(path: PathBuf) -> io::Result { - let mut dir_data = DirData { - path: path, - files: Default::default(), - }; - let entries = fs::read_dir(&dir_data.path)?; + pub fn load(path: PathBuf) -> io::Result { + let mut files: HashMap = Default::default(); + + let entries = fs::read_dir(&path)?; for entry in entries { let entry = entry?; - - dir_data.files.insert(entry.file_name(), entry); + files.insert(entry.file_name(), entry); } - Ok(dir_data) + Ok(Self { path, files }) } pub fn files(&self) -> &HashMap { diff --git a/text/diff_util/dir_diff.rs b/text/diff_util/dir_diff.rs index dbb91dd2..aca74465 100755 --- a/text/diff_util/dir_diff.rs +++ b/text/diff_util/dir_diff.rs @@ -117,15 +117,15 @@ impl<'a> DirDiff<'a> { show_if_different.push_str("-b "); } - if let Some(label1) = &self.format_options.label1 { + if let Some(label1) = &self.format_options.label1() { show_if_different.push_str(format!("--label {} ", label1).as_str()) } - if let Some(label2) = &self.format_options.label2 { + if let Some(label2) = &self.format_options.label2() { show_if_different.push_str(format!("--label2 {} ", label2).as_str()) } - if let Some(label1) = &self.format_options.label1 { + if let Some(label1) = &self.format_options.label1() { show_if_different.push_str(format!("{} ", label1).as_str()) } else { show_if_different @@ -133,7 +133,7 @@ impl<'a> DirDiff<'a> { show_if_different.push(' '); } - if let Some(label2) = &self.format_options.label2 { + if let Some(label2) = &self.format_options.label2() { show_if_different.push_str(format!("{} ", label2).as_str()) } else { show_if_different diff --git a/text/diff_util/file_diff.rs b/text/diff_util/file_diff.rs index ae557708..5c92a6a7 100755 --- a/text/diff_util/file_diff.rs +++ b/text/diff_util/file_diff.rs @@ -31,28 +31,24 @@ pub struct FileDiff<'a> { } impl<'a> FileDiff<'a> { - pub fn are_different(&self) -> bool { - self.are_different - } - fn new( file1: &'a mut FileData, file2: &'a mut FileData, format_options: &'a FormatOptions, ) -> Self { - if format_options.label1.is_none() && format_options.label2.is_some() { - panic!("label1 can not be NONE when label2 is available"); - } - Self { file1, file2, hunks: Hunks::new(), - format_options: format_options, + format_options, are_different: false, } } + pub fn are_different(&self) -> bool { + self.are_different + } + pub fn file_diff( path1: PathBuf, path2: PathBuf, @@ -362,11 +358,11 @@ impl<'a> FileDiff<'a> { fn print_context(&mut self, context: usize) { println!( "*** {}", - Self::get_header(self.file1, &self.format_options.label1) + Self::get_header(self.file1, &self.format_options.label1()) ); println!( "--- {}", - Self::get_header(self.file2, &self.format_options.label2) + Self::get_header(self.file2, &self.format_options.label2()) ); let change_ranges = self.get_context_ranges(context); @@ -423,11 +419,11 @@ impl<'a> FileDiff<'a> { fn print_unified(&mut self, unified: usize) { println!( "--- {}", - Self::get_header(self.file1, &self.format_options.label1) + Self::get_header(self.file1, &self.format_options.label1()) ); println!( "+++ {}", - Self::get_header(self.file2, &self.format_options.label2) + Self::get_header(self.file2, &self.format_options.label2()) ); let context_ranges = self.get_context_ranges(unified);