Skip to content

Commit

Permalink
Merge pull request uutils#7207 from sylvestre/thiserror3
Browse files Browse the repository at this point in the history
Move other programs to thiserror
  • Loading branch information
cakebaker authored Jan 25, 2025
2 parents 0fd9a29 + 3816bff commit dfd5885
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 147 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/uu/du/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
49 changes: 13 additions & 36 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;
Expand Down Expand Up @@ -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,
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}"),
}
}
#[error("Invalid exclude syntax: {0}")]
InvalidGlob(String),
}

impl Error for DuError {}

impl UError for DuError {
fn code(&self) -> i32 {
match self {
Expand Down
1 change: 1 addition & 0 deletions src/uu/groups/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ path = "src/groups.rs"

[dependencies]
clap = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true, features = ["entries", "process"] }

[[bin]]
Expand Down
28 changes: 7 additions & 21 deletions src/uu/groups/src/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//
// ============================================================================
// Test suite summary for GNU coreutils 8.32.162-4eda
// ============================================================================
// PASS: tests/misc/groups-dash.sh
// PASS: tests/misc/groups-process-all.sh
// PASS: tests/misc/groups-version.sh

// spell-checker:ignore (ToDO) passwd

use std::error::Error;
use std::fmt::Display;
use thiserror::Error;
use uucore::{
display::Quotable,
entries::{get_groups_gnu, gid2grp, Locate, Passwd},
Expand All @@ -29,26 +21,20 @@ mod options {
const ABOUT: &str = help_about!("groups.md");
const USAGE: &str = help_usage!("groups.md");

#[derive(Debug)]
#[derive(Debug, Error)]
enum GroupsError {
#[error("failed to fetch groups")]
GetGroupsFailed,

#[error("cannot find name for group ID {0}")]
GroupNotFound(u32),

#[error("{user}: no such user", user = .0.quote())]
UserNotFound(String),
}

impl Error for GroupsError {}
impl UError for GroupsError {}

impl Display for GroupsError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::GetGroupsFailed => write!(f, "failed to fetch groups"),
Self::GroupNotFound(gid) => write!(f, "cannot find name for group ID {gid}"),
Self::UserNotFound(user) => write!(f, "{}: no such user", user.quote()),
}
}
}

fn infallible_gid2grp(gid: &u32) -> String {
match gid2grp(*gid) {
Ok(grp) => grp,
Expand Down
1 change: 1 addition & 0 deletions src/uu/mktemp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ path = "src/mktemp.rs"
clap = { workspace = true }
rand = { workspace = true }
tempfile = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true }

[[bin]]
Expand Down
60 changes: 16 additions & 44 deletions src/uu/mktemp/src/mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use uucore::error::{FromIo, UError, UResult, UUsageError};
use uucore::{format_usage, help_about, help_usage};

use std::env;
use std::error::Error;
use std::ffi::OsStr;
use std::fmt::Display;
use std::io::ErrorKind;
use std::iter;
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
Expand All @@ -25,6 +23,7 @@ use std::os::unix::prelude::PermissionsExt;

use rand::Rng;
use tempfile::Builder;
use thiserror::Error;

const ABOUT: &str = help_about!("mktemp.md");
const USAGE: &str = help_usage!("mktemp.md");
Expand All @@ -46,21 +45,35 @@ const TMPDIR_ENV_VAR: &str = "TMPDIR";
#[cfg(windows)]
const TMPDIR_ENV_VAR: &str = "TMP";

#[derive(Debug)]
#[derive(Debug, Error)]
enum MkTempError {
#[error("could not persist file {path}", path = .0.quote())]
PersistError(PathBuf),

#[error("with --suffix, template {template} must end in X", template = .0.quote())]
MustEndInX(String),

#[error("too few X's in template {template}", template = .0.quote())]
TooFewXs(String),

/// The template prefix contains a path separator (e.g. `"a/bXXX"`).
#[error("invalid template, {template}, contains directory separator", template = .0.quote())]
PrefixContainsDirSeparator(String),

/// The template suffix contains a path separator (e.g. `"XXXa/b"`).
#[error("invalid suffix {suffix}, contains directory separator", suffix = .0.quote())]
SuffixContainsDirSeparator(String),

#[error("invalid template, {template}; with --tmpdir, it may not be absolute", template = .0.quote())]
InvalidTemplate(String),

#[error("too many templates")]
TooManyTemplates,

/// When a specified temporary directory could not be found.
#[error("failed to create {template_type} via template {template}: No such file or directory",
template_type = .0,
template = .1.quote())]
NotFound(String, String),
}

