mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 12:15:42 +02:00
Port random integer & decimal to engine-p + related refactoring (#3393)
* Implement minmax for Range; Simplify range command * Port random integer to enginep; New FromValue impl Now, FromValue is implemented for Tagged<Range> to allow extracting args into this type. * Make sure range value extraction fails properly The range endpoint extraction methods now return error instead of silently clipping the value. This now makes `random integer ..-4` fail properly since -4 can't be cast as u64. * Port random decimal to enginep & Refactor This added a way to interpret Range limits as f64 and a Primitive helper to get its value as f64. A side effect of this commit is that it is now possible to specify the command bounds as true decimals. E.g., `random decimal 0.0..3.14` does not clip 3.14 to 3.
This commit is contained in:
@ -129,6 +129,30 @@ impl Primitive {
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a primitive value to a f64, if possible. Uses a span to build an error if the conversion isn't possible.
|
||||
pub fn as_f64(&self, span: Span) -> Result<f64, ShellError> {
|
||||
match self {
|
||||
Primitive::Int(int) => int.to_f64().ok_or_else(|| {
|
||||
ShellError::range_error(
|
||||
ExpectedRange::F64,
|
||||
&format!("{}", int).spanned(span),
|
||||
"converting an integer into a 64-bit floating point",
|
||||
)
|
||||
}),
|
||||
Primitive::Decimal(decimal) => decimal.to_f64().ok_or_else(|| {
|
||||
ShellError::range_error(
|
||||
ExpectedRange::F64,
|
||||
&format!("{}", decimal).spanned(span),
|
||||
"converting a decimal into a 64-bit floating point",
|
||||
)
|
||||
}),
|
||||
other => Err(ShellError::type_error(
|
||||
"number",
|
||||
other.type_name().spanned(span),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a primitive value to a i64, if possible. Uses a span to build an error if the conversion isn't possible.
|
||||
pub fn as_i64(&self, span: Span) -> Result<i64, ShellError> {
|
||||
match self {
|
||||
|
@ -1,5 +1,6 @@
|
||||
use crate::value::Primitive;
|
||||
use derive_new::new;
|
||||
use nu_errors::ShellError;
|
||||
use nu_source::{DbgDocBldr, DebugDocBuilder, Spanned};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@ -35,3 +36,89 @@ pub struct Range {
|
||||
pub from: (Spanned<Primitive>, RangeInclusion),
|
||||
pub to: (Spanned<Primitive>, RangeInclusion),
|
||||
}
|
||||
|
||||
impl Range {
|
||||
pub fn min_u64(&self) -> Result<u64, ShellError> {
|
||||
let (from, range_incl) = &self.from;
|
||||
|
||||
let minval = if let Primitive::Nothing = from.item {
|
||||
u64::MIN
|
||||
} else {
|
||||
from.item.as_u64(from.span)?
|
||||
};
|
||||
|
||||
match range_incl {
|
||||
RangeInclusion::Inclusive => Ok(minval),
|
||||
RangeInclusion::Exclusive => Ok(minval.saturating_add(1)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn max_u64(&self) -> Result<u64, ShellError> {
|
||||
let (to, range_incl) = &self.to;
|
||||
|
||||
let maxval = if let Primitive::Nothing = to.item {
|
||||
u64::MAX
|
||||
} else {
|
||||
to.item.as_u64(to.span)?
|
||||
};
|
||||
|
||||
match range_incl {
|
||||
RangeInclusion::Inclusive => Ok(maxval),
|
||||
RangeInclusion::Exclusive => Ok(maxval.saturating_sub(1)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn min_usize(&self) -> Result<usize, ShellError> {
|
||||
let (from, range_incl) = &self.from;
|
||||
|
||||
let minval = if let Primitive::Nothing = from.item {
|
||||
usize::MIN
|
||||
} else {
|
||||
from.item.as_usize(from.span)?
|
||||
};
|
||||
|
||||
match range_incl {
|
||||
RangeInclusion::Inclusive => Ok(minval),
|
||||
RangeInclusion::Exclusive => Ok(minval.saturating_add(1)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn max_usize(&self) -> Result<usize, ShellError> {
|
||||
let (to, range_incl) = &self.to;
|
||||
|
||||
let maxval = if let Primitive::Nothing = to.item {
|
||||
usize::MAX
|
||||
} else {
|
||||
to.item.as_usize(to.span)?
|
||||
};
|
||||
|
||||
match range_incl {
|
||||
RangeInclusion::Inclusive => Ok(maxval),
|
||||
RangeInclusion::Exclusive => Ok(maxval.saturating_sub(1)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn min_f64(&self) -> Result<f64, ShellError> {
|
||||
let from = &self.from.0;
|
||||
|
||||
if let Primitive::Nothing = from.item {
|
||||
Ok(f64::MIN)
|
||||
} else {
|
||||
Ok(from.item.as_f64(from.span)?)
|
||||
}
|
||||
|
||||
// How would inclusive vs. exclusive range work here?
|
||||
}
|
||||
|
||||
pub fn max_f64(&self) -> Result<f64, ShellError> {
|
||||
let to = &self.to.0;
|
||||
|
||||
if let Primitive::Nothing = to.item {
|
||||
Ok(f64::MAX)
|
||||
} else {
|
||||
Ok(to.item.as_f64(to.span)?)
|
||||
}
|
||||
|
||||
// How would inclusive vs. exclusive range work here?
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user