mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
seq: simplify error handling
Co-authored-by: Terts Diepraam <terts.diepraam@gmail.com>
This commit is contained in:
parent
98264e9cdf
commit
ac4ff2ac0e
1 changed files with 12 additions and 33 deletions
|
@ -2,7 +2,7 @@
|
||||||
// *
|
// *
|
||||||
// * For the full copyright and license information, please view the LICENSE
|
// * For the full copyright and license information, please view the LICENSE
|
||||||
// * file that was distributed with this source code.
|
// * file that was distributed with this source code.
|
||||||
// spell-checker:ignore numberparse argtype
|
// spell-checker:ignore numberparse
|
||||||
//! Errors returned by seq.
|
//! Errors returned by seq.
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
@ -30,29 +30,6 @@ pub enum SeqError {
|
||||||
NoArguments,
|
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 {
|
impl UError for SeqError {
|
||||||
/// Always return 1.
|
/// Always return 1.
|
||||||
fn code(&self) -> i32 {
|
fn code(&self) -> i32 {
|
||||||
|
@ -68,15 +45,17 @@ impl Error for SeqError {}
|
||||||
|
|
||||||
impl Display for SeqError {
|
impl Display for SeqError {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(
|
match self {
|
||||||
f,
|
Self::ParseError(s, e) => {
|
||||||
"{}{}",
|
let error_type = match e {
|
||||||
self.argtype(),
|
ParseNumberError::Float => "floating point",
|
||||||
if self.arg() == "" {
|
ParseNumberError::Nan => "'not-a-number'",
|
||||||
String::new()
|
ParseNumberError::Hex => "hexadecimal",
|
||||||
} else {
|
};
|
||||||
self.arg().quote().to_string()
|
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"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue