From 9de6393e2c919421a5a35354d335d22a87f344ea Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 19 Jan 2025 23:50:16 +0100 Subject: [PATCH] touch: move to thiserror --- Cargo.lock | 3 +++ src/uu/touch/Cargo.toml | 1 + src/uu/touch/src/error.rs | 41 ++++++++++----------------------------- 3 files changed, 14 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 957a797e4..032a41081 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3123,6 +3123,7 @@ dependencies = [ "clap", "num-bigint", "num-traits", + "thiserror 2.0.11", "uucore", ] @@ -3172,6 +3173,7 @@ dependencies = [ "rayon", "self_cell", "tempfile", + "thiserror 2.0.11", "unicode-width 0.2.0", "uucore", ] @@ -3304,6 +3306,7 @@ dependencies = [ "clap", "filetime", "parse_datetime", + "thiserror 2.0.11", "uucore", "windows-sys 0.59.0", ] diff --git a/src/uu/touch/Cargo.toml b/src/uu/touch/Cargo.toml index b076ddfd8..e1e9ecb95 100644 --- a/src/uu/touch/Cargo.toml +++ b/src/uu/touch/Cargo.toml @@ -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] diff --git a/src/uu/touch/src/error.rs b/src/uu/touch/src/error.rs index b39f3faf8..78cc8f330 100644 --- a/src/uu/touch/src/error.rs +++ b/src/uu/touch/src/error.rs @@ -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 {}