Expand All @@ -70,47 +83,6 @@ impl UError for MkTempError {
}
}

impl Error for MkTempError {}

impl Display for MkTempError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use MkTempError::*;
match self {
PersistError(p) => write!(f, "could not persist file {}", p.quote()),
MustEndInX(s) => write!(f, "with --suffix, template {} must end in X", s.quote()),
TooFewXs(s) => write!(f, "too few X's in template {}", s.quote()),
PrefixContainsDirSeparator(s) => {
write!(
f,
"invalid template, {}, contains directory separator",
s.quote()
)
}
SuffixContainsDirSeparator(s) => {
write!(
f,
"invalid suffix {}, contains directory separator",
s.quote()
)
}
InvalidTemplate(s) => write!(
f,
"invalid template, {}; with --tmpdir, it may not be absolute",
s.quote()
),
TooManyTemplates => {
write!(f, "too many templates")
}
NotFound(template_type, s) => write!(
f,
"failed to create {} via template {}: No such file or directory",
template_type,
s.quote()
),
}
}
}

/// Options parsed from the command-line.
///
/// This provides a layer of indirection between the application logic
Expand Down
1 change: 1 addition & 0 deletions src/uu/nohup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ path = "src/nohup.rs"
clap = { workspace = true }
libc = { workspace = true }
uucore = { workspace = true, features = ["fs"] }
thiserror = { workspace = true }

[[bin]]
name = "nohup"
Expand Down
43 changes: 16 additions & 27 deletions src/uu/nohup/src/nohup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use libc::{c_char, dup2, execvp, signal};
use libc::{SIGHUP, SIG_IGN};
use std::env;
use std::ffi::CString;
use std::fmt::{Display, Formatter};
use std::fs::{File, OpenOptions};
use std::io::{Error, IsTerminal};
use std::os::unix::prelude::*;
use std::path::{Path, PathBuf};
use thiserror::Error;
use uucore::display::Quotable;
use uucore::error::{set_exit_code, UClapError, UError, UResult};
use uucore::{format_usage, help_about, help_section, help_usage, show_error};
Expand All @@ -33,15 +33,24 @@ mod options {
pub const CMD: &str = "cmd";
}

#[derive(Debug)]
#[derive(Debug, Error)]
enum NohupError {
#[error("Cannot detach from console")]
CannotDetach,
CannotReplace(&'static str, std::io::Error),
OpenFailed(i32, std::io::Error),
OpenFailed2(i32, std::io::Error, String, std::io::Error),
}

impl std::error::Error for NohupError {}
#[error("Cannot replace {name}: {err}", name = .0, err = .1)]
CannotReplace(&'static str, #[source] Error),

#[error("failed to open {path}: {err}", path = NOHUP_OUT.quote(), err = .1)]
OpenFailed(i32, #[source] Error),

#[error("failed to open {first_path}: {first_err}\nfailed to open {second_path}: {second_err}",
first_path = NOHUP_OUT.quote(),
first_err = .1,
second_path = .2.quote(),
second_err = .3)]
OpenFailed2(i32, #[source] Error, String, Error),
}

impl UError for NohupError {
fn code(&self) -> i32 {
Expand All @@ -52,26 +61,6 @@ impl UError for NohupError {
}
}

impl Display for NohupError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match self {
Self::CannotDetach => write!(f, "Cannot detach from console"),
Self::CannotReplace(s, e) => write!(f, "Cannot replace {s}: {e}"),
Self::OpenFailed(_, e) => {
write!(f, "failed to open {}: {}", NOHUP_OUT.quote(), e)
}
Self::OpenFailed2(_, e1, s, e2) => write!(
f,
"failed to open {}: {}\nfailed to open {}: {}",
NOHUP_OUT.quote(),
e1,
s.quote(),
e2
),
}
}
}

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args).with_exit_code(125)?;
Expand Down
1 change: 1 addition & 0 deletions src/uu/tsort/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ path = "src/tsort.rs"

[dependencies]
clap = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true }

[[bin]]
Expand Down
Loading

0 comments on commit dfd5885

Please sign in to comment.