1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Adjust identify_algo_name_and_length to return Option<(String, Option<usize>)> instead of (String, Option<usize>)

This commit is contained in:
Sylvestre Ledru 2024-06-26 23:45:46 +02:00
parent adcc548f7c
commit 212ddf7d8c

View file

@ -418,7 +418,7 @@ fn identify_algo_name_and_length(
algo_name_input: Option<&str>, algo_name_input: Option<&str>,
res: &mut ChecksumResult, res: &mut ChecksumResult,
properly_formatted: &mut bool, properly_formatted: &mut bool,
) -> (String, Option<usize>) { ) -> Option<(String, Option<usize>)> {
// When the algo-based format is matched, extract details from regex captures // When the algo-based format is matched, extract details from regex captures
let algorithm = caps.name("algo").map_or("", |m| m.as_str()).to_lowercase(); let algorithm = caps.name("algo").map_or("", |m| m.as_str()).to_lowercase();
@ -428,13 +428,13 @@ fn identify_algo_name_and_length(
if algo_name_input.is_some() && algo_name_input != Some(&algorithm) { if algo_name_input.is_some() && algo_name_input != Some(&algorithm) {
res.bad_format += 1; res.bad_format += 1;
*properly_formatted = false; *properly_formatted = false;
return (String::new(), None); return None;
} }
if !SUPPORTED_ALGORITHMS.contains(&algorithm.as_str()) { if !SUPPORTED_ALGORITHMS.contains(&algorithm.as_str()) {
// Not supported algo, leave early // Not supported algo, leave early
*properly_formatted = false; *properly_formatted = false;
return (String::new(), None); return None;
} }
let bits = caps.name("bits").map_or(Some(None), |m| { let bits = caps.name("bits").map_or(Some(None), |m| {
@ -445,15 +445,9 @@ fn identify_algo_name_and_length(
*properly_formatted = false; *properly_formatted = false;
None // Return None to signal a divisibility issue None // Return None to signal a divisibility issue
} }
}); })?;
if bits.is_none() { Some((algorithm, bits))
// If bits is None, we have a parsing or divisibility issue
// Exit the loop outside of the closure
return (String::new(), None);
}
(algorithm, bits.unwrap())
} }
/*** /***
@ -518,6 +512,7 @@ where
&mut res, &mut res,
&mut properly_formatted, &mut properly_formatted,
) )
.unwrap_or((String::new(), None))
} else if let Some(a) = algo_name_input { } else if let Some(a) = algo_name_input {
// When a specific algorithm name is input, use it and use the provided bits // When a specific algorithm name is input, use it and use the provided bits
(a.to_lowercase(), length_input) (a.to_lowercase(), length_input)