From 3a0b43bdf7f2e91b192c6ee4c8f13cf95843cf7e Mon Sep 17 00:00:00 2001 From: Chandra Kiran G <121796315+kiran-4444@users.noreply.github.com> Date: Wed, 2 Apr 2025 13:49:19 +0530 Subject: [PATCH] df: add thiserror (#7545) * refactor: Add thiserror to df * fix: Try fixing tests * refactor(df): Move `df` to `thiserror` * chore(df): Add back comment * chore: Refactor column.rs correctly --- Cargo.lock | 1 + src/uu/df/Cargo.toml | 1 + src/uu/df/src/columns.rs | 5 ++++- src/uu/df/src/df.rs | 15 +++------------ 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6cad3aadb..1cb562c3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2672,6 +2672,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 d82ec6c83..8e2560854 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/columns.rs b/src/uu/df/src/columns.rs index 0a9a20b85..0d2d121a3 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::{ArgMatches, parser::ValueSource}; +use thiserror::Error; +use uucore::display::Quotable; /// The columns in the output table produced by `df`. /// @@ -56,9 +58,10 @@ 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 a73cc21ef..04bb99a49 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -19,10 +19,10 @@ use uucore::{format_usage, help_about, help_section, help_usage, show}; use clap::{Arg, ArgAction, ArgMatches, Command, parser::ValueSource}; -use std::error::Error; use std::ffi::OsString; use std::fmt; use std::path::Path; +use thiserror::Error; use crate::blocks::{BlockSize, read_block_size}; use crate::columns::{Column, ColumnError}; @@ -426,28 +426,19 @@ where Ok(result) } -#[derive(Debug)] +#[derive(Debug, Error)] enum DfError { /// A problem while parsing command-line options. + #[error("{}", .0)] 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)?;