mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 11:07:44 +00:00
test: fmt + requested change
This commit is contained in:
parent
bf94e8fff1
commit
11fd56c2be
3 changed files with 17 additions and 25 deletions
|
@ -29,14 +29,7 @@ impl std::fmt::Display for ParseError {
|
|||
/// 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 {
|
||||
match self {
|
||||
Self::Expected(_) => 2,
|
||||
Self::MissingArgument(_) => 2,
|
||||
Self::ExtraArgument(_) => 2,
|
||||
Self::UnknownOperator(_) => 2,
|
||||
Self::ExpectedValue => 2,
|
||||
Self::InvalidInteger(_) => 2,
|
||||
}
|
||||
2
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,13 +7,12 @@
|
|||
|
||||
// spell-checker:ignore (grammar) BOOLOP STRLEN FILETEST FILEOP INTOP STRINGOP ; (vars) LParen StrlenOp
|
||||
|
||||
use std::ffi::OsString;
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::iter::Peekable;
|
||||
|
||||
use super::error::{ParseError, ParseResult};
|
||||
|
||||
use uucore::display::Quotable;
|
||||
use uucore::error::UResult;
|
||||
|
||||
/// Represents one of the binary comparison operators for strings, integers, or files
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
|
@ -98,16 +97,16 @@ impl std::fmt::Display for Symbol {
|
|||
/// Format a Symbol for printing
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let s = match &self {
|
||||
Self::LParen => OsString::from("("),
|
||||
Self::Bang => OsString::from("!"),
|
||||
Self::LParen => OsStr::new("("),
|
||||
Self::Bang => OsStr::new("!"),
|
||||
Self::BoolOp(s)
|
||||
| Self::Literal(s)
|
||||
| Self::Op(Operator::String(s))
|
||||
| Self::Op(Operator::Int(s))
|
||||
| Self::Op(Operator::File(s))
|
||||
| Self::UnaryOp(UnaryOperator::StrlenOp(s))
|
||||
| Self::UnaryOp(UnaryOperator::FiletestOp(s)) => s.clone(),
|
||||
Self::None => OsString::from("None"),
|
||||
| Self::UnaryOp(UnaryOperator::FiletestOp(s)) => OsStr::new(s),
|
||||
Self::None => OsStr::new("None"),
|
||||
};
|
||||
write!(f, "{}", s.quote())
|
||||
}
|
||||
|
@ -159,7 +158,7 @@ impl Parser {
|
|||
fn expect(&mut self, value: &str) -> ParseResult<()> {
|
||||
match self.next_token() {
|
||||
Symbol::Literal(s) if s == value => Ok(()),
|
||||
_ => Err(ParseError::Expected(value.into())),
|
||||
_ => Err(ParseError::Expected(value.quote().to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -424,11 +423,11 @@ impl Parser {
|
|||
|
||||
/// Parser entry point: parse the token stream `self.tokens`, storing the
|
||||
/// resulting `Symbol` stack in `self.stack`.
|
||||
fn parse(&mut self) -> UResult<()> {
|
||||
fn parse(&mut self) -> ParseResult<()> {
|
||||
self.expr()?;
|
||||
|
||||
match self.tokens.next() {
|
||||
Some(token) => Err(ParseError::ExtraArgument(token.quote().to_string()).into()),
|
||||
Some(token) => Err(ParseError::ExtraArgument(token.quote().to_string())),
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
|
@ -436,7 +435,7 @@ impl Parser {
|
|||
|
||||
/// Parse the token stream `args`, returning a `Symbol` stack representing the
|
||||
/// operations to perform in postfix order.
|
||||
pub fn parse(args: Vec<OsString>) -> UResult<Vec<Symbol>> {
|
||||
pub fn parse(args: Vec<OsString>) -> ParseResult<Vec<Symbol>> {
|
||||
let mut p = Parser::new(args);
|
||||
p.parse()?;
|
||||
Ok(p.stack)
|
||||
|
|
|
@ -12,6 +12,7 @@ pub(crate) mod error;
|
|||
mod parser;
|
||||
|
||||
use clap::{crate_version, Command};
|
||||
use error::{ParseError, ParseResult};
|
||||
use parser::{parse, Operator, Symbol, UnaryOperator};
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::fs;
|
||||
|
@ -20,7 +21,6 @@ use std::os::unix::fs::MetadataExt;
|
|||
use uucore::display::Quotable;
|
||||
use uucore::error::{UResult, USimpleError};
|
||||
use uucore::format_usage;
|
||||
use error::{ParseError, ParseResult};
|
||||
|
||||
const USAGE: &str = "\
|
||||
{} EXPRESSION
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue