mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-06 16:07:47 +00:00
ln: move to thiserror
This commit is contained in:
parent
d0e6a6271c
commit
c1bb57fd1e
2 changed files with 18 additions and 27 deletions
|
@ -19,6 +19,7 @@ path = "src/ln.rs"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { workspace = true }
|
clap = { workspace = true }
|
||||||
uucore = { workspace = true, features = ["backup-control", "fs"] }
|
uucore = { workspace = true, features = ["backup-control", "fs"] }
|
||||||
|
thiserror = { workspace = true }
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "ln"
|
name = "ln"
|
||||||
|
|
|
@ -13,10 +13,9 @@ use uucore::{format_usage, help_about, help_section, help_usage, prompt_yes, sho
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::error::Error;
|
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::fmt::Display;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
#[cfg(any(unix, target_os = "redox"))]
|
#[cfg(any(unix, target_os = "redox"))]
|
||||||
use std::os::unix::fs::symlink;
|
use std::os::unix::fs::symlink;
|
||||||
|
@ -46,38 +45,25 @@ pub enum OverwriteMode {
|
||||||
Force,
|
Force,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Error, Debug)]
|
||||||
enum LnError {
|
enum LnError {
|
||||||
|
#[error("target {} is not a directory", _0.quote())]
|
||||||
TargetIsDirectory(PathBuf),
|
TargetIsDirectory(PathBuf),
|
||||||
|
|
||||||
|
#[error("")]
|
||||||
SomeLinksFailed,
|
SomeLinksFailed,
|
||||||
|
|
||||||
|
#[error("{} and {} are the same file", _0.quote(), _1.quote())]
|
||||||
SameFile(PathBuf, PathBuf),
|
SameFile(PathBuf, PathBuf),
|
||||||
|
|
||||||
|
#[error("missing destination file operand after {}", _0.quote())]
|
||||||
MissingDestination(PathBuf),
|
MissingDestination(PathBuf),
|
||||||
ExtraOperand(OsString),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for LnError {
|
#[error("extra operand {}\nTry '{} --help' for more information.",
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
format!("{:?}", _0).trim_matches('"'), _1)]
|
||||||
match self {
|
ExtraOperand(OsString, String),
|
||||||
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()
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for LnError {}
|
|
||||||
|
|
||||||
impl UError for LnError {
|
impl UError for LnError {
|
||||||
fn code(&self) -> i32 {
|
fn code(&self) -> i32 {
|
||||||
1
|
1
|
||||||
|
@ -284,7 +270,11 @@ fn exec(files: &[PathBuf], settings: &Settings) -> UResult<()> {
|
||||||
return Err(LnError::MissingDestination(files[0].clone()).into());
|
return Err(LnError::MissingDestination(files[0].clone()).into());
|
||||||
}
|
}
|
||||||
if files.len() > 2 {
|
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());
|
assert!(!files.is_empty());
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue