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

cksum/hashsum: move to quick-error

This commit is contained in:
Sylvestre Ledru 2024-06-04 21:46:37 +02:00
parent 66ccb1a479
commit edae51d1a6
3 changed files with 49 additions and 71 deletions

1
Cargo.lock generated
View file

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

View file

@ -52,6 +52,7 @@ 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 }
@ -76,7 +77,7 @@ default = []
# * non-default features # * non-default features
backup-control = [] backup-control = []
colors = [] colors = []
checksum = ["regex", "sum"] checksum = ["quick-error", "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

@ -8,9 +8,7 @@ use data_encoding::BASE64;
use os_display::Quotable; use os_display::Quotable;
use regex::Regex; use regex::Regex;
use std::{ use std::{
error::Error,
ffi::OsStr, ffi::OsStr,
fmt::Display,
fs::File, fs::File,
io::{self, BufReader, Read}, io::{self, BufReader, Read},
path::Path, path::Path,
@ -70,82 +68,60 @@ pub struct HashAlgorithm {
pub bits: usize, pub bits: usize,
} }
#[derive(Debug)] quick_error! {
pub enum ChecksumError { #[derive(Debug)]
RawMultipleFiles, pub enum ChecksumError {
IgnoreNotCheck, RawMultipleFiles {
InvalidOutputSizeForSha3, display("the --raw option is not supported with multiple files")
BitsRequiredForSha3, }
BitsRequiredForShake128, IgnoreNotCheck {
BitsRequiredForShake256, display("the --ignore-missing option is meaningful only when verifying checksums")
UnknownAlgorithm, }
InvalidLength, InvalidOutputSizeForSha3 {
LengthOnlyForBlake2b, display("Invalid output size for SHA3 (expected 224, 256, 384, or 512)")
BinaryTextConflict, }
AlgorithmNotSupportedWithCheck, BitsRequiredForSha3 {
CombineMultipleAlgorithms, display("--bits required for SHA3")
NeedAlgorithmToHash, }
NoProperlyFormattedChecksumLinesFound(String), BitsRequiredForShake128 {
display("--bits required for SHAKE128")
}
BitsRequiredForShake256 {
display("--bits required for SHAKE256")
}
UnknownAlgorithm {
display("unknown algorithm: clap should have prevented this case")
}
InvalidLength {
display("length is not a multiple of 8")
}
LengthOnlyForBlake2b {
display("--length is only supported with --algorithm=blake2b")
}
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 Error for ChecksumError {}
impl UError for ChecksumError { impl UError for ChecksumError {
fn code(&self) -> i32 { fn code(&self) -> i32 {
1 1
} }
} }
impl Display for ChecksumError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::RawMultipleFiles => {
write!(f, "the --raw option is not supported with multiple files")
}
Self::IgnoreNotCheck => write!(
f,
"the --ignore-missing option is meaningful only when verifying checksums"
),
Self::InvalidOutputSizeForSha3 => write!(
f,
"Invalid output size for SHA3 (expected 224, 256, 384, or 512)"
),
Self::BitsRequiredForSha3 => write!(f, "--bits required for SHA3"),
Self::BitsRequiredForShake128 => write!(f, "--bits required for SHAKE128"),
Self::BitsRequiredForShake256 => write!(f, "--bits required for SHAKE256"),
Self::UnknownAlgorithm => {
write!(f, "unknown algorithm: clap should have prevented this case")
}
Self::InvalidLength => write!(f, "length is not a multiple of 8"),
Self::LengthOnlyForBlake2b => {
write!(f, "--length is only supported with --algorithm=blake2b")
}
Self::BinaryTextConflict => write!(
f,
"the --binary and --text options are meaningless when verifying checksums"
),
Self::AlgorithmNotSupportedWithCheck => write!(
f,
"--check is not supported with --algorithm={{bsd,sysv,crc}}"
),
Self::CombineMultipleAlgorithms => {
write!(f, "You cannot combine multiple hash algorithms!")
}
Self::NeedAlgorithmToHash => write!(
f,
"Needs an algorithm to hash with.\nUse --help for more information."
),
Self::NoProperlyFormattedChecksumLinesFound(filename) => {
write!(
f,
"{}: no properly formatted checksum lines found",
filename
)
}
}
}
}
/// Creates a SHA3 hasher instance based on the specified bits argument. /// Creates a SHA3 hasher instance based on the specified bits argument.
/// ///
/// # Returns /// # Returns