From 95550ac88b5eed56e9812851b04513b590a0bda8 Mon Sep 17 00:00:00 2001 From: Chandra Kiran G Date: Sun, 23 Mar 2025 21:05:11 +0530 Subject: [PATCH 1/5] refactor: Add thiserror to df --- Cargo.lock | 1 + src/uu/df/Cargo.toml | 1 + src/uu/df/src/df.rs | 60 +++++++------------------------------------- 3 files changed, 11 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cda5577108e..f206e86b50b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2674,6 +2674,7 @@ version = "0.0.30" dependencies = [ "clap", "tempfile", + "thiserror 2.0.12", "unicode-width 0.2.0", "uucore", ] diff --git a/src/uu/df/Cargo.toml b/src/uu/df/Cargo.toml index a749d7087f6..38ee75764e4 100644 --- a/src/uu/df/Cargo.toml +++ b/src/uu/df/Cargo.toml @@ -20,6 +20,7 @@ path = "src/df.rs" clap = { workspace = true } uucore = { workspace = true, features = ["libc", "fsext"] } unicode-width = { workspace = true } +thiserror.workspace = true [dev-dependencies] tempfile = { workspace = true } diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index 8d5b7a6c5d7..fcd2286e529 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -19,10 +19,9 @@ use uucore::{format_usage, help_about, help_section, help_usage, show}; use clap::{parser::ValueSource, Arg, ArgAction, ArgMatches, Command}; -use std::error::Error; use std::ffi::OsString; -use std::fmt; use std::path::Path; +use thiserror::Error; use crate::blocks::{read_block_size, BlockSize}; use crate::columns::{Column, ColumnError}; @@ -114,52 +113,21 @@ impl Default for Options { } } -#[derive(Debug)] +#[derive(Debug, Error)] enum OptionsError { + #[error("--block-size argument {} too large", .0.quote())] BlockSizeTooLarge(String), + #[error("invalid --block-size argument {}", .0.quote())] InvalidBlockSize(String), + #[error("invalid suffix in --block-size argument {}", .0.quote())] InvalidSuffix(String), - /// An error getting the columns to display in the output table. + #[error("option --output: field used more than once {:?}", .0)] ColumnError(ColumnError), - + #[error("error getting the filesystem types to include in the output table")] FilesystemTypeBothSelectedAndExcluded(Vec), } -impl fmt::Display for OptionsError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - // TODO This needs to vary based on whether `--block-size` - // or `-B` were provided. - Self::BlockSizeTooLarge(s) => { - write!(f, "--block-size argument {} too large", s.quote()) - } - // TODO This needs to vary based on whether `--block-size` - // or `-B` were provided. - Self::InvalidBlockSize(s) => write!(f, "invalid --block-size argument {s}"), - // TODO This needs to vary based on whether `--block-size` - // or `-B` were provided. - Self::InvalidSuffix(s) => write!(f, "invalid suffix in --block-size argument {s}"), - Self::ColumnError(ColumnError::MultipleColumns(s)) => write!( - f, - "option --output: field {} used more than once", - s.quote() - ), - #[allow(clippy::print_in_format_impl)] - Self::FilesystemTypeBothSelectedAndExcluded(types) => { - for t in types { - eprintln!( - "{}: file system type {} both selected and excluded", - uucore::util_name(), - t.quote() - ); - } - Ok(()) - } - } - } -} - impl Options { /// Convert command-line arguments into [`Options`]. fn from(matches: &ArgMatches) -> Result { @@ -426,28 +394,18 @@ where Ok(result) } -#[derive(Debug)] +#[derive(Debug, Error)] enum DfError { - /// A problem while parsing command-line options. + #[error("problem while parsing command-line options")] OptionsError(OptionsError), } -impl Error for DfError {} - impl UError for DfError { fn usage(&self) -> bool { matches!(self, Self::OptionsError(OptionsError::ColumnError(_))) } } -impl fmt::Display for DfError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Self::OptionsError(e) => e.fmt(f), - } - } -} - #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().try_get_matches_from(args)?; From de50f13534582f14526576d202d4fe935e48e098 Mon Sep 17 00:00:00 2001 From: Chandra Kiran G Date: Mon, 24 Mar 2025 01:48:40 +0530 Subject: [PATCH 2/5] fix: Try fixing tests --- src/uu/df/Cargo.toml | 2 +- src/uu/df/src/columns.rs | 6 ++++-- src/uu/df/src/df.rs | 12 ++++++------ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/uu/df/Cargo.toml b/src/uu/df/Cargo.toml index 38ee75764e4..4523703c530 100644 --- a/src/uu/df/Cargo.toml +++ b/src/uu/df/Cargo.toml @@ -20,7 +20,7 @@ path = "src/df.rs" clap = { workspace = true } uucore = { workspace = true, features = ["libc", "fsext"] } unicode-width = { workspace = true } -thiserror.workspace = true +thiserror = { workspace = true } [dev-dependencies] tempfile = { workspace = true } diff --git a/src/uu/df/src/columns.rs b/src/uu/df/src/columns.rs index 3c0a244192e..4890f0ba333 100644 --- a/src/uu/df/src/columns.rs +++ b/src/uu/df/src/columns.rs @@ -5,6 +5,8 @@ // spell-checker:ignore itotal iused iavail ipcent pcent squashfs use crate::{OPT_INODES, OPT_OUTPUT, OPT_PRINT_TYPE}; use clap::{parser::ValueSource, ArgMatches}; +use thiserror::Error; +use uucore::display::Quotable; /// The columns in the output table produced by `df`. /// @@ -56,9 +58,9 @@ pub(crate) enum Column { } /// An error while defining which columns to display in the output table. -#[derive(Debug)] +#[derive(Debug, Error)] pub(crate) enum ColumnError { - /// If a column appears more than once in the `--output` argument. + #[error("{}", .0.quote())] MultipleColumns(String), } diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index fcd2286e529..91165e31909 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -117,14 +117,13 @@ impl Default for Options { enum OptionsError { #[error("--block-size argument {} too large", .0.quote())] BlockSizeTooLarge(String), - #[error("invalid --block-size argument {}", .0.quote())] + #[error("invalid --block-size argument {}", .0)] InvalidBlockSize(String), - #[error("invalid suffix in --block-size argument {}", .0.quote())] + #[error("invalid suffix in --block-size argument {}", .0)] InvalidSuffix(String), - - #[error("option --output: field used more than once {:?}", .0)] + #[error("option --output: field {} used more than once", .0)] ColumnError(ColumnError), - #[error("error getting the filesystem types to include in the output table")] + #[error("file system type {} both selected and excluded", .0[0].quote())] FilesystemTypeBothSelectedAndExcluded(Vec), } @@ -396,7 +395,8 @@ where #[derive(Debug, Error)] enum DfError { - #[error("problem while parsing command-line options")] + /// A problem while parsing command-line options. + #[error("{}", .0)] OptionsError(OptionsError), } From 464fd4a3717b602c783afe6154ed62165e252221 Mon Sep 17 00:00:00 2001 From: Chandra Kiran G Date: Tue, 1 Apr 2025 00:27:44 +0530 Subject: [PATCH 3/5] refactor(df): Move `df` to `thiserror` --- src/uu/df/src/df.rs | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index 91165e31909..55ee8839847 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -20,6 +20,7 @@ use uucore::{format_usage, help_about, help_section, help_usage, show}; use clap::{parser::ValueSource, Arg, ArgAction, ArgMatches, Command}; use std::ffi::OsString; +use std::fmt; use std::path::Path; use thiserror::Error; @@ -113,20 +114,52 @@ impl Default for Options { } } -#[derive(Debug, Error)] +#[derive(Debug)] enum OptionsError { - #[error("--block-size argument {} too large", .0.quote())] BlockSizeTooLarge(String), - #[error("invalid --block-size argument {}", .0)] InvalidBlockSize(String), - #[error("invalid suffix in --block-size argument {}", .0)] InvalidSuffix(String), - #[error("option --output: field {} used more than once", .0)] + + /// An error getting the columns to display in the output table. ColumnError(ColumnError), - #[error("file system type {} both selected and excluded", .0[0].quote())] + FilesystemTypeBothSelectedAndExcluded(Vec), } +impl fmt::Display for OptionsError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + // TODO This needs to vary based on whether `--block-size` + // or `-B` were provided. + Self::BlockSizeTooLarge(s) => { + write!(f, "--block-size argument {} too large", s.quote()) + } + // TODO This needs to vary based on whether `--block-size` + // or `-B` were provided. + Self::InvalidBlockSize(s) => write!(f, "invalid --block-size argument {s}"), + // TODO This needs to vary based on whether `--block-size` + // or `-B` were provided. + Self::InvalidSuffix(s) => write!(f, "invalid suffix in --block-size argument {s}"), + Self::ColumnError(ColumnError::MultipleColumns(s)) => write!( + f, + "option --output: field {} used more than once", + s.quote() + ), + #[allow(clippy::print_in_format_impl)] + Self::FilesystemTypeBothSelectedAndExcluded(types) => { + for t in types { + eprintln!( + "{}: file system type {} both selected and excluded", + uucore::util_name(), + t.quote() + ); + } + Ok(()) + } + } + } +} + impl Options { /// Convert command-line arguments into [`Options`]. fn from(matches: &ArgMatches) -> Result { From 6a8520a8f3aa16d433bc8505eaf78c9b9e809eae Mon Sep 17 00:00:00 2001 From: Chandra Kiran G <121796315+kiran-4444@users.noreply.github.com> Date: Tue, 1 Apr 2025 01:56:40 +0530 Subject: [PATCH 4/5] chore(df): Add back comment --- src/uu/df/src/columns.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/uu/df/src/columns.rs b/src/uu/df/src/columns.rs index 4890f0ba333..6af847c8939 100644 --- a/src/uu/df/src/columns.rs +++ b/src/uu/df/src/columns.rs @@ -60,6 +60,7 @@ pub(crate) enum Column { /// An error while defining which columns to display in the output table. #[derive(Debug, Error)] pub(crate) enum ColumnError { + /// If a column appears more than once in the `--output` argument. #[error("{}", .0.quote())] MultipleColumns(String), } From d2e380e23471c6d814d3d4b5cc073bfbb0ccd85c Mon Sep 17 00:00:00 2001 From: Chandra Kiran G Date: Tue, 1 Apr 2025 02:13:26 +0530 Subject: [PATCH 5/5] chore: Refactor column.rs correctly --- src/uu/df/src/columns.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/df/src/columns.rs b/src/uu/df/src/columns.rs index 6af847c8939..0d2d121a3d1 100644 --- a/src/uu/df/src/columns.rs +++ b/src/uu/df/src/columns.rs @@ -4,7 +4,7 @@ // file that was distributed with this source code. // spell-checker:ignore itotal iused iavail ipcent pcent squashfs use crate::{OPT_INODES, OPT_OUTPUT, OPT_PRINT_TYPE}; -use clap::{parser::ValueSource, ArgMatches}; +use clap::{ArgMatches, parser::ValueSource}; use thiserror::Error; use uucore::display::Quotable;