From 6866eedc83a87f08326045782baf2618e280b1d6 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 19 Jan 2025 23:35:54 +0100 Subject: [PATCH] mv: move to thiserror --- Cargo.lock | 1 + src/uu/mv/Cargo.toml | 1 + src/uu/mv/src/error.rs | 48 +++++++++++++++++------------------------- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 032a41081..23405f8ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2940,6 +2940,7 @@ dependencies = [ "clap", "fs_extra", "indicatif", + "thiserror 2.0.11", "uucore", ] diff --git a/src/uu/mv/Cargo.toml b/src/uu/mv/Cargo.toml index 4982edd80..13b40f7fb 100644 --- a/src/uu/mv/Cargo.toml +++ b/src/uu/mv/Cargo.toml @@ -26,6 +26,7 @@ uucore = { workspace = true, features = [ "fsxattr", "update-control", ] } +thiserror = { workspace = true } [[bin]] name = "mv" diff --git a/src/uu/mv/src/error.rs b/src/uu/mv/src/error.rs index 6daa8188e..5049725a6 100644 --- a/src/uu/mv/src/error.rs +++ b/src/uu/mv/src/error.rs @@ -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") - } - } - } -}