mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 05:34:58 +02:00
Add Filesize
type (#14369)
# Description Adds a new `Filesize` type so that `FromValue` can be used to convert a `Value::Filesize` to a `Filesize`. Currently, to extract a filesize from a `Value` using `FromValue`, you have to extract an `i64` which coerces `Value::Int`, `Value::Duration`, and `Value::Filesize` to an `i64`. Having a separate type also allows us to enforce checked math to catch overflows. Similarly, it allows us to specify other trait implementations like `Display` in a common place. # User-Facing Changes Multiplication with filesizes now error on overflow. Should not be a breaking change for plugins (i.e., serialization) since `Filesize` is marked with `serde(transparent)`. # Tests + Formatting Updated some tests.
This commit is contained in:
@ -90,7 +90,7 @@ pub fn average(values: &[Value], span: Span, head: Span) -> Result<Value, ShellE
|
||||
let total = &sum(Value::int(0, head), values.to_vec(), span, head)?;
|
||||
let span = total.span();
|
||||
match total {
|
||||
Value::Filesize { val, .. } => Ok(Value::filesize(val / values.len() as i64, span)),
|
||||
Value::Filesize { val, .. } => Ok(Value::filesize(val.get() / values.len() as i64, span)),
|
||||
Value::Duration { val, .. } => Ok(Value::duration(val / values.len() as i64, span)),
|
||||
_ => total.div(head, &Value::int(values.len() as i64, head), head),
|
||||
}
|
||||
|
@ -142,9 +142,10 @@ pub fn mode(values: &[Value], _span: Span, head: Span) -> Result<Value, ShellErr
|
||||
Value::Float { val, .. } => {
|
||||
Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Float))
|
||||
}
|
||||
Value::Filesize { val, .. } => {
|
||||
Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Filesize))
|
||||
}
|
||||
Value::Filesize { val, .. } => Ok(HashableType::new(
|
||||
val.get().to_ne_bytes(),
|
||||
NumberTypes::Filesize,
|
||||
)),
|
||||
Value::Error { error, .. } => Err(*error.clone()),
|
||||
other => Err(ShellError::UnsupportedInput {
|
||||
msg: "Unable to give a result with this input".to_string(),
|
||||
|
Reference in New Issue
Block a user