test: move to thiserror

This commit is contained in:
Sylvestre Ledru 2025-06-09 09:39:49 +02:00
parent 6ca60cf2c7
commit 09bb35e11e
4 changed files with 14 additions and 19 deletions

1
Cargo.lock generated
View file

@ -3578,6 +3578,7 @@ version = "0.1.0"
dependencies = [
"clap",
"libc",
"thiserror 2.0.12",
"uucore",
]

1
fuzz/Cargo.lock generated
View file

@ -1379,6 +1379,7 @@ version = "0.1.0"
dependencies = [
"clap",
"libc",
"thiserror",
"uucore",
]

View file

@ -21,6 +21,7 @@ path = "src/test.rs"
clap = { workspace = true }
libc = { workspace = true }
uucore = { workspace = true, features = ["process"] }
thiserror = { workspace = true }
[[bin]]
name = "test"

View file

@ -2,42 +2,34 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use thiserror::Error;
/// Represents an error encountered while parsing a test expression
#[derive(Debug)]
#[derive(Error, Debug)]
pub enum ParseError {
#[error("expected value")]
ExpectedValue,
#[error("expected {0}")]
Expected(String),
#[error("extra argument {0}")]
ExtraArgument(String),
#[error("missing argument after {0}")]
MissingArgument(String),
#[error("unknown operator {0}")]
UnknownOperator(String),
#[error("invalid integer {0}")]
InvalidInteger(String),
#[error("{0}: unary operator expected")]
UnaryOperatorExpected(String),
}
/// A Result type for parsing test expressions
pub type ParseResult<T> = Result<T, ParseError>;
/// Implement Display trait for ParseError to make it easier to print useful errors.
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Expected(s) => write!(f, "expected {s}"),
Self::ExpectedValue => write!(f, "expected value"),
Self::MissingArgument(s) => write!(f, "missing argument after {s}"),
Self::ExtraArgument(s) => write!(f, "extra argument {s}"),
Self::UnknownOperator(s) => write!(f, "unknown operator {s}"),
Self::InvalidInteger(s) => write!(f, "invalid integer {s}"),
Self::UnaryOperatorExpected(op) => write!(f, "{op}: unary operator expected"),
}
}
}
/// Implement UError trait for ParseError to make it easier to return useful error codes from main().
impl uucore::error::UError for ParseError {
fn code(&self) -> i32 {
2
}
}
/// Implement standard Error trait for UError
impl std::error::Error for ParseError {}