From c1bb57fd1e0363996363f548bb86674a5e31c8b8 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Wed, 19 Mar 2025 23:03:50 +0100 Subject: [PATCH] ln: move to thiserror --- src/uu/ln/Cargo.toml | 1 + src/uu/ln/src/ln.rs | 44 +++++++++++++++++--------------------------- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/uu/ln/Cargo.toml b/src/uu/ln/Cargo.toml index 5038f4564..2101a6605 100644 --- a/src/uu/ln/Cargo.toml +++ b/src/uu/ln/Cargo.toml @@ -19,6 +19,7 @@ path = "src/ln.rs" [dependencies] clap = { workspace = true } uucore = { workspace = true, features = ["backup-control", "fs"] } +thiserror = { workspace = true } [[bin]] name = "ln" diff --git a/src/uu/ln/src/ln.rs b/src/uu/ln/src/ln.rs index fef1b1e0b..2529fdbf7 100644 --- a/src/uu/ln/src/ln.rs +++ b/src/uu/ln/src/ln.rs @@ -13,10 +13,9 @@ use uucore::{format_usage, help_about, help_section, help_usage, prompt_yes, sho use std::borrow::Cow; use std::collections::HashSet; -use std::error::Error; use std::ffi::OsString; -use std::fmt::Display; use std::fs; +use thiserror::Error; #[cfg(any(unix, target_os = "redox"))] use std::os::unix::fs::symlink; @@ -46,38 +45,25 @@ pub enum OverwriteMode { Force, } -#[derive(Debug)] +#[derive(Error, Debug)] enum LnError { + #[error("target {} is not a directory", _0.quote())] TargetIsDirectory(PathBuf), + + #[error("")] SomeLinksFailed, + + #[error("{} and {} are the same file", _0.quote(), _1.quote())] SameFile(PathBuf, PathBuf), + + #[error("missing destination file operand after {}", _0.quote())] MissingDestination(PathBuf), - ExtraOperand(OsString), -} -impl Display for LnError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::TargetIsDirectory(s) => write!(f, "target {} is not a directory", s.quote()), - Self::SameFile(s, d) => { - write!(f, "{} and {} are the same file", s.quote(), d.quote()) - } - Self::SomeLinksFailed => Ok(()), - Self::MissingDestination(s) => { - write!(f, "missing destination file operand after {}", s.quote()) - } - Self::ExtraOperand(s) => write!( - f, - "extra operand {}\nTry '{} --help' for more information.", - s.quote(), - uucore::execution_phrase() - ), - } - } + #[error("extra operand {}\nTry '{} --help' for more information.", + format!("{:?}", _0).trim_matches('"'), _1)] + ExtraOperand(OsString, String), } -impl Error for LnError {} - impl UError for LnError { fn code(&self) -> i32 { 1 @@ -284,7 +270,11 @@ fn exec(files: &[PathBuf], settings: &Settings) -> UResult<()> { return Err(LnError::MissingDestination(files[0].clone()).into()); } if files.len() > 2 { - return Err(LnError::ExtraOperand(files[2].clone().into()).into()); + return Err(LnError::ExtraOperand( + files[2].clone().into(), + uucore::execution_phrase().to_string(), + ) + .into()); } assert!(!files.is_empty());