cksum/hashsum: move to thiserror

This commit is contained in:
Sylvestre Ledru 2024-06-19 18:31:34 +02:00
parent edae51d1a6
commit cc8bda5def
3 changed files with 42 additions and 62 deletions

1
Cargo.lock generated
View file

@ -3443,7 +3443,6 @@ dependencies = [
"number_prefix", "number_prefix",
"once_cell", "once_cell",
"os_display", "os_display",
"quick-error",
"regex", "regex",
"sha1", "sha1",
"sha2", "sha2",

View file

@ -52,7 +52,6 @@ blake2b_simd = { workspace = true, optional = true }
blake3 = { workspace = true, optional = true } blake3 = { workspace = true, optional = true }
sm3 = { workspace = true, optional = true } sm3 = { workspace = true, optional = true }
regex = { workspace = true, optional = true } regex = { workspace = true, optional = true }
quick-error = { workspace = true, optional = true }
[target.'cfg(unix)'.dependencies] [target.'cfg(unix)'.dependencies]
walkdir = { workspace = true, optional = true } walkdir = { workspace = true, optional = true }
@ -77,7 +76,7 @@ default = []
# * non-default features # * non-default features
backup-control = [] backup-control = []
colors = [] colors = []
checksum = ["quick-error", "regex", "sum"] checksum = ["thiserror", "regex", "sum"]
encoding = ["data-encoding", "data-encoding-macro", "z85", "thiserror"] encoding = ["data-encoding", "data-encoding-macro", "z85", "thiserror"]
entries = ["libc"] entries = ["libc"]
fs = ["dunce", "libc", "winapi-util", "windows-sys"] fs = ["dunce", "libc", "winapi-util", "windows-sys"]

View file

@ -23,9 +23,9 @@ use crate::{
}, },
util_name, util_name,
}; };
use quick_error::quick_error;
use std::io::stdin; use std::io::stdin;
use std::io::BufRead; use std::io::BufRead;
use thiserror::Error;
pub const ALGORITHM_OPTIONS_SYSV: &str = "sysv"; pub const ALGORITHM_OPTIONS_SYSV: &str = "sysv";
pub const ALGORITHM_OPTIONS_BSD: &str = "bsd"; pub const ALGORITHM_OPTIONS_BSD: &str = "bsd";
@ -68,52 +68,36 @@ pub struct HashAlgorithm {
pub bits: usize, pub bits: usize,
} }
quick_error! { #[derive(Debug, Error)]
#[derive(Debug)] pub enum ChecksumError {
pub enum ChecksumError { #[error("the --raw option is not supported with multiple files")]
RawMultipleFiles { RawMultipleFiles,
display("the --raw option is not supported with multiple files") #[error("the --ignore-missing option is meaningful only when verifying checksums")]
} IgnoreNotCheck,
IgnoreNotCheck { #[error("Invalid output size for SHA3 (expected 224, 256, 384, or 512)")]
display("the --ignore-missing option is meaningful only when verifying checksums") InvalidOutputSizeForSha3,
} #[error("--bits required for SHA3")]
InvalidOutputSizeForSha3 { BitsRequiredForSha3,
display("Invalid output size for SHA3 (expected 224, 256, 384, or 512)") #[error("--bits required for SHAKE128")]
} BitsRequiredForShake128,
BitsRequiredForSha3 { #[error("--bits required for SHAKE256")]
display("--bits required for SHA3") BitsRequiredForShake256,
} #[error("unknown algorithm: clap should have prevented this case")]
BitsRequiredForShake128 { UnknownAlgorithm,
display("--bits required for SHAKE128") #[error("length is not a multiple of 8")]
} InvalidLength,
BitsRequiredForShake256 { #[error("--length is only supported with --algorithm=blake2b")]
display("--bits required for SHAKE256") LengthOnlyForBlake2b,
} #[error("the --binary and --text options are meaningless when verifying checksums")]
UnknownAlgorithm { BinaryTextConflict,
display("unknown algorithm: clap should have prevented this case") #[error("--check is not supported with --algorithm={{bsd,sysv,crc}}")]
} AlgorithmNotSupportedWithCheck,
InvalidLength { #[error("You cannot combine multiple hash algorithms!")]
display("length is not a multiple of 8") CombineMultipleAlgorithms,
} #[error("Needs an algorithm to hash with.\nUse --help for more information.")]
LengthOnlyForBlake2b { NeedAlgorithmToHash,
display("--length is only supported with --algorithm=blake2b") #[error("{filename}: no properly formatted checksum lines found")]
} NoProperlyFormattedChecksumLinesFound { filename: String },
BinaryTextConflict {
display("the --binary and --text options are meaningless when verifying checksums")
}
AlgorithmNotSupportedWithCheck {
display("--check is not supported with --algorithm={{bsd,sysv,crc}}")
}
CombineMultipleAlgorithms {
display("You cannot combine multiple hash algorithms!")
}
NeedAlgorithmToHash {
display("Needs an algorithm to hash with.\nUse --help for more information.")
}
NoProperlyFormattedChecksumLinesFound(filename: String) {
display("{filename}: no properly formatted checksum lines found")
}
}
} }
impl UError for ChecksumError { impl UError for ChecksumError {
@ -332,14 +316,10 @@ fn determine_regex(
return Ok((algo_based_regex_base64, true)); return Ok((algo_based_regex_base64, true));
} }
} }
Err(ChecksumError::NoProperlyFormattedChecksumLinesFound {
Err( filename: get_filename_for_output(filename, input_is_stdin),
ChecksumError::NoProperlyFormattedChecksumLinesFound(get_filename_for_output( }
filename, .into())
input_is_stdin,
))
.into(),
)
} }
// Function to convert bytes to a hexadecimal string // Function to convert bytes to a hexadecimal string
@ -368,7 +348,9 @@ fn get_expected_checksum(
} }
} }
Err(_) => Err(Box::new( Err(_) => Err(Box::new(
ChecksumError::NoProperlyFormattedChecksumLinesFound((&filename).to_string()), ChecksumError::NoProperlyFormattedChecksumLinesFound {
filename: (&filename).to_string(),
},
)), )),
} }
} else { } else {
@ -579,9 +561,9 @@ where
// return an error // return an error
if !properly_formatted { if !properly_formatted {
if !status { if !status {
return Err(ChecksumError::NoProperlyFormattedChecksumLinesFound( return Err(ChecksumError::NoProperlyFormattedChecksumLinesFound {
get_filename_for_output(filename_input, input_is_stdin), filename: get_filename_for_output(filename_input, input_is_stdin),
) }
.into()); .into());
} }
set_exit_code(1); set_exit_code(1);