make the pattern-matcher and eval engine use the same unit computation (#8973)

# Description
this pr adresses
[this](7413ef2824/crates/nu-protocol/src/engine/pattern_match.rs (L149))
'fix me'
This commit is contained in:
mike
2023-05-12 20:18:11 +03:00
committed by GitHub
parent 6cbd42974b
commit 2254805a6d
3 changed files with 134 additions and 172 deletions

View File

@ -1,6 +1,6 @@
use crate::{
ast::{Expr, MatchPattern, Pattern, RangeInclusion},
Span, Unit, Value, VarId,
Span, Value, VarId,
};
pub trait Matcher {
@ -145,55 +145,10 @@ impl Matcher for Pattern {
}
}
Expr::ValueWithUnit(amount, unit) => {
if let Value::Filesize { val, .. } = &value {
// FIXME: we probably want this math in one place that both the
// pattern matcher and the eval engine can get to it
match &amount.expr {
Expr::Int(amount) => match &unit.item {
Unit::Byte => amount == val,
Unit::Kilobyte => *val == amount * 1000,
Unit::Megabyte => *val == amount * 1000 * 1000,
Unit::Gigabyte => *val == amount * 1000 * 1000 * 1000,
Unit::Petabyte => *val == amount * 1000 * 1000 * 1000 * 1000,
Unit::Exabyte => {
*val == amount * 1000 * 1000 * 1000 * 1000 * 1000
}
Unit::Zettabyte => {
*val == amount * 1000 * 1000 * 1000 * 1000 * 1000 * 1000
}
Unit::Kibibyte => *val == amount * 1024,
Unit::Mebibyte => *val == amount * 1024 * 1024,
Unit::Gibibyte => *val == amount * 1024 * 1024 * 1024,
Unit::Pebibyte => *val == amount * 1024 * 1024 * 1024 * 1024,
Unit::Exbibyte => {
*val == amount * 1024 * 1024 * 1024 * 1024 * 1024
}
Unit::Zebibyte => {
*val == amount * 1024 * 1024 * 1024 * 1024 * 1024 * 1024
}
_ => false,
},
_ => false,
}
} else if let Value::Duration { val, .. } = &value {
// FIXME: we probably want this math in one place that both the
// pattern matcher and the eval engine can get to it
match &amount.expr {
Expr::Int(amount) => match &unit.item {
Unit::Nanosecond => val == amount,
Unit::Microsecond => *val == amount * 1000,
Unit::Millisecond => *val == amount * 1000 * 1000,
Unit::Second => *val == amount * 1000 * 1000 * 1000,
Unit::Minute => *val == amount * 1000 * 1000 * 1000 * 60,
Unit::Hour => *val == amount * 1000 * 1000 * 1000 * 60 * 60,
Unit::Day => *val == amount * 1000 * 1000 * 1000 * 60 * 60 * 24,
Unit::Week => {
*val == amount * 1000 * 1000 * 1000 * 60 * 60 * 24 * 7
}
_ => false,
},
_ => false,
}
let span = unit.span;
if let Expr::Int(size) = amount.expr {
&unit.item.to_value(size, span) == value
} else {
false
}