uucore: create a new function to manage the warning/error display

This commit is contained in:
Sylvestre Ledru 2024-05-10 23:49:02 +02:00
parent 81500ae3b8
commit 0b04bcaf9a
8 changed files with 40 additions and 65 deletions

View file

@ -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 }

View file

@ -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(())
}

View file

@ -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 }

View file

@ -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(())

View file

@ -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"]

View file

@ -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")]

View file

@ -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);
}
}

View file

@ -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")]