uucore: {USimpleError, UUsageError}::new take Into<String> instead of String

This commit is contained in:
Terts Diepraam 2021-08-10 16:50:42 +02:00
parent 1649217116
commit d12d4f760c
5 changed files with 15 additions and 15 deletions

View file

@ -109,7 +109,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if derefer == 1 { if derefer == 1 {
return Err(USimpleError::new( return Err(USimpleError::new(
1, 1,
"-R --dereference requires -H or -L".to_string(), "-R --dereference requires -H or -L",
)); ));
} }
derefer = 0; derefer = 0;

View file

@ -79,7 +79,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
print!("{}", separator); print!("{}", separator);
} }
} else { } else {
return Err(UUsageError::new(1, "missing operand".to_string())); return Err(UUsageError::new(1, "missing operand"));
} }
Ok(()) Ok(())

View file

@ -173,20 +173,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if (state.nflag || state.rflag) && default_format && !state.cflag { if (state.nflag || state.rflag) && default_format && !state.cflag {
return Err(USimpleError::new( return Err(USimpleError::new(
1, 1,
"cannot print only names or real IDs in default format".to_string(), "cannot print only names or real IDs in default format",
)); ));
} }
if state.zflag && default_format && !state.cflag { if state.zflag && default_format && !state.cflag {
// NOTE: GNU test suite "id/zero.sh" needs this stderr output: // NOTE: GNU test suite "id/zero.sh" needs this stderr output:
return Err(USimpleError::new( return Err(USimpleError::new(
1, 1,
"option --zero not permitted in default format".to_string(), "option --zero not permitted in default format",
)); ));
} }
if state.user_specified && state.cflag { if state.user_specified && state.cflag {
return Err(USimpleError::new( return Err(USimpleError::new(
1, 1,
"cannot print security context when user specified".to_string(), "cannot print security context when user specified",
)); ));
} }
@ -220,7 +220,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
} else { } else {
return Err(USimpleError::new( return Err(USimpleError::new(
1, 1,
"--context (-Z) works only on an SELinux-enabled kernel".to_string(), "--context (-Z) works only on an SELinux-enabled kernel",
)); ));
} }
} }

View file

@ -1238,7 +1238,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if separator.len() != 1 { if separator.len() != 1 {
return Err(UUsageError::new( return Err(UUsageError::new(
2, 2,
"separator must be exactly one character long".into(), "separator must be exactly one character long",
)); ));
} }
settings.separator = Some(separator.chars().next().unwrap()) settings.separator = Some(separator.chars().next().unwrap())
@ -1517,7 +1517,7 @@ fn exec(
file_merger.write_all(settings, output) file_merger.write_all(settings, output)
} else if settings.check { } else if settings.check {
if files.len() > 1 { if files.len() > 1 {
Err(UUsageError::new(2, "only one file allowed with -c".into())) Err(UUsageError::new(2, "only one file allowed with -c"))
} else { } else {
check::check(files.first().unwrap(), settings) check::check(files.first().unwrap(), settings)
} }

View file

@ -268,7 +268,7 @@ where
/// let err = USimpleError { code: 1, message: "error!".into()}; /// let err = USimpleError { code: 1, message: "error!".into()};
/// let res: UResult<()> = Err(err.into()); /// let res: UResult<()> = Err(err.into());
/// // or using the `new` method: /// // or using the `new` method:
/// let res: UResult<()> = Err(USimpleError::new(1, "error!".into())); /// let res: UResult<()> = Err(USimpleError::new(1, "error!"));
/// ``` /// ```
#[derive(Debug)] #[derive(Debug)]
pub struct USimpleError { pub struct USimpleError {
@ -278,8 +278,8 @@ pub struct USimpleError {
impl USimpleError { impl USimpleError {
#[allow(clippy::new_ret_no_self)] #[allow(clippy::new_ret_no_self)]
pub fn new(code: i32, message: String) -> Box<dyn UError> { pub fn new<S: Into<String>>(code: i32, message: S) -> Box<dyn UError> {
Box::new(Self { code, message }) Box::new(Self { code, message: message.into() })
} }
} }
@ -305,8 +305,8 @@ pub struct UUsageError {
impl UUsageError { impl UUsageError {
#[allow(clippy::new_ret_no_self)] #[allow(clippy::new_ret_no_self)]
pub fn new(code: i32, message: String) -> Box<dyn UError> { pub fn new<S: Into<String>>(code: i32, message: S) -> Box<dyn UError> {
Box::new(Self { code, message }) Box::new(Self { code, message: message.into() })
} }
} }
@ -359,9 +359,9 @@ pub struct UIoError {
impl UIoError { impl UIoError {
#[allow(clippy::new_ret_no_self)] #[allow(clippy::new_ret_no_self)]
pub fn new(kind: std::io::ErrorKind, context: String) -> Box<dyn UError> { pub fn new<S: Into<String>>(kind: std::io::ErrorKind, context: S) -> Box<dyn UError> {
Box::new(Self { Box::new(Self {
context, context: context.into(),
inner: std::io::Error::new(kind, ""), inner: std::io::Error::new(kind, ""),
}) })
} }