Skip to content

Commit

Permalink
test: fmt + requested change
Browse files Browse the repository at this point in the history
  • Loading branch information
leon3s committed Mar 25, 2023
1 parent c8de6f1 commit 393eb2b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
21 changes: 7 additions & 14 deletions src/uu/test/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/// Represents an error encountered while parsing a test expression
#[derive(Debug)]
pub enum ParseError {
ExpectedValue,
Expected(String),
ExtraArgument(String),
MissingArgument(String),
UnknownOperator(String),
InvalidInteger(String),
ExpectedValue,
Expected(String),
ExtraArgument(String),
MissingArgument(String),
UnknownOperator(String),
InvalidInteger(String),
}

/// A Result type for parsing test expressions
Expand All @@ -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
}
}

Expand Down
19 changes: 9 additions & 10 deletions src/uu/test/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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())),
}
}

Expand Down Expand Up @@ -424,19 +423,19 @@ 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(()),
}
}
}

/// 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)
Expand Down
2 changes: 1 addition & 1 deletion src/uu/test/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 393eb2b

Please sign in to comment.