From 096e41086d662d6cf204ffa1bfcaec7f03ac6b4c Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Mon, 20 Jan 2025 18:21:09 +0100 Subject: [PATCH] du: move to thiserror --- src/uu/du/Cargo.toml | 1 + src/uu/du/src/du.rs | 49 ++++++++++++-------------------------------- 2 files changed, 14 insertions(+), 36 deletions(-) diff --git a/src/uu/du/Cargo.toml b/src/uu/du/Cargo.toml index 27ec1700a..0c0f2aaad 100644 --- a/src/uu/du/Cargo.toml +++ b/src/uu/du/Cargo.toml @@ -22,6 +22,7 @@ chrono = { workspace = true } glob = { workspace = true } clap = { workspace = true } uucore = { workspace = true, features = ["format"] } +thiserror = { workspace = true } [target.'cfg(target_os = "windows")'.dependencies] windows-sys = { workspace = true, features = [ diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index bd017f1d5..5b9017f31 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -8,8 +8,6 @@ use clap::{builder::PossibleValue, crate_version, Arg, ArgAction, ArgMatches, Co use glob::Pattern; use std::collections::HashSet; use std::env; -use std::error::Error; -use std::fmt::Display; #[cfg(not(windows))] use std::fs::Metadata; use std::fs::{self, DirEntry, File}; @@ -25,6 +23,7 @@ use std::str::FromStr; use std::sync::mpsc; use std::thread; use std::time::{Duration, UNIX_EPOCH}; +use thiserror::Error; use uucore::display::{print_verbatim, Quotable}; use uucore::error::{set_exit_code, FromIo, UError, UResult, USimpleError}; use uucore::line_ending::LineEnding; @@ -409,48 +408,26 @@ fn du( Ok(my_stat) } -#[derive(Debug)] +#[derive(Debug, Error)] enum DuError { + #[error("invalid maximum depth {depth}", depth = .0.quote())] InvalidMaxDepthArg(String), + + #[error("summarizing conflicts with --max-depth={depth}", depth = .0.maybe_quote())] SummarizeDepthConflict(String), + + #[error("invalid argument {style} for 'time style'\nValid arguments are:\n- 'full-iso'\n- 'long-iso'\n- 'iso'\nTry '{help}' for more information.", + style = .0.quote(), + help = uucore::execution_phrase())] InvalidTimeStyleArg(String), + + #[error("'birth' and 'creation' arguments for --time are not supported on this platform.")] InvalidTimeArg, + + #[error("Invalid exclude syntax: {0}")] InvalidGlob(String), } -impl Display for DuError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::InvalidMaxDepthArg(s) => write!(f, "invalid maximum depth {}", s.quote()), - Self::SummarizeDepthConflict(s) => { - write!( - f, - "summarizing conflicts with --max-depth={}", - s.maybe_quote() - ) - } - Self::InvalidTimeStyleArg(s) => write!( - f, - "invalid argument {} for 'time style' -Valid arguments are: -- 'full-iso' -- 'long-iso' -- 'iso' -Try '{} --help' for more information.", - s.quote(), - uucore::execution_phrase() - ), - Self::InvalidTimeArg => write!( - f, - "'birth' and 'creation' arguments for --time are not supported on this platform.", - ), - Self::InvalidGlob(s) => write!(f, "Invalid exclude syntax: {s}"), - } - } -} - -impl Error for DuError {} - impl UError for DuError { fn code(&self) -> i32 { match self {