Skip to content

Commit b8b8bc9

Browse files
authored
Merge pull request #276 from fox0/clippy--manual_map
Fix clippy::manual_map, clippy::redundant_field_names and remove one panic
2 parents 417730d + 337a63e commit b8b8bc9

File tree

6 files changed

+61
-38
lines changed

6 files changed

+61
-38
lines changed

file/file.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ impl Value {
212212

213213
/// Parses Hexadecimal, Octal and Unsigned Decimal
214214
// TODO
215-
#[allow(clippy::needless_match)]
215+
#[allow(clippy::manual_map)] // TODO remove this
216+
#[allow(clippy::needless_match)] // TODO remove this
216217
fn parse_number(input: &mut String) -> Option<u64> {
217218
if let Some(hex_num) = parse_hexadecimal(input) {
218219
Some(hex_num)

text/diff.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ fn check_difference(args: Args) -> io::Result<DiffExitStatus> {
108108
textdomain(PROJECT_NAME)?;
109109
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
110110

111-
let output_format: OutputFormat = (&args).into();
112-
113111
let path1 = PathBuf::from(&args.file1);
114112
let path2 = PathBuf::from(&args.file2);
115113

@@ -124,16 +122,19 @@ fn check_difference(args: Args) -> io::Result<DiffExitStatus> {
124122
return Ok(DiffExitStatus::Trouble);
125123
}
126124

125+
let output_format: OutputFormat = (&args).into();
126+
127+
let format_options = FormatOptions::try_new(
128+
args.ignore_eol_space,
129+
output_format,
130+
args.label,
131+
args.label2,
132+
);
133+
let format_options = format_options.unwrap();
134+
127135
let path1_is_file = fs::metadata(&path1)?.is_file();
128136
let path2_is_file = fs::metadata(&path2)?.is_file();
129137

130-
let format_options = FormatOptions {
131-
ignore_trailing_white_spaces: args.ignore_eol_space,
132-
label1: args.label,
133-
label2: args.label2,
134-
output_format: output_format,
135-
};
136-
137138
if path1_is_file && path2_is_file {
138139
return FileDiff::file_diff(path1, path2, &format_options, None);
139140
} else if !path1_is_file && !path2_is_file {

text/diff_util/common.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
11
pub struct FormatOptions {
22
pub ignore_trailing_white_spaces: bool,
33
pub output_format: OutputFormat,
4-
pub label1: Option<String>,
5-
pub label2: Option<String>,
4+
label1: Option<String>,
5+
label2: Option<String>,
6+
}
7+
8+
impl FormatOptions {
9+
pub fn try_new(
10+
ignore_trailing_white_spaces: bool,
11+
output_format: OutputFormat,
12+
label1: Option<String>,
13+
label2: Option<String>,
14+
) -> Result<Self, &'static str> {
15+
if label1.is_none() && label2.is_some() {
16+
return Err("label1 can not be NONE when label2 is available");
17+
}
18+
19+
Ok(Self {
20+
ignore_trailing_white_spaces,
21+
output_format,
22+
label1,
23+
label2,
24+
})
25+
}
26+
27+
pub fn label1(&self) -> &Option<String> {
28+
&self.label1
29+
}
30+
31+
pub fn label2(&self) -> &Option<String> {
32+
&self.label2
33+
}
634
}
735

836
#[allow(dead_code)]

text/diff_util/dir_data.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,17 @@ pub struct DirData {
1414
}
1515

1616
impl DirData {
17-
pub fn load(path: PathBuf) -> io::Result<DirData> {
18-
let mut dir_data = DirData {
19-
path: path,
20-
files: Default::default(),
21-
};
22-
let entries = fs::read_dir(&dir_data.path)?;
17+
pub fn load(path: PathBuf) -> io::Result<Self> {
18+
let mut files: HashMap<OsString, DirEntry> = Default::default();
19+
20+
let entries = fs::read_dir(&path)?;
2321

2422
for entry in entries {
2523
let entry = entry?;
26-
27-
dir_data.files.insert(entry.file_name(), entry);
24+
files.insert(entry.file_name(), entry);
2825
}
2926

30-
Ok(dir_data)
27+
Ok(Self { path, files })
3128
}
3229

3330
pub fn files(&self) -> &HashMap<OsString, DirEntry> {

text/diff_util/dir_diff.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,23 @@ impl<'a> DirDiff<'a> {
117117
show_if_different.push_str("-b ");
118118
}
119119

120-
if let Some(label1) = &self.format_options.label1 {
120+
if let Some(label1) = &self.format_options.label1() {
121121
show_if_different.push_str(format!("--label {} ", label1).as_str())
122122
}
123123

124-
if let Some(label2) = &self.format_options.label2 {
124+
if let Some(label2) = &self.format_options.label2() {
125125
show_if_different.push_str(format!("--label2 {} ", label2).as_str())
126126
}
127127

128-
if let Some(label1) = &self.format_options.label1 {
128+
if let Some(label1) = &self.format_options.label1() {
129129
show_if_different.push_str(format!("{} ", label1).as_str())
130130
} else {
131131
show_if_different
132132
.push_str(path1.to_str().unwrap_or(COULD_NOT_UNWRAP_FILENAME));
133133
show_if_different.push(' ');
134134
}
135135

136-
if let Some(label2) = &self.format_options.label2 {
136+
if let Some(label2) = &self.format_options.label2() {
137137
show_if_different.push_str(format!("{} ", label2).as_str())
138138
} else {
139139
show_if_different

text/diff_util/file_diff.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,24 @@ pub struct FileDiff<'a> {
3131
}
3232

3333
impl<'a> FileDiff<'a> {
34-
pub fn are_different(&self) -> bool {
35-
self.are_different
36-
}
37-
3834
fn new(
3935
file1: &'a mut FileData,
4036
file2: &'a mut FileData,
4137
format_options: &'a FormatOptions,
4238
) -> Self {
43-
if format_options.label1.is_none() && format_options.label2.is_some() {
44-
panic!("label1 can not be NONE when label2 is available");
45-
}
46-
4739
Self {
4840
file1,
4941
file2,
5042
hunks: Hunks::new(),
51-
format_options: format_options,
43+
format_options,
5244
are_different: false,
5345
}
5446
}
5547

48+
pub fn are_different(&self) -> bool {
49+
self.are_different
50+
}
51+
5652
pub fn file_diff(
5753
path1: PathBuf,
5854
path2: PathBuf,
@@ -362,11 +358,11 @@ impl<'a> FileDiff<'a> {
362358
fn print_context(&mut self, context: usize) {
363359
println!(
364360
"*** {}",
365-
Self::get_header(self.file1, &self.format_options.label1)
361+
Self::get_header(self.file1, &self.format_options.label1())
366362
);
367363
println!(
368364
"--- {}",
369-
Self::get_header(self.file2, &self.format_options.label2)
365+
Self::get_header(self.file2, &self.format_options.label2())
370366
);
371367

372368
let change_ranges = self.get_context_ranges(context);
@@ -423,11 +419,11 @@ impl<'a> FileDiff<'a> {
423419
fn print_unified(&mut self, unified: usize) {
424420
println!(
425421
"--- {}",
426-
Self::get_header(self.file1, &self.format_options.label1)
422+
Self::get_header(self.file1, &self.format_options.label1())
427423
);
428424
println!(
429425
"+++ {}",
430-
Self::get_header(self.file2, &self.format_options.label2)
426+
Self::get_header(self.file2, &self.format_options.label2())
431427
);
432428

433429
let context_ranges = self.get_context_ranges(unified);

0 commit comments

Comments
 (0)