1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

expr: Use thiserror to be consistent with error definition in other uutils

This commit is contained in:
Dorian Peron 2025-02-22 01:11:06 +01:00 committed by Sylvestre Ledru
parent 9f643838b8
commit 2670885b4f
3 changed files with 17 additions and 43 deletions

1
Cargo.lock generated
View file

@ -2781,6 +2781,7 @@ dependencies = [
"num-bigint",
"num-traits",
"onig",
"thiserror 2.0.11",
"uucore",
]

View file

@ -22,6 +22,7 @@ num-bigint = { workspace = true }
num-traits = { workspace = true }
onig = { workspace = true }
uucore = { workspace = true }
thiserror = { workspace = true }
[[bin]]
name = "expr"

View file

@ -3,10 +3,9 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use std::fmt::Display;
use clap::{crate_version, Arg, ArgAction, Command};
use syntax_tree::AstNode;
use thiserror::Error;
use uucore::{
display::Quotable,
error::{UError, UResult},
@ -25,63 +24,36 @@ mod options {
pub type ExprResult<T> = Result<T, ExprError>;
#[derive(Debug, PartialEq, Eq)]
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum ExprError {
#[error("syntax error: unexpected argument {}", .0.quote())]
UnexpectedArgument(String),
#[error("syntax error: missing argument after {}", .0.quote())]
MissingArgument(String),
#[error("non-integer argument")]
NonIntegerArgument,
#[error("missing operand")]
MissingOperand,
#[error("division by zero")]
DivisionByZero,
#[error("Invalid regex expression")]
InvalidRegexExpression,
#[error("syntax error: expecting ')' after {}", .0.quote())]
ExpectedClosingBraceAfter(String),
#[error("syntax error: expecting ')' instead of {}", .0.quote())]
ExpectedClosingBraceInsteadOf(String),
#[error("Unmatched ( or \\(")]
UnmatchedOpeningParenthesis,
#[error("Unmatched ) or \\)")]
UnmatchedClosingParenthesis,
#[error("Unmatched \\{{")]
UnmatchedOpeningBrace,
#[error("Unmatched ) or \\}}")]
UnmatchedClosingBrace,
#[error("Invalid content of {0}")]
InvalidContent(String),
}
impl Display for ExprError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::UnexpectedArgument(s) => {
write!(f, "syntax error: unexpected argument {}", s.quote())
}
Self::MissingArgument(s) => {
write!(f, "syntax error: missing argument after {}", s.quote())
}
Self::NonIntegerArgument => write!(f, "non-integer argument"),
Self::MissingOperand => write!(f, "missing operand"),
Self::DivisionByZero => write!(f, "division by zero"),
Self::InvalidRegexExpression => write!(f, "Invalid regex expression"),
Self::ExpectedClosingBraceAfter(s) => {
write!(f, "syntax error: expecting ')' after {}", s.quote())
}
Self::ExpectedClosingBraceInsteadOf(s) => {
write!(f, "syntax error: expecting ')' instead of {}", s.quote())
}
Self::UnmatchedOpeningParenthesis => {
write!(f, "Unmatched ( or \\(")
}
Self::UnmatchedClosingParenthesis => {
write!(f, "Unmatched ) or \\)")
}
Self::UnmatchedOpeningBrace => {
write!(f, "Unmatched \\{{")
}
Self::UnmatchedClosingBrace => {
write!(f, "Unmatched ) or \\}}")
}
Self::InvalidContent(s) => {
write!(f, "Invalid content of {}", s)
}
}
}
}
impl std::error::Error for ExprError {}
impl UError for ExprError {
fn code(&self) -> i32 {
2