diff --git a/src/uu/cksum/Cargo.toml b/src/uu/cksum/Cargo.toml index dcd5b56b1..208c01901 100644 --- a/src/uu/cksum/Cargo.toml +++ b/src/uu/cksum/Cargo.toml @@ -16,7 +16,7 @@ path = "src/cksum.rs" [dependencies] clap = { workspace = true } -uucore = { workspace = true, features = ["encoding", "sum"] } +uucore = { workspace = true, features = ["checksum", "encoding", "sum"] } hex = { workspace = true } regex = { workspace = true } diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index 746b22b10..7c66de2a1 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -6,7 +6,6 @@ // spell-checker:ignore (ToDO) fname, algo use clap::{crate_version, value_parser, Arg, ArgAction, Command}; use regex::Regex; -use std::cmp::Ordering; use std::error::Error; use std::ffi::OsStr; use std::fmt::Display; @@ -15,8 +14,8 @@ use std::io::BufRead; use std::io::{self, stdin, stdout, BufReader, Read, Write}; use std::iter; use std::path::Path; +use uucore::checksum::cksum_output; use uucore::error::set_exit_code; -use uucore::show_warning_caps; use uucore::{ encoding, error::{FromIo, UError, UResult, USimpleError}, @@ -501,41 +500,13 @@ where set_exit_code(1); } - // if any incorrectly formatted line, show it - match bad_format.cmp(&1) { - Ordering::Equal => { - show_warning_caps!("{} line is improperly formatted", bad_format); - } - Ordering::Greater => { - show_warning_caps!("{} lines are improperly formatted", bad_format); - } - Ordering::Less => {} - }; - // if we have any failed checksum verification, we set an exit code if failed_cksum > 0 || failed_open_file > 0 { set_exit_code(1); } - match failed_open_file.cmp(&1) { - Ordering::Equal => { - show_warning_caps!("{} listed file could not be read", failed_open_file); - } - Ordering::Greater => { - show_warning_caps!("{} listed files could not be read", failed_open_file); - } - Ordering::Less => {} - } - - match failed_cksum.cmp(&1) { - Ordering::Equal => { - show_warning_caps!("{} computed checksum did NOT match", failed_cksum); - } - Ordering::Greater => { - show_warning_caps!("{} computed checksums did NOT match", failed_cksum); - } - Ordering::Less => {} - }; + // if any incorrectly formatted line, show it + cksum_output(bad_format, failed_cksum, failed_open_file); Ok(()) } diff --git a/src/uu/hashsum/Cargo.toml b/src/uu/hashsum/Cargo.toml index f3799aedb..d450e3cef 100644 --- a/src/uu/hashsum/Cargo.toml +++ b/src/uu/hashsum/Cargo.toml @@ -16,7 +16,7 @@ path = "src/hashsum.rs" [dependencies] clap = { workspace = true } -uucore = { workspace = true, features = ["sum"] } +uucore = { workspace = true, features = ["checksum", "sum"] } memchr = { workspace = true } regex = { workspace = true } hex = { workspace = true } diff --git a/src/uu/hashsum/src/hashsum.rs b/src/uu/hashsum/src/hashsum.rs index 39b5a0a07..bddb4d5aa 100644 --- a/src/uu/hashsum/src/hashsum.rs +++ b/src/uu/hashsum/src/hashsum.rs @@ -12,7 +12,6 @@ use clap::{Arg, ArgMatches, Command}; use hex::encode; use regex::Captures; use regex::Regex; -use std::cmp::Ordering; use std::error::Error; use std::ffi::{OsStr, OsString}; use std::fs::File; @@ -20,6 +19,8 @@ use std::io::{self, stdin, BufRead, BufReader, Read}; use std::iter; use std::num::ParseIntError; use std::path::Path; +use uucore::checksum::cksum_output; +use uucore::display::Quotable; use uucore::error::USimpleError; use uucore::error::{set_exit_code, FromIo, UError, UResult}; use uucore::sum::{ @@ -27,7 +28,6 @@ use uucore::sum::{ Sha3_384, Sha3_512, Sha512, Shake128, Shake256, }; use uucore::util_name; -use uucore::{display::Quotable, show_warning_caps}; use uucore::{format_usage, help_about, help_usage}; const NAME: &str = "hashsum"; @@ -830,35 +830,7 @@ where } if !options.status && !skip_summary { - match bad_format.cmp(&1) { - Ordering::Equal => { - show_warning_caps!("{} line is improperly formatted", bad_format); - } - Ordering::Greater => { - show_warning_caps!("{} lines are improperly formatted", bad_format); - } - Ordering::Less => {} - }; - - match failed_cksum.cmp(&1) { - Ordering::Equal => { - show_warning_caps!("{} computed checksum did NOT match", failed_cksum); - } - Ordering::Greater => { - show_warning_caps!("{} computed checksums did NOT match", failed_cksum); - } - Ordering::Less => {} - }; - - match failed_open_file.cmp(&1) { - Ordering::Equal => { - show_warning_caps!("{} listed file could not be read", failed_open_file); - } - Ordering::Greater => { - show_warning_caps!("{} listed files could not be read", failed_open_file); - } - Ordering::Less => {} - } + cksum_output(bad_format, failed_cksum, failed_open_file); } Ok(()) diff --git a/src/uucore/Cargo.toml b/src/uucore/Cargo.toml index 2e7140b6b..81d202398 100644 --- a/src/uucore/Cargo.toml +++ b/src/uucore/Cargo.toml @@ -75,6 +75,7 @@ default = [] # * non-default features backup-control = [] colors = [] +checksum = [] 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.rs b/src/uucore/src/lib/features.rs index 423ff34ba..ef7b17b31 100644 --- a/src/uucore/src/lib/features.rs +++ b/src/uucore/src/lib/features.rs @@ -6,6 +6,8 @@ #[cfg(feature = "backup-control")] pub mod backup_control; +#[cfg(feature = "checksum")] +pub mod checksum; #[cfg(feature = "colors")] pub mod colors; #[cfg(feature = "encoding")] diff --git a/src/uucore/src/lib/features/checksum.rs b/src/uucore/src/lib/features/checksum.rs new file mode 100644 index 000000000..1652ce47c --- /dev/null +++ b/src/uucore/src/lib/features/checksum.rs @@ -0,0 +1,27 @@ +// This file is part of the uutils coreutils package. +// +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. + +use crate::show_warning_caps; + +#[allow(clippy::comparison_chain)] +pub fn cksum_output(bad_format: i32, failed_cksum: i32, failed_open_file: i32) { + if bad_format == 1 { + show_warning_caps!("{} line is improperly formatted", bad_format); + } else if bad_format > 1 { + show_warning_caps!("{} lines are improperly formatted", bad_format); + } + + if failed_cksum == 1 { + show_warning_caps!("{} computed checksum did NOT match", failed_cksum); + } else if failed_cksum > 1 { + show_warning_caps!("{} computed checksums did NOT match", failed_cksum); + } + + if failed_open_file == 1 { + show_warning_caps!("{} listed file could not be read", failed_open_file); + } else if failed_open_file > 1 { + show_warning_caps!("{} listed files could not be read", failed_open_file); + } +} diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index e891cc404..366c420d9 100644 --- a/src/uucore/src/lib/lib.rs +++ b/src/uucore/src/lib/lib.rs @@ -37,6 +37,8 @@ pub use crate::parser::shortcut_value_parser; // * feature-gated modules #[cfg(feature = "backup-control")] pub use crate::features::backup_control; +#[cfg(feature = "checksum")] +pub use crate::features::checksum; #[cfg(feature = "colors")] pub use crate::features::colors; #[cfg(feature = "encoding")]