1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 20:17:45 +00:00

touch: move to thiserror

This commit is contained in:
Sylvestre Ledru 2025-01-19 23:50:16 +01:00
parent 70f3429102
commit 9de6393e2c
3 changed files with 14 additions and 31 deletions

3
Cargo.lock generated
View file

@ -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",
]

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 {}