Skip to content

Commit 3a0b43b

Browse files
authored
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
1 parent dd5517c commit 3a0b43b

File tree

4 files changed

+9
-13
lines changed

4 files changed

+9
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/df/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ path = "src/df.rs"
2020
clap = { workspace = true }
2121
uucore = { workspace = true, features = ["libc", "fsext"] }
2222
unicode-width = { workspace = true }
23+
thiserror = { workspace = true }
2324

2425
[dev-dependencies]
2526
tempfile = { workspace = true }

src/uu/df/src/columns.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// spell-checker:ignore itotal iused iavail ipcent pcent squashfs
66
use crate::{OPT_INODES, OPT_OUTPUT, OPT_PRINT_TYPE};
77
use clap::{ArgMatches, parser::ValueSource};
8+
use thiserror::Error;
9+
use uucore::display::Quotable;
810

911
/// The columns in the output table produced by `df`.
1012
///
@@ -56,9 +58,10 @@ pub(crate) enum Column {
5658
}
5759

5860
/// An error while defining which columns to display in the output table.
59-
#[derive(Debug)]
61+
#[derive(Debug, Error)]
6062
pub(crate) enum ColumnError {
6163
/// If a column appears more than once in the `--output` argument.
64+
#[error("{}", .0.quote())]
6265
MultipleColumns(String),
6366
}
6467

src/uu/df/src/df.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ use uucore::{format_usage, help_about, help_section, help_usage, show};
1919

2020
use clap::{Arg, ArgAction, ArgMatches, Command, parser::ValueSource};
2121

22-
use std::error::Error;
2322
use std::ffi::OsString;
2423
use std::fmt;
2524
use std::path::Path;
25+
use thiserror::Error;
2626

2727
use crate::blocks::{BlockSize, read_block_size};
2828
use crate::columns::{Column, ColumnError};
@@ -426,28 +426,19 @@ where
426426
Ok(result)
427427
}
428428

429-
#[derive(Debug)]
429+
#[derive(Debug, Error)]
430430
enum DfError {
431431
/// A problem while parsing command-line options.
432+
#[error("{}", .0)]
432433
OptionsError(OptionsError),
433434
}
434435

435-
impl Error for DfError {}
436-
437436
impl UError for DfError {
438437
fn usage(&self) -> bool {
439438
matches!(self, Self::OptionsError(OptionsError::ColumnError(_)))
440439
}
441440
}
442441

443-
impl fmt::Display for DfError {
444-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
445-
match self {
446-
Self::OptionsError(e) => e.fmt(f),
447-
}
448-
}
449-
}
450-
451442
#[uucore::main]
452443
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
453444
let matches = uu_app().try_get_matches_from(args)?;

0 commit comments

Comments
 (0)