1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

feat(checksum): refactor ChecksumResult to include more counters in it

- Add comments to explain what each field is counting
This commit is contained in:
Dorian Péron 2024-11-17 16:28:53 +01:00
parent a0af49f2d8
commit 7c4724edc3

View file

@ -68,11 +68,27 @@ pub struct HashAlgorithm {
pub bits: usize, pub bits: usize,
} }
/// This structure holds the count of checksum test lines' outcomes.
#[derive(Default)] #[derive(Default)]
struct ChecksumResult { struct ChecksumResult {
pub bad_format: i32, /// Number of lines in the file where the computed checksum MATCHES
pub failed_cksum: i32, /// the expectation.
pub failed_open_file: i32, pub correct: u32,
/// Number of lines in the file where the computed checksum DIFFERS
/// from the expectation.
pub failed_cksum: u32,
pub failed_open_file: u32,
/// Number of improperly formatted lines.
pub bad_format: u32,
/// Total number of non-empty, non-comment lines.
pub total: u32,
}
impl ChecksumResult {
#[inline]
fn total_properly_formatted(&self) -> u32 {
self.total - self.bad_format
}
} }
/// Represents a reason for which the processing of a checksum line /// Represents a reason for which the processing of a checksum line
@ -681,8 +697,6 @@ fn process_checksum_file(
cli_algo_length: Option<usize>, cli_algo_length: Option<usize>,
opts: ChecksumOptions, opts: ChecksumOptions,
) -> Result<(), FileCheckError> { ) -> Result<(), FileCheckError> {
let mut correct_format = 0;
let mut properly_formatted_lines = 0;
let mut res = ChecksumResult::default(); let mut res = ChecksumResult::default();
let input_is_stdin = filename_input == OsStr::new("-"); let input_is_stdin = filename_input == OsStr::new("-");
@ -712,7 +726,7 @@ fn process_checksum_file(
}; };
for (i, line) in lines.iter().enumerate() { for (i, line) in lines.iter().enumerate() {
match process_checksum_line( let line_result = process_checksum_line(
filename_input, filename_input,
line, line,
i, i,
@ -721,34 +735,31 @@ fn process_checksum_file(
cli_algo_name, cli_algo_name,
cli_algo_length, cli_algo_length,
opts, opts,
) { );
Ok(()) => {
correct_format += 1; // Match a first time to elude critical UErrors, and increment the total
properly_formatted_lines += 1 // in all cases except on skipped.
} use LineCheckError::*;
Err(LineCheckError::DigestMismatch) => { match line_result {
res.failed_cksum += 1; Err(UError(e)) => return Err(e.into()),
properly_formatted_lines += 1 Err(Skipped) => (),
} _ => res.total += 1,
Err(LineCheckError::UError(e)) => return Err(e.into()),
Err(LineCheckError::Skipped) => continue,
Err(LineCheckError::ImproperlyFormatted) => res.bad_format += 1,
Err(LineCheckError::CantOpenFile | LineCheckError::FileIsDirectory) => {
properly_formatted_lines += 1;
res.failed_open_file += 1
}
Err(LineCheckError::FileNotFound) => {
properly_formatted_lines += 1;
if !opts.ignore_missing {
res.failed_open_file += 1
}
} }
// Match a second time to update the right field of `res`.
match line_result {
Ok(()) => res.correct += 1,
Err(DigestMismatch) => res.failed_cksum += 1,
Err(ImproperlyFormatted) => res.bad_format += 1,
Err(CantOpenFile | FileIsDirectory) => res.failed_open_file += 1,
Err(FileNotFound) if !opts.ignore_missing => res.failed_open_file += 1,
_ => continue,
}; };
} }
// not a single line correctly formatted found // not a single line correctly formatted found
// return an error // return an error
if properly_formatted_lines == 0 { if res.total_properly_formatted() == 0 {
if !opts.status { if !opts.status {
log_no_properly_formatted(get_filename_for_output(filename_input, input_is_stdin)); log_no_properly_formatted(get_filename_for_output(filename_input, input_is_stdin));
} }
@ -759,7 +770,7 @@ fn process_checksum_file(
// if any incorrectly formatted line, show it // if any incorrectly formatted line, show it
cksum_output(&res, opts.status); cksum_output(&res, opts.status);
if opts.ignore_missing && correct_format == 0 { if opts.ignore_missing && res.correct == 0 {
// we have only bad format // we have only bad format
// and we had ignore-missing // and we had ignore-missing
eprintln!( eprintln!(