From ac4ff2ac0e3056550c292a5e862eb3e1aaa2480a Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Thu, 6 Jul 2023 10:26:29 +0200 Subject: [PATCH] seq: simplify error handling Co-authored-by: Terts Diepraam --- src/uu/seq/src/error.rs | 45 +++++++++++------------------------------ 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/src/uu/seq/src/error.rs b/src/uu/seq/src/error.rs index 4945914d1..fc8452e13 100644 --- a/src/uu/seq/src/error.rs +++ b/src/uu/seq/src/error.rs @@ -2,7 +2,7 @@ // * // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -// spell-checker:ignore numberparse argtype +// spell-checker:ignore numberparse //! Errors returned by seq. use std::error::Error; use std::fmt::Display; @@ -30,29 +30,6 @@ pub enum SeqError { NoArguments, } -impl SeqError { - /// The [`String`] argument as read from the command-line. - fn arg(&self) -> &str { - match self { - Self::ParseError(s, _) => s, - Self::ZeroIncrement(s) => s, - Self::NoArguments => "", - } - } - - /// The type of argument that is causing the error. - fn argtype(&self) -> &str { - match self { - Self::ParseError(_, e) => match e { - ParseNumberError::Float => "invalid floating point argument: ", - ParseNumberError::Nan => "invalid 'not-a-number' argument: ", - ParseNumberError::Hex => "invalid hexadecimal argument: ", - }, - Self::ZeroIncrement(_) => "invalid Zero increment value: ", - Self::NoArguments => "missing operand", - } - } -} impl UError for SeqError { /// Always return 1. fn code(&self) -> i32 { @@ -68,15 +45,17 @@ impl Error for SeqError {} impl Display for SeqError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}{}", - self.argtype(), - if self.arg() == "" { - String::new() - } else { - self.arg().quote().to_string() + match self { + Self::ParseError(s, e) => { + let error_type = match e { + ParseNumberError::Float => "floating point", + ParseNumberError::Nan => "'not-a-number'", + ParseNumberError::Hex => "hexadecimal", + }; + write!(f, "invalid {error_type} argument: {}", s.quote()) } - ) + Self::ZeroIncrement(s) => write!(f, "invalid Zero increment value: {}", s.quote()), + Self::NoArguments => write!(f, "missing operand"), + } } }