From 09bb35e11ea404fb3b7235854f683c70cc0aa622 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Mon, 9 Jun 2025 09:39:49 +0200 Subject: [PATCH] test: move to thiserror --- Cargo.lock | 1 + fuzz/Cargo.lock | 1 + src/uu/test/Cargo.toml | 1 + src/uu/test/src/error.rs | 30 +++++++++++------------------- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7ee0e3c28..0c66b92bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3578,6 +3578,7 @@ version = "0.1.0" dependencies = [ "clap", "libc", + "thiserror 2.0.12", "uucore", ] diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index 6190e8ba6..6328b3f6e 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -1379,6 +1379,7 @@ version = "0.1.0" dependencies = [ "clap", "libc", + "thiserror", "uucore", ] diff --git a/src/uu/test/Cargo.toml b/src/uu/test/Cargo.toml index c59f9fcb4..53cf597af 100644 --- a/src/uu/test/Cargo.toml +++ b/src/uu/test/Cargo.toml @@ -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" diff --git a/src/uu/test/src/error.rs b/src/uu/test/src/error.rs index e392216e1..235786bc6 100644 --- a/src/uu/test/src/error.rs +++ b/src/uu/test/src/error.rs @@ -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 = Result; -/// 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 {}