Skip to content

Commit 4c7d472

Browse files
committed
misc: Apply clippy suggestions
1 parent 93120ec commit 4c7d472

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

src/colorize.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ where
66
F: FnMut(&str, &str),
77
{
88
let prefix = if let Some(key) = key {
9-
format!("{}: ", key)
9+
format!("{key}: ")
1010
} else {
11-
"".to_owned()
11+
String::new()
1212
};
13-
let subindent = &format!("{} ", indent);
13+
let subindent = &format!("{indent} ");
1414

1515
match diff {
1616
Value::Object(obj) => {
@@ -20,7 +20,7 @@ where
2020
subcolorize(key, old, output, "-", indent);
2121
subcolorize(key, new, output, "+", indent);
2222
} else {
23-
output(color, &format!("{}{}{{", indent, prefix));
23+
output(color, &format!("{indent}{prefix}{{"));
2424
let re_delete = Regex::new(r"^(.*)__deleted$").unwrap();
2525
let re_added = Regex::new(r"^(.*)__added$").unwrap();
2626
for (subkey, subvalue) in obj {
@@ -44,13 +44,13 @@ where
4444
);
4545
continue;
4646
}
47-
subcolorize(Some(&subkey), subvalue, output, color, subindent);
47+
subcolorize(Some(subkey), subvalue, output, color, subindent);
4848
}
49-
output(color, &format!("{}}}", indent));
49+
output(color, &format!("{indent}}}"));
5050
}
5151
}
5252
Value::Array(array) => {
53-
output(color, &format!("{}{}[", indent, prefix));
53+
output(color, &format!("{indent}{prefix}["));
5454

5555
let mut looks_like_diff = true;
5656
for item in array {
@@ -76,11 +76,9 @@ where
7676
let op = subitem[0].as_str().unwrap();
7777
let subvalue = &subitem.get(1);
7878
if op == " " && subvalue.is_none() {
79-
output(" ", &format!("{}...", subindent));
79+
output(" ", &format!("{subindent}..."));
8080
} else {
81-
if !([" ", "-", "+", "~"].contains(&op)) {
82-
panic!("Unexpected op '{}'", op);
83-
}
81+
assert!(([" ", "-", "+", "~"].contains(&op)), "Unexpected op '{}'", op);
8482
let subvalue = subvalue.unwrap();
8583
let color = if op == "~" { " " } else { op };
8684
subcolorize(None, subvalue, output, color, subindent);
@@ -93,7 +91,7 @@ where
9391
}
9492
}
9593

96-
output(color, &format!("{}]", indent));
94+
output(color, &format!("{indent}]"));
9795
}
9896
_ => output(color, &(indent.to_owned() + &prefix + &diff.to_string())),
9997
}
@@ -102,11 +100,11 @@ where
102100
/// Returns the JSON structural difference formatted as a `Vec<String>`.
103101
///
104102
/// If `None`, there is no JSON structural difference to be formatted.
105-
pub fn colorize_to_array(diff: &Value) -> Vec<String> {
103+
#[must_use] pub fn colorize_to_array(diff: &Value) -> Vec<String> {
106104
let mut output: Vec<String> = Vec::new();
107105

108106
let mut output_func = |color: &str, line: &str| {
109-
output.push(format!("{}{}", color, line));
107+
output.push(format!("{color}{line}"));
110108
};
111109

112110
subcolorize(None, diff, &mut output_func, " ", "");

src/diff.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,15 @@ impl BestMatch {
3535

3636
impl JsonDiff {
3737
/// Finds the JSON structural difference of two JSON files.
38-
pub fn diff(json1: &Value, json2: &Value, keys_only: bool) -> Self {
38+
#[must_use] pub fn diff(json1: &Value, json2: &Value, keys_only: bool) -> Self {
3939
Self::diff_with_score(json1, json2, keys_only)
4040
}
4141

4242
/// Finds the JSON structural difference of two JSON files and
4343
/// returns it as a formatted string.
44-
pub fn diff_string(json1: &Value, json2: &Value, keys_only: bool) -> Option<String> {
44+
#[must_use] pub fn diff_string(json1: &Value, json2: &Value, keys_only: bool) -> Option<String> {
4545
let Self { score: _, diff } = Self::diff(json1, json2, keys_only);
46-
if let Some(value) = diff {
47-
Some(colorize_to_array(&value).join("\n") + "\n")
48-
} else {
49-
None
50-
}
46+
diff.map(|value| colorize_to_array(&value).join("\n") + "\n")
5147
}
5248

5349
fn object_diff(obj1: &Map<String, Value>, obj2: &Map<String, Value>, keys_only: bool) -> Self {
@@ -56,15 +52,15 @@ impl JsonDiff {
5652

5753
for (key, value1) in obj1 {
5854
if !obj2.contains_key(key) {
59-
let key_deleted = format!("{}__deleted", key);
55+
let key_deleted = format!("{key}__deleted");
6056
result.insert(key_deleted, value1.clone());
6157
score -= 30.;
6258
}
6359
}
6460

6561
for (key, value2) in obj2 {
6662
if !obj1.contains_key(key) {
67-
let key_added = format!("{}__added", key);
63+
let key_added = format!("{key}__added");
6864
result.insert(key_added, value2.clone());
6965
score -= 30.;
7066
}
@@ -76,11 +72,11 @@ impl JsonDiff {
7672
let Self {
7773
score: subscore,
7874
diff: change,
79-
} = Self::diff_with_score(&value1, &value2, keys_only);
75+
} = Self::diff_with_score(value1, value2, keys_only);
8076
if let Some(change) = change {
8177
result.insert(key.clone(), change);
8278
}
83-
score += ((subscore / 5.).max(-10.)).min(20.);
79+
score += (subscore / 5.).clamp(-10., 20.);
8480
}
8581
}
8682

@@ -117,7 +113,7 @@ impl JsonDiff {
117113

118114
for (match_index, (key, candidate)) in fuzzy_originals.into_iter().enumerate() {
119115
if key != "__next" {
120-
let index_distance = (match_index as isize - index as isize).abs() as usize;
116+
let index_distance = (match_index as isize - index as isize).unsigned_abs();
121117
if Self::check_type(item, candidate) {
122118
let Self { score, diff: _ } = Self::diff(item, candidate, false);
123119
if best_match.as_ref().map_or(true, |v| score > v.score)
@@ -232,12 +228,10 @@ impl JsonDiff {
232228
"equal" => {
233229
for key in seq1.iter().take(opcode.first_end).skip(opcode.first_start) {
234230
let is_scalarized1 = Self::is_scalarized(key, &originals1);
235-
if is_scalarized1 && !(Self::is_scalarized(key, &originals2)) {
236-
panic!(
231+
assert!(!(is_scalarized1 && !(Self::is_scalarized(key, &originals2))),
237232
"Internal bug: the items associated to the key {} are different in the two dictionaries",
238233
key
239234
);
240-
}
241235
if is_scalarized1 {
242236
let item1 = Self::descalarize(key, &scalar_values1, &originals1);
243237
let item2 = Self::descalarize(key, &scalar_values2, &originals2);

0 commit comments

Comments
 (0)