From edae51d1a61edeee2e81896c1eb0949962b33310 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Tue, 4 Jun 2024 21:46:37 +0200 Subject: [PATCH] cksum/hashsum: move to quick-error --- Cargo.lock | 1 + src/uucore/Cargo.toml | 3 +- src/uucore/src/lib/features/checksum.rs | 116 ++++++++++-------------- 3 files changed, 49 insertions(+), 71 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 032c8e77e..4395dfe3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3443,6 +3443,7 @@ dependencies = [ "number_prefix", "once_cell", "os_display", + "quick-error", "regex", "sha1", "sha2", diff --git a/src/uucore/Cargo.toml b/src/uucore/Cargo.toml index 5730803c9..3a87fe26a 100644 --- a/src/uucore/Cargo.toml +++ b/src/uucore/Cargo.toml @@ -52,6 +52,7 @@ blake2b_simd = { workspace = true, optional = true } blake3 = { workspace = true, optional = true } sm3 = { workspace = true, optional = true } regex = { workspace = true, optional = true } +quick-error = { workspace = true, optional = true } [target.'cfg(unix)'.dependencies] walkdir = { workspace = true, optional = true } @@ -76,7 +77,7 @@ default = [] # * non-default features backup-control = [] colors = [] -checksum = ["regex", "sum"] +checksum = ["quick-error", "regex", "sum"] encoding = ["data-encoding", "data-encoding-macro", "z85", "thiserror"] entries = ["libc"] fs = ["dunce", "libc", "winapi-util", "windows-sys"] diff --git a/src/uucore/src/lib/features/checksum.rs b/src/uucore/src/lib/features/checksum.rs index 908db5b8b..fbea911ef 100644 --- a/src/uucore/src/lib/features/checksum.rs +++ b/src/uucore/src/lib/features/checksum.rs @@ -8,9 +8,7 @@ use data_encoding::BASE64; use os_display::Quotable; use regex::Regex; use std::{ - error::Error, ffi::OsStr, - fmt::Display, fs::File, io::{self, BufReader, Read}, path::Path, @@ -70,82 +68,60 @@ pub struct HashAlgorithm { pub bits: usize, } -#[derive(Debug)] -pub enum ChecksumError { - RawMultipleFiles, - IgnoreNotCheck, - InvalidOutputSizeForSha3, - BitsRequiredForSha3, - BitsRequiredForShake128, - BitsRequiredForShake256, - UnknownAlgorithm, - InvalidLength, - LengthOnlyForBlake2b, - BinaryTextConflict, - AlgorithmNotSupportedWithCheck, - CombineMultipleAlgorithms, - NeedAlgorithmToHash, - NoProperlyFormattedChecksumLinesFound(String), +quick_error! { + #[derive(Debug)] + pub enum ChecksumError { + RawMultipleFiles { + display("the --raw option is not supported with multiple files") + } + IgnoreNotCheck { + display("the --ignore-missing option is meaningful only when verifying checksums") + } + InvalidOutputSizeForSha3 { + display("Invalid output size for SHA3 (expected 224, 256, 384, or 512)") + } + BitsRequiredForSha3 { + display("--bits required for SHA3") + } + 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 { fn code(&self) -> i32 { 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. /// /// # Returns