From 36e7b28426feaad2fcfe0a162f170a7cb0c75e69 Mon Sep 17 00:00:00 2001 From: Lewis Boon <4803529+lewisboon@users.noreply.github.com> Date: Sat, 5 Apr 2025 23:11:10 +0100 Subject: [PATCH 1/2] df: migrate OptionsError to thiserror Closed #7536 --- src/uu/df/src/df.rs | 42 ++++++------------------------------------ 1 file changed, 6 insertions(+), 36 deletions(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index 517673b7b..218aaa425 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -20,7 +20,6 @@ use uucore::{format_usage, help_about, help_section, help_usage, show}; use clap::{Arg, ArgAction, ArgMatches, Command, parser::ValueSource}; use std::ffi::OsString; -use std::fmt; use std::path::Path; use thiserror::Error; @@ -114,52 +113,23 @@ impl Default for Options { } } -#[derive(Debug)] +#[derive(Debug, Error)] enum OptionsError { + #[error("--block-size argument '{0}' too large")] BlockSizeTooLarge(String), + #[error("invalid --block-size argument {0}")] InvalidBlockSize(String), + #[error("invalid suffix in --block-size argument {0}")] InvalidSuffix(String), /// An error getting the columns to display in the output table. + #[error("option --output: field {0} used more than once")] ColumnError(ColumnError), + #[error("{}", .0.iter().map(|t| format!("file system type {} both selected and excluded", t.quote())).collect::>().join(format!("\n{}: ", uucore::util_name()).as_str()))] 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 59d0e5039766cf5bd745efaf94b2df93c6ae0bb4 Mon Sep 17 00:00:00 2001 From: Lewis Boon <4803529+lewisboon@users.noreply.github.com> Date: Sun, 6 Apr 2025 20:43:04 +0100 Subject: [PATCH 2/2] df: migrate OptionsError to thiserror Closes #7536 --- src/uu/df/src/df.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index 218aaa425..71c580a15 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -115,10 +115,16 @@ impl Default for Options { #[derive(Debug, Error)] enum OptionsError { + // TODO This needs to vary based on whether `--block-size` + // or `-B` were provided. #[error("--block-size argument '{0}' too large")] BlockSizeTooLarge(String), + // TODO This needs to vary based on whether `--block-size` + // or `-B` were provided., #[error("invalid --block-size argument {0}")] InvalidBlockSize(String), + // TODO This needs to vary based on whether `--block-size` + // or `-B` were provided. #[error("invalid suffix in --block-size argument {0}")] InvalidSuffix(String), @@ -126,7 +132,10 @@ enum OptionsError { #[error("option --output: field {0} used more than once")] ColumnError(ColumnError), - #[error("{}", .0.iter().map(|t| format!("file system type {} both selected and excluded", t.quote())).collect::>().join(format!("\n{}: ", uucore::util_name()).as_str()))] + #[error("{}", .0.iter() + .map(|t| format!("file system type {} both selected and excluded", t.quote())) + .collect::>() + .join(format!("\n{}: ", uucore::util_name()).as_str()))] FilesystemTypeBothSelectedAndExcluded(Vec), }