From 11fd56c2be74e89940eb5ed0a183acdbbc8824da Mon Sep 17 00:00:00 2001 From: leon3s Date: Sat, 25 Mar 2023 14:02:55 +0100 Subject: [PATCH] test: fmt + requested change --- src/uu/test/src/error.rs | 21 +++++++-------------- src/uu/test/src/parser.rs | 19 +++++++++---------- src/uu/test/src/test.rs | 2 +- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/uu/test/src/error.rs b/src/uu/test/src/error.rs index f237124ab..e9ab1c082 100644 --- a/src/uu/test/src/error.rs +++ b/src/uu/test/src/error.rs @@ -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 @@ -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 } } diff --git a/src/uu/test/src/parser.rs b/src/uu/test/src/parser.rs index dc791c934..db90098a1 100644 --- a/src/uu/test/src/parser.rs +++ b/src/uu/test/src/parser.rs @@ -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) -> UResult> { +pub fn parse(args: Vec) -> ParseResult> { let mut p = Parser::new(args); p.parse()?; Ok(p.stack) diff --git a/src/uu/test/src/test.rs b/src/uu/test/src/test.rs index 168ce77a8..313ef0413 100644 --- a/src/uu/test/src/test.rs +++ b/src/uu/test/src/test.rs @@ -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