1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-31 04:57:45 +00:00

Fix errors

This commit is contained in:
Arpit Bhadauria 2023-12-03 22:07:56 +00:00
parent f8573d5551
commit 5672e3d9bd

View file

@ -5,7 +5,7 @@
// spell-checker:ignore (ToDO) ints paren prec multibytes // spell-checker:ignore (ToDO) ints paren prec multibytes
use num_bigint::BigInt; use num_bigint::{BigInt, ParseBigIntError};
use num_traits::ToPrimitive; use num_traits::ToPrimitive;
use onig::{Regex, RegexOptions, Syntax}; use onig::{Regex, RegexOptions, Syntax};
@ -57,9 +57,9 @@ impl BinOp {
impl RelationOp { impl RelationOp {
fn eval(&self, a: &AstNode, b: &AstNode) -> ExprResult<NumOrStr> { fn eval(&self, a: &AstNode, b: &AstNode) -> ExprResult<NumOrStr> {
let a = a.eval()?.coerce_num(); let a = a.eval()?;
let b = b.eval()?.coerce_num(); let b = b.eval()?;
let b = if let (NumOrStr::Num(a), NumOrStr::Num(b)) = (&a, &b) { let b = if let (Ok(a), Ok(b)) = (&a.coerce_bigint(), &b.coerce_bigint()) {
match self { match self {
Self::Lt => a < b, Self::Lt => a < b,
Self::Leq => a <= b, Self::Leq => a <= b,
@ -242,13 +242,10 @@ impl NumOrStr {
} }
} }
pub fn coerce_num(self: Self) -> NumOrStr { pub fn coerce_bigint(self: &Self) -> Result<BigInt, ParseBigIntError> {
match self { match self {
Self::Num(num) => Self::from(num), Self::Num(num) => Ok(num.clone()),
Self::Str(str) => match str.parse::<BigInt>() { Self::Str(str) => str.parse::<BigInt>(),
Ok(num) => Self::from(num),
Err(_) => Self::from(str),
},
} }
} }
} }
@ -458,7 +455,7 @@ impl<'a> Parser<'a> {
/// Truthy strings are either empty or match the regex "-?0+". /// Truthy strings are either empty or match the regex "-?0+".
pub fn is_truthy(s: &NumOrStr) -> bool { pub fn is_truthy(s: &NumOrStr) -> bool {
match s { match s {
NumOrStr::Num(num) => num == &BigInt::from(0), NumOrStr::Num(num) => num != &BigInt::from(0),
NumOrStr::Str(str) => { NumOrStr::Str(str) => {
// Edge case: `-` followed by nothing is truthy // Edge case: `-` followed by nothing is truthy
if str == "-" { if str == "-" {