diff --git a/src/uu/expr/src/expr.rs b/src/uu/expr/src/expr.rs index b46034f84..1a9bb07de 100644 --- a/src/uu/expr/src/expr.rs +++ b/src/uu/expr/src/expr.rs @@ -108,7 +108,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { .map(|v| v.into_iter().map(|s| s.as_ref()).collect::>()) .unwrap_or_default(); - let res = AstNode::parse(&token_strings)?.eval()?.to_string(); + let res: String = AstNode::parse(&token_strings)?.eval()?.into(); println!("{res}"); if !is_truthy(&NumOrStr::from(res)) { return Err(1.into()); diff --git a/src/uu/expr/src/syntax_tree.rs b/src/uu/expr/src/syntax_tree.rs index 4f447e60f..7677b5e7e 100644 --- a/src/uu/expr/src/syntax_tree.rs +++ b/src/uu/expr/src/syntax_tree.rs @@ -59,7 +59,7 @@ impl RelationOp { fn eval(&self, a: &AstNode, b: &AstNode) -> ExprResult { let a = a.eval()?; let b = b.eval()?; - let b = if let (Ok(a), Ok(b)) = (&a.coerce_bigint(), &b.coerce_bigint()) { + let b = if let (Ok(a), Ok(b)) = (&a.to_bigint(), &b.to_bigint()) { match self { Self::Lt => a < b, Self::Leq => a <= b, @@ -89,8 +89,8 @@ impl RelationOp { impl NumericOp { fn eval(&self, left: &AstNode, right: &AstNode) -> ExprResult { - let a = left.eval()?.to_bigint()?; - let b = right.eval()?.to_bigint()?; + let a = >>::into(left.eval()?)?; + let b = >>::into(right.eval()?)?; Ok(NumOrStr::Num(match self { Self::Add => a + b, Self::Sub => a - b, @@ -135,9 +135,9 @@ impl StringOp { Ok(left) } Self::Match => { - let left = left.eval()?; - let right = right.eval()?; - let re_string = format!("^{}", right.to_string()); + let left: String = left.eval()?.into(); + let right: String = right.eval()?.into(); + let re_string = format!("^{}", right); let re = Regex::with_options( &re_string, RegexOptions::REGEX_OPTION_NONE, @@ -145,18 +145,18 @@ impl StringOp { ) .map_err(|_| ExprError::InvalidRegexExpression)?; Ok(NumOrStr::from(if re.captures_len() > 0 { - re.captures(&left.to_string()) + re.captures(&left) .map(|captures| captures.at(1).unwrap()) .unwrap_or("") .to_string() } else { - re.find(&left.to_string()) + re.find(&left) .map_or("0".to_string(), |(start, end)| (end - start).to_string()) })) } Self::Index => { - let left = left.eval()?.to_string(); - let right = right.eval()?.to_string(); + let left: String = left.eval()?.into(); + let right: String = right.eval()?.into(); for (current_idx, ch_h) in left.chars().enumerate() { for ch_n in right.to_string().chars() { if ch_n == ch_h { @@ -218,22 +218,26 @@ impl From for NumOrStr { } } -impl NumOrStr { - pub fn to_usize(self) -> Option { - match self.to_bigint() { +impl Into> for NumOrStr { + fn into(self) -> Option { + match self.into() { Ok(num) => num.to_usize(), Err(_) => None, } } +} - pub fn to_string(self) -> String { +impl Into for NumOrStr { + fn into(self) -> String { match self { Self::Num(num) => num.to_string(), Self::Str(str) => str.to_string(), } } +} - pub fn to_bigint(self) -> ExprResult { +impl Into> for NumOrStr { + fn into(self) -> ExprResult { match self { Self::Num(num) => Ok(num), Self::Str(str) => str @@ -241,8 +245,10 @@ impl NumOrStr { .map_err(|_| ExprError::NonIntegerArgument), } } +} - pub fn coerce_bigint(&self) -> Result { +impl NumOrStr { + pub fn to_bigint(&self) -> Result { match self { Self::Num(num) => Ok(num.clone()), Self::Str(str) => str.parse::(), @@ -288,7 +294,7 @@ impl AstNode { pos, length, } => { - let string = string.eval()?.to_string(); + let string: String = string.eval()?.into(); // The GNU docs say: // @@ -297,8 +303,9 @@ impl AstNode { // // So we coerce errors into 0 to make that the only case we // have to care about. - let pos: usize = pos.eval()?.to_usize().unwrap_or(0); - let length: usize = length.eval()?.to_usize().unwrap_or(0); + let pos: usize = >>::into(pos.eval()?).unwrap_or(0); + let length: usize = + >>::into(length.eval()?).unwrap_or(0); let (Some(pos), Some(_)) = (pos.checked_sub(1), length.checked_sub(1)) else { return Ok(NumOrStr::from(String::new())); @@ -308,9 +315,11 @@ impl AstNode { string.chars().skip(pos).take(length).collect::(), )) } - Self::Length { string } => { - Ok(NumOrStr::from(string.eval()?.to_string().chars().count())) - } + Self::Length { string } => Ok(NumOrStr::from( + >::into(string.eval()?) + .chars() + .count(), + )), } } }