Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expr: improve the GNU compat #7167

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/uu/expr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ num-bigint = { workspace = true }
num-traits = { workspace = true }
onig = { workspace = true }
uucore = { workspace = true }
thiserror = { workspace = true }

[[bin]]
name = "expr"
Expand Down
94 changes: 47 additions & 47 deletions src/uu/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
// 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},
format_usage, help_about, help_section, help_usage,
};
Expand All @@ -25,70 +23,72 @@ mod options {

pub type ExprResult<T> = Result<T, ExprError>;

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, Error, PartialEq, Eq)]
pub enum ExprError {
#[error("syntax error: unexpected argument '{0}'")]
UnexpectedArgument(String),

#[error("syntax error: missing argument after '{0}'")]
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}'")]
ExpectedClosingBraceAfter(String),

#[error("syntax error: expecting ')' instead of '{0}'")]
ExpectedClosingBraceInsteadOf(String),
UnmatchedOpeningParenthesis,
UnmatchedClosingParenthesis,
UnmatchedOpeningBrace,
UnmatchedClosingBrace,
InvalidContent(String),

#[error("{0}")]
UnmatchedBrace(#[from] BraceType),

#[error("Invalid content of \\{{\\}}")]
InvalidBraceContent,

#[error("Regular expression too big")]
RegexTooBig,
}

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)
}
}
}
#[derive(Debug, Error, PartialEq, Eq)]
pub enum BraceType {
#[error("Unmatched ( or \\(")]
OpenParen,

#[error("Unmatched ) or \\)")]
CloseParen,

#[error("Unmatched \\{{")]
OpenCurly,

#[error("Unmatched \\}}")]
CloseCurly,
}

impl std::error::Error for ExprError {}
#[derive(Debug, PartialEq)]
enum BraceContent {
Valid,
Invalid,
Unmatched(BraceType),
RegexTooBig,
}
Comment on lines +77 to +83
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow I don't understand the purpose of this enum. With the exception of Valid, its variants are already defined as variants in ExprError.


impl UError for ExprError {
fn code(&self) -> i32 {
2
}

fn usage(&self) -> bool {
*self == Self::MissingOperand
matches!(self, Self::MissingOperand)
}
}

Expand Down
Loading
Loading