1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 11:07:44 +00:00

Merge pull request #7175 from sylvestre/thiserror2

Move some programs to thiserror
This commit is contained in:
Daniel Hofstetter 2025-01-20 16:17:25 +01:00 committed by GitHub
commit 6a5f2aa334
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 161 additions and 249 deletions

92
Cargo.lock generated
View file

@ -88,11 +88,12 @@ dependencies = [
[[package]]
name = "anstyle-wincon"
version = "3.0.6"
version = "3.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125"
checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e"
dependencies = [
"anstyle",
"once_cell",
"windows-sys 0.59.0",
]
@ -160,7 +161,7 @@ version = "0.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f49d8fed880d473ea71efb9bf597651e77201bdd4893efe54c9e5d65ae04ce6f"
dependencies = [
"bitflags 2.7.0",
"bitflags 2.8.0",
"cexpr",
"clang-sys",
"itertools 0.13.0",
@ -182,9 +183,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.7.0"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1be3f42a67d6d345ecd59f675f3f012d6974981560836e938c22b424b85ce1be"
checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"
[[package]]
name = "bitvec"
@ -262,9 +263,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cc"
version = "1.2.8"
version = "1.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad0cf6e91fde44c773c6ee7ec6bba798504641a8bc2eb7e37a04ffbf4dfaa55a"
checksum = "13208fcbb66eaeffe09b99fffbe1af420f00a7b35aa99ad683dfc1aa76145229"
dependencies = [
"shlex",
]
@ -689,7 +690,7 @@ version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6"
dependencies = [
"bitflags 2.7.0",
"bitflags 2.8.0",
"crossterm_winapi",
"filedescriptor",
"mio",
@ -869,7 +870,7 @@ version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22be12de19decddab85d09f251ec8363f060ccb22ec9c81bc157c0c8433946d8"
dependencies = [
"bitflags 2.7.0",
"bitflags 2.8.0",
"log",
"scopeguard",
"uuid",
@ -1162,7 +1163,7 @@ version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f37dccff2791ab604f9babef0ba14fbe0be30bd368dc541e2b08d07c8aa908f3"
dependencies = [
"bitflags 2.7.0",
"bitflags 2.8.0",
"inotify-sys",
"libc",
]
@ -1219,9 +1220,9 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
[[package]]
name = "js-sys"
version = "0.3.76"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7"
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
dependencies = [
"once_cell",
"wasm-bindgen",
@ -1290,7 +1291,7 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d"
dependencies = [
"bitflags 2.7.0",
"bitflags 2.8.0",
"libc",
"redox_syscall",
]
@ -1325,9 +1326,9 @@ checksum = "9374ef4228402d4b7e403e5838cb880d9ee663314b0a900d5a6aabf0c213552e"
[[package]]
name = "log"
version = "0.4.22"
version = "0.4.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f"
[[package]]
name = "lru"
@ -1381,9 +1382,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "miniz_oxide"
version = "0.8.2"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ffbe83022cedc1d264172192511ae958937694cd57ce297164951b8b3568394"
checksum = "b8402cab7aefae129c6977bb0ff1b8fd9a04eb5b51efc50a70bea51cda0c7924"
dependencies = [
"adler2",
]
@ -1406,7 +1407,7 @@ version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [
"bitflags 2.7.0",
"bitflags 2.8.0",
"cfg-if",
"cfg_aliases",
"libc",
@ -1428,7 +1429,7 @@ version = "8.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fee8403b3d66ac7b26aee6e40a897d85dc5ce26f44da36b8b73e987cc52e943"
dependencies = [
"bitflags 2.7.0",
"bitflags 2.8.0",
"filetime",
"fsevent-sys",
"inotify",
@ -1722,9 +1723,9 @@ dependencies = [
[[package]]
name = "prettyplease"
version = "0.2.27"
version = "0.2.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "483f8c21f64f3ea09fe0f30f5d48c3e8eefe5dac9129f0075f76593b4c1da705"
checksum = "6924ced06e1f7dfe3fa48d57b9f74f55d8915f5036121bef647ef4b204895fac"
dependencies = [
"proc-macro2",
"syn",
@ -1754,7 +1755,7 @@ version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc5b72d8145275d844d4b5f6d4e1eef00c8cd889edb6035c21675d1bb1f45c9f"
dependencies = [
"bitflags 2.7.0",
"bitflags 2.8.0",
"hex",
"procfs-core",
"rustix 0.38.43",
@ -1766,7 +1767,7 @@ version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "239df02d8349b06fc07398a3a1697b06418223b1c7725085e801e7c0fc6a12ec"
dependencies = [
"bitflags 2.7.0",
"bitflags 2.8.0",
"hex",
]
@ -1856,7 +1857,7 @@ version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834"
dependencies = [
"bitflags 2.7.0",
"bitflags 2.8.0",
]
[[package]]
@ -1991,13 +1992,19 @@ version = "0.38.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a78891ee6bf2340288408954ac787aa063d8e8817e9f53abb37c695c6d834ef6"
dependencies = [
"bitflags 2.7.0",
"bitflags 2.8.0",
"errno",
"libc",
"linux-raw-sys 0.4.15",
"windows-sys 0.59.0",
]
[[package]]
name = "rustversion"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4"
[[package]]
name = "same-file"
version = "1.0.6"
@ -2025,7 +2032,7 @@ version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0139b2436c81305eb6bda33af151851f75bd62783817b25f44daa371119c30b5"
dependencies = [
"bitflags 2.7.0",
"bitflags 2.8.0",
"libc",
"once_cell",
"reference-counted-singleton",
@ -2547,6 +2554,7 @@ name = "uu_chroot"
version = "0.0.29"
dependencies = [
"clap",
"thiserror 2.0.11",
"uucore",
]
@ -2940,6 +2948,7 @@ dependencies = [
"clap",
"fs_extra",
"indicatif",
"thiserror 2.0.11",
"uucore",
]
@ -3123,6 +3132,7 @@ dependencies = [
"clap",
"num-bigint",
"num-traits",
"thiserror 2.0.11",
"uucore",
]
@ -3172,6 +3182,7 @@ dependencies = [
"rayon",
"self_cell",
"tempfile",
"thiserror 2.0.11",
"unicode-width 0.2.0",
"uucore",
]
@ -3304,6 +3315,7 @@ dependencies = [
"clap",
"filetime",
"parse_datetime",
"thiserror 2.0.11",
"uucore",
"windows-sys 0.59.0",
]
@ -3510,9 +3522,9 @@ version = "0.0.29"
[[package]]
name = "uuid"
version = "1.11.1"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b913a3b5fe84142e269d63cc62b64319ccaf89b748fc31fe025177f767a756c4"
checksum = "744018581f9a3454a9e15beb8a33b017183f1e7c0cd170232a2d1453b23a51c4"
[[package]]
name = "uutils_term_grid"
@ -3547,20 +3559,21 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.99"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396"
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.99"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79"
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
dependencies = [
"bumpalo",
"log",
@ -3572,9 +3585,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.99"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe"
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@ -3582,9 +3595,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.99"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2"
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
dependencies = [
"proc-macro2",
"quote",
@ -3595,9 +3608,12 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.99"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6"
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
dependencies = [
"unicode-ident",
]
[[package]]
name = "web-time"

View file

@ -18,6 +18,7 @@ path = "src/chroot.rs"
[dependencies]
clap = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true, features = ["entries", "fs"] }
[[bin]]

View file

@ -4,59 +4,75 @@
// file that was distributed with this source code.
// spell-checker:ignore NEWROOT Userspec userspec
//! Errors returned by chroot.
use std::fmt::Display;
use std::io::Error;
use thiserror::Error;
use uucore::display::Quotable;
use uucore::error::UError;
use uucore::libc;
/// Errors that can happen while executing chroot.
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum ChrootError {
/// Failed to enter the specified directory.
CannotEnter(String, Error),
#[error("cannot chroot to {dir}: {err}", dir = .0.quote(), err = .1)]
CannotEnter(String, #[source] Error),
/// Failed to execute the specified command.
CommandFailed(String, Error),
#[error("failed to run command {cmd}: {err}", cmd = .0.to_string().quote(), err = .1)]
CommandFailed(String, #[source] Error),
/// Failed to find the specified command.
CommandNotFound(String, Error),
#[error("failed to run command {cmd}: {err}", cmd = .0.to_string().quote(), err = .1)]
CommandNotFound(String, #[source] Error),
#[error("--groups parsing failed")]
GroupsParsingFailed,
#[error("invalid group: {group}", group = .0.quote())]
InvalidGroup(String),
#[error("invalid group list: {list}", list = .0.quote())]
InvalidGroupList(String),
/// The given user and group specification was invalid.
#[error("invalid userspec: {spec}", spec = .0.quote())]
InvalidUserspec(String),
/// The new root directory was not given.
#[error(
"Missing operand: NEWROOT\nTry '{0} --help' for more information.",
uucore::execution_phrase()
)]
MissingNewRoot,
#[error("no group specified for unknown uid: {0}")]
NoGroupSpecified(libc::uid_t),
/// Failed to find the specified user.
#[error("invalid user")]
NoSuchUser,
/// Failed to find the specified group.
#[error("invalid group")]
NoSuchGroup,
/// The given directory does not exist.
#[error("cannot change root directory to {dir}: no such directory", dir = .0.quote())]
NoSuchDirectory(String),
/// The call to `setgid()` failed.
SetGidFailed(String, Error),
#[error("cannot set gid to {gid}: {err}", gid = .0, err = .1)]
SetGidFailed(String, #[source] Error),
/// The call to `setgroups()` failed.
#[error("cannot set groups: {0}")]
SetGroupsFailed(Error),
/// The call to `setuid()` failed.
SetUserFailed(String, Error),
#[error("cannot set user to {user}: {err}", user = .0.maybe_quote(), err = .1)]
SetUserFailed(String, #[source] Error),
}
impl std::error::Error for ChrootError {}
impl UError for ChrootError {
// 125 if chroot itself fails
// 126 if command is found but cannot be invoked
@ -69,36 +85,3 @@ impl UError for ChrootError {
}
}
}
impl Display for ChrootError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::CannotEnter(s, e) => write!(f, "cannot chroot to {}: {}", s.quote(), e,),
Self::CommandFailed(s, e) | Self::CommandNotFound(s, e) => {
write!(f, "failed to run command {}: {}", s.to_string().quote(), e,)
}
Self::GroupsParsingFailed => write!(f, "--groups parsing failed"),
Self::InvalidGroup(s) => write!(f, "invalid group: {}", s.quote()),
Self::InvalidGroupList(s) => write!(f, "invalid group list: {}", s.quote()),
Self::InvalidUserspec(s) => write!(f, "invalid userspec: {}", s.quote(),),
Self::MissingNewRoot => write!(
f,
"Missing operand: NEWROOT\nTry '{} --help' for more information.",
uucore::execution_phrase(),
),
Self::NoGroupSpecified(uid) => write!(f, "no group specified for unknown uid: {}", uid),
Self::NoSuchUser => write!(f, "invalid user"),
Self::NoSuchGroup => write!(f, "invalid group"),
Self::NoSuchDirectory(s) => write!(
f,
"cannot change root directory to {}: no such directory",
s.quote(),
),
Self::SetGidFailed(s, e) => write!(f, "cannot set gid to {s}: {e}"),
Self::SetGroupsFailed(e) => write!(f, "cannot set groups: {e}"),
Self::SetUserFailed(s, e) => {
write!(f, "cannot set user to {}: {}", s.maybe_quote(), e)
}
}
}
}

View file

@ -26,6 +26,7 @@ uucore = { workspace = true, features = [
"fsxattr",
"update-control",
] }
thiserror = { workspace = true }
[[bin]]
name = "mv"

View file

@ -2,47 +2,37 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use std::error::Error;
use std::fmt::{Display, Formatter, Result};
use thiserror::Error;
use uucore::error::UError;
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum MvError {
#[error("cannot stat {0}: No such file or directory")]
NoSuchFile(String),
#[error("cannot stat {0}: Not a directory")]
CannotStatNotADirectory(String),
#[error("{0} and {1} are the same file")]
SameFile(String, String),
#[error("cannot move {0} to a subdirectory of itself, {1}")]
SelfTargetSubdirectory(String, String),
#[error("cannot overwrite directory {0} with non-directory")]
DirectoryToNonDirectory(String),
#[error("cannot overwrite non-directory {1} with directory {0}")]
NonDirectoryToDirectory(String, String),
#[error("target {0}: Not a directory")]
NotADirectory(String),
#[error("target directory {0}: Not a directory")]
TargetNotADirectory(String),
#[error("failed to access {0}: Not a directory")]
FailedToAccessNotADirectory(String),
}
impl Error for MvError {}
impl UError for MvError {}
impl Display for MvError {
fn fmt(&self, f: &mut Formatter) -> Result {
match self {
Self::NoSuchFile(s) => write!(f, "cannot stat {s}: No such file or directory"),
Self::CannotStatNotADirectory(s) => write!(f, "cannot stat {s}: Not a directory"),
Self::SameFile(s, t) => write!(f, "{s} and {t} are the same file"),
Self::SelfTargetSubdirectory(s, t) => {
write!(f, "cannot move {s} to a subdirectory of itself, {t}")
}
Self::DirectoryToNonDirectory(t) => {
write!(f, "cannot overwrite directory {t} with non-directory")
}
Self::NonDirectoryToDirectory(s, t) => {
write!(f, "cannot overwrite non-directory {t} with directory {s}")
}
Self::NotADirectory(t) => write!(f, "target {t}: Not a directory"),
Self::TargetNotADirectory(t) => write!(f, "target directory {t}: Not a directory"),
Self::FailedToAccessNotADirectory(t) => {
write!(f, "failed to access {t}: Not a directory")
}
}
}
}

View file

@ -22,6 +22,7 @@ bigdecimal = { workspace = true }
clap = { workspace = true }
num-bigint = { workspace = true }
num-traits = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true, features = ["format", "quoting-style"] }
[[bin]]

View file

@ -4,32 +4,40 @@
// file that was distributed with this source code.
// spell-checker:ignore numberparse
//! Errors returned by seq.
use std::error::Error;
use std::fmt::Display;
use crate::numberparse::ParseNumberError;
use thiserror::Error;
use uucore::display::Quotable;
use uucore::error::UError;
use crate::numberparse::ParseNumberError;
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum SeqError {
/// An error parsing the input arguments.
///
/// The parameters are the [`String`] argument as read from the
/// command line and the underlying parsing error itself.
#[error("invalid {} argument: {}", parse_error_type(.1), .0.quote())]
ParseError(String, ParseNumberError),
/// The increment argument was zero, which is not allowed.
///
/// The parameter is the increment argument as a [`String`] as read
/// from the command line.
#[error("invalid Zero increment value: {}", .0.quote())]
ZeroIncrement(String),
/// No arguments were passed to this function, 1 or more is required
#[error("missing operand")]
NoArguments,
}
fn parse_error_type(e: &ParseNumberError) -> &'static str {
match e {
ParseNumberError::Float => "floating point",
ParseNumberError::Nan => "'not-a-number'",
ParseNumberError::Hex => "hexadecimal",
}
}
impl UError for SeqError {
/// Always return 1.
fn code(&self) -> i32 {
@ -40,22 +48,3 @@ impl UError for SeqError {
true
}
}
impl Error for SeqError {}
impl Display for SeqError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ParseError(s, e) => {
let error_type = match e {
ParseNumberError::Float => "floating point",
ParseNumberError::Nan => "'not-a-number'",
ParseNumberError::Hex => "hexadecimal",
};
write!(f, "invalid {error_type} argument: {}", s.quote())
}
Self::ZeroIncrement(s) => write!(f, "invalid Zero increment value: {}", s.quote()),
Self::NoArguments => write!(f, "missing operand"),
}
}
}

View file

@ -28,6 +28,7 @@ rand = { workspace = true }
rayon = { workspace = true }
self_cell = { workspace = true }
tempfile = { workspace = true }
thiserror = { workspace = true }
unicode-width = { workspace = true }
uucore = { workspace = true, features = ["fs", "version-cmp"] }

View file

@ -30,9 +30,7 @@ use rand::{thread_rng, Rng};
use rayon::prelude::*;
use std::cmp::Ordering;
use std::env;
use std::error::Error;
use std::ffi::{OsStr, OsString};
use std::fmt::Display;
use std::fs::{File, OpenOptions};
use std::hash::{Hash, Hasher};
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write};
@ -40,9 +38,11 @@ use std::ops::Range;
use std::path::Path;
use std::path::PathBuf;
use std::str::Utf8Error;
use thiserror::Error;
use unicode_width::UnicodeWidthStr;
use uucore::display::Quotable;
use uucore::error::{set_exit_code, strip_errno, UError, UResult, USimpleError, UUsageError};
use uucore::error::strip_errno;
use uucore::error::{set_exit_code, UError, UResult, USimpleError, UUsageError};
use uucore::line_ending::LineEnding;
use uucore::parse_size::{ParseSizeError, Parser};
use uucore::shortcut_value_parser::ShortcutValueParser;
@ -119,44 +119,43 @@ const POSITIVE: char = '+';
// available memory into consideration, instead of relying on this constant only.
const DEFAULT_BUF_SIZE: usize = 1_000_000_000; // 1 GB
#[derive(Debug)]
enum SortError {
#[derive(Debug, Error)]
pub enum SortError {
#[error("{}", format_disorder(.file, .line_number, .line, .silent))]
Disorder {
file: OsString,
line_number: usize,
line: String,
silent: bool,
},
OpenFailed {
path: String,
error: std::io::Error,
},
#[error("open failed: {}: {}", .path.maybe_quote(), strip_errno(.error))]
OpenFailed { path: String, error: std::io::Error },
#[error("failed to parse key {}: {}", .key.quote(), .msg)]
ParseKeyError { key: String, msg: String },
#[error("cannot read: {}: {}", .path.maybe_quote(), strip_errno(.error))]
ReadFailed {
path: PathBuf,
error: std::io::Error,
},
ParseKeyError {
key: String,
msg: String,
},
OpenTmpFileFailed {
error: std::io::Error,
},
CompressProgExecutionFailed {
code: i32,
},
CompressProgTerminatedAbnormally {
prog: String,
},
TmpFileCreationFailed {
path: PathBuf,
},
Uft8Error {
error: Utf8Error,
},
}
impl Error for SortError {}
#[error("failed to open temporary file: {}", strip_errno(.error))]
OpenTmpFileFailed { error: std::io::Error },
#[error("couldn't execute compress program: errno {code}")]
CompressProgExecutionFailed { code: i32 },
#[error("{} terminated abnormally", .prog.quote())]
CompressProgTerminatedAbnormally { prog: String },
#[error("cannot create temporary file in '{}':", .path.display())]
TmpFileCreationFailed { path: PathBuf },
#[error("{error}")]
Uft8Error { error: Utf8Error },
}
impl UError for SortError {
fn code(&self) -> i32 {
@ -167,60 +166,11 @@ impl UError for SortError {
}
}
impl Display for SortError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Disorder {
file,
line_number,
line,
silent,
} => {
if *silent {
Ok(())
} else {
write!(
f,
"{}:{}: disorder: {}",
file.maybe_quote(),
line_number,
line
)
}
}
Self::OpenFailed { path, error } => {
write!(
f,
"open failed: {}: {}",
path.maybe_quote(),
strip_errno(error)
)
}
Self::ParseKeyError { key, msg } => {
write!(f, "failed to parse key {}: {}", key.quote(), msg)
}
Self::ReadFailed { path, error } => {
write!(
f,
"cannot read: {}: {}",
path.maybe_quote(),
strip_errno(error)
)
}
Self::OpenTmpFileFailed { error } => {
write!(f, "failed to open temporary file: {}", strip_errno(error))
}
Self::CompressProgExecutionFailed { code } => {
write!(f, "couldn't execute compress program: errno {code}")
}
Self::CompressProgTerminatedAbnormally { prog } => {
write!(f, "{} terminated abnormally", prog.quote())
}
Self::TmpFileCreationFailed { path } => {
write!(f, "cannot create temporary file in '{}':", path.display())
}
Self::Uft8Error { error } => write!(f, "{error}"),
}
fn format_disorder(file: &OsString, line_number: &usize, line: &String, silent: &bool) -> String {
if *silent {
String::new()
} else {
format!("{}:{}: disorder: {}", file.maybe_quote(), line_number, line)
}
}

View file

@ -22,6 +22,7 @@ filetime = { workspace = true }
clap = { workspace = true }
chrono = { workspace = true }
parse_datetime = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true, features = ["libc"] }
[target.'cfg(target_os = "windows")'.dependencies]

View file

@ -4,29 +4,31 @@
// file that was distributed with this source code.
// spell-checker:ignore (misc) uioerror
use std::error::Error;
use std::fmt::{Display, Formatter, Result};
use std::path::PathBuf;
use filetime::FileTime;
use std::path::PathBuf;
use thiserror::Error;
use uucore::display::Quotable;
use uucore::error::{UError, UIoError};
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum TouchError {
#[error("Unable to parse date: {0}")]
InvalidDateFormat(String),
/// The source time couldn't be converted to a [chrono::DateTime]
#[error("Source has invalid access or modification time: {0}")]
InvalidFiletime(FileTime),
/// The reference file's attributes could not be found or read
#[error("failed to get attributes of {}: {}", .0.quote(), to_uioerror(.1))]
ReferenceFileInaccessible(PathBuf, std::io::Error),
/// An error getting a path to stdout on Windows
#[error("GetFinalPathNameByHandleW failed with code {0}")]
WindowsStdoutPathError(String),
/// An error encountered on a specific file
#[error("{error}")]
TouchFileError {
path: PathBuf,
index: usize,
@ -34,31 +36,6 @@ pub enum TouchError {
},
}
impl Error for TouchError {}
impl UError for TouchError {}
impl Display for TouchError {
fn fmt(&self, f: &mut Formatter) -> Result {
match self {
Self::InvalidDateFormat(s) => write!(f, "Unable to parse date: {s}"),
Self::InvalidFiletime(time) => {
write!(f, "Source has invalid access or modification time: {time}",)
}
Self::ReferenceFileInaccessible(path, err) => {
write!(
f,
"failed to get attributes of {}: {}",
path.quote(),
to_uioerror(err)
)
}
Self::WindowsStdoutPathError(code) => {
write!(f, "GetFinalPathNameByHandleW failed with code {code}")
}
Self::TouchFileError { error, .. } => write!(f, "{error}"),
}
}
}
fn to_uioerror(err: &std::io::Error) -> UIoError {
let copy = if let Some(code) = err.raw_os_error() {
std::io::Error::from_raw_os_error(code)
@ -67,3 +44,5 @@ fn to_uioerror(err: &std::io::Error) -> UIoError {
};
UIoError::from(copy)
}
impl UError for TouchError {}