From 6ed68cd581491e66bc312d7bf6a00790c5ba06ab Mon Sep 17 00:00:00 2001 From: Ian Manske Date: Tue, 5 Nov 2024 21:53:24 -0800 Subject: [PATCH] Fix compilation errors --- crates/nu-cmd-extra/src/extra/bits/into.rs | 2 +- .../nu-cmd-extra/src/extra/conversions/fmt.rs | 2 +- .../nu-command/src/charting/hashable_value.rs | 9 ++- crates/nu-command/src/conversions/fill.rs | 2 +- .../nu-command/src/conversions/into/binary.rs | 2 +- crates/nu-command/src/conversions/into/int.rs | 2 +- .../nu-command/src/database/values/sqlite.rs | 2 +- crates/nu-command/src/formats/to/json.rs | 2 +- crates/nu-command/src/formats/to/msgpack.rs | 2 +- crates/nu-command/src/formats/to/toml.rs | 2 +- crates/nu-command/src/formats/to/yaml.rs | 4 +- crates/nu-command/src/math/avg.rs | 2 +- crates/nu-command/src/math/mode.rs | 7 +- crates/nu-parser/src/parser.rs | 80 +++++++++++++++---- crates/nu-protocol/src/ir/mod.rs | 8 +- crates/nu_plugin_formats/src/to/plist.rs | 12 ++- crates/nuon/src/from.rs | 39 +++------ 17 files changed, 106 insertions(+), 73 deletions(-) diff --git a/crates/nu-cmd-extra/src/extra/bits/into.rs b/crates/nu-cmd-extra/src/extra/bits/into.rs index 7edc8d3584..3452c1eeee 100644 --- a/crates/nu-cmd-extra/src/extra/bits/into.rs +++ b/crates/nu-cmd-extra/src/extra/bits/into.rs @@ -203,7 +203,7 @@ pub fn action(input: &Value, _args: &Arguments, span: Span) -> Value { Value::string(raw_string.trim(), span) } Value::Int { val, .. } => convert_to_smallest_number_type(*val, span), - Value::Filesize { val, .. } => convert_to_smallest_number_type(*val, span), + Value::Filesize { val, .. } => convert_to_smallest_number_type(val.get(), span), Value::Duration { val, .. } => convert_to_smallest_number_type(*val, span), Value::String { val, .. } => { let raw_bytes = val.as_bytes(); diff --git a/crates/nu-cmd-extra/src/extra/conversions/fmt.rs b/crates/nu-cmd-extra/src/extra/conversions/fmt.rs index 15ca742c76..5bf2ae47a4 100644 --- a/crates/nu-cmd-extra/src/extra/conversions/fmt.rs +++ b/crates/nu-cmd-extra/src/extra/conversions/fmt.rs @@ -66,7 +66,7 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value { match input { Value::Float { val, .. } => fmt_it_64(*val, span), Value::Int { val, .. } => fmt_it(*val, span), - Value::Filesize { val, .. } => fmt_it(*val, span), + Value::Filesize { val, .. } => fmt_it(val.get(), span), // Propagate errors by explicitly matching them before the final case. Value::Error { .. } => input.clone(), other => Value::error( diff --git a/crates/nu-command/src/charting/hashable_value.rs b/crates/nu-command/src/charting/hashable_value.rs index b74b5f7761..636f5b7a05 100644 --- a/crates/nu-command/src/charting/hashable_value.rs +++ b/crates/nu-command/src/charting/hashable_value.rs @@ -1,5 +1,5 @@ use chrono::{DateTime, FixedOffset}; -use nu_protocol::{ShellError, Span, Value}; +use nu_protocol::{Filesize, ShellError, Span, Value}; use std::hash::{Hash, Hasher}; /// A subset of [`Value`], which is hashable. @@ -30,7 +30,7 @@ pub enum HashableValue { span: Span, }, Filesize { - val: i64, + val: Filesize, span: Span, }, Duration { @@ -198,7 +198,10 @@ mod test { (Value::int(1, span), HashableValue::Int { val: 1, span }), ( Value::filesize(1, span), - HashableValue::Filesize { val: 1, span }, + HashableValue::Filesize { + val: 1.into(), + span, + }, ), ( Value::duration(1, span), diff --git a/crates/nu-command/src/conversions/fill.rs b/crates/nu-command/src/conversions/fill.rs index d125ee29b2..65d6b20b22 100644 --- a/crates/nu-command/src/conversions/fill.rs +++ b/crates/nu-command/src/conversions/fill.rs @@ -167,7 +167,7 @@ fn fill( fn action(input: &Value, args: &Arguments, span: Span) -> Value { match input { Value::Int { val, .. } => fill_int(*val, args, span), - Value::Filesize { val, .. } => fill_int(*val, args, span), + Value::Filesize { val, .. } => fill_int(val.get(), args, span), Value::Float { val, .. } => fill_float(*val, args, span), Value::String { val, .. } => fill_string(val, args, span), // Propagate errors by explicitly matching them before the final case. diff --git a/crates/nu-command/src/conversions/into/binary.rs b/crates/nu-command/src/conversions/into/binary.rs index d82f911506..fb549e50a7 100644 --- a/crates/nu-command/src/conversions/into/binary.rs +++ b/crates/nu-command/src/conversions/into/binary.rs @@ -147,7 +147,7 @@ pub fn action(input: &Value, _args: &Arguments, span: Span) -> Value { Value::Binary { .. } => input.clone(), Value::Int { val, .. } => Value::binary(val.to_ne_bytes().to_vec(), span), Value::Float { val, .. } => Value::binary(val.to_ne_bytes().to_vec(), span), - Value::Filesize { val, .. } => Value::binary(val.to_ne_bytes().to_vec(), span), + Value::Filesize { val, .. } => Value::binary(val.get().to_ne_bytes().to_vec(), span), Value::String { val, .. } => Value::binary(val.as_bytes().to_vec(), span), Value::Bool { val, .. } => Value::binary(i64::from(*val).to_ne_bytes().to_vec(), span), Value::Duration { val, .. } => Value::binary(val.to_ne_bytes().to_vec(), span), diff --git a/crates/nu-command/src/conversions/into/int.rs b/crates/nu-command/src/conversions/into/int.rs index f55770e7ff..67cba23090 100644 --- a/crates/nu-command/src/conversions/into/int.rs +++ b/crates/nu-command/src/conversions/into/int.rs @@ -253,7 +253,7 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value { convert_int(input, span, radix) } } - Value::Filesize { val, .. } => Value::int(*val, span), + Value::Filesize { val, .. } => Value::int(val.get(), span), Value::Float { val, .. } => Value::int( { if radix == 10 { diff --git a/crates/nu-command/src/database/values/sqlite.rs b/crates/nu-command/src/database/values/sqlite.rs index 5d0f7d1fe3..620ac95324 100644 --- a/crates/nu-command/src/database/values/sqlite.rs +++ b/crates/nu-command/src/database/values/sqlite.rs @@ -421,7 +421,7 @@ pub fn value_to_sql(value: Value) -> Result, ShellError Value::Bool { val, .. } => Box::new(val), Value::Int { val, .. } => Box::new(val), Value::Float { val, .. } => Box::new(val), - Value::Filesize { val, .. } => Box::new(val), + Value::Filesize { val, .. } => Box::new(val.get()), Value::Duration { val, .. } => Box::new(val), Value::Date { val, .. } => Box::new(val), Value::String { val, .. } => Box::new(val), diff --git a/crates/nu-command/src/formats/to/json.rs b/crates/nu-command/src/formats/to/json.rs index 9e68961092..27c0af856a 100644 --- a/crates/nu-command/src/formats/to/json.rs +++ b/crates/nu-command/src/formats/to/json.rs @@ -109,7 +109,7 @@ pub fn value_to_json_value(v: &Value) -> Result { let span = v.span(); Ok(match v { Value::Bool { val, .. } => nu_json::Value::Bool(*val), - Value::Filesize { val, .. } => nu_json::Value::I64(*val), + Value::Filesize { val, .. } => nu_json::Value::I64(val.get()), Value::Duration { val, .. } => nu_json::Value::I64(*val), Value::Date { val, .. } => nu_json::Value::String(val.to_string()), Value::Float { val, .. } => nu_json::Value::F64(*val), diff --git a/crates/nu-command/src/formats/to/msgpack.rs b/crates/nu-command/src/formats/to/msgpack.rs index 537aa9efdb..f7c4c7df3d 100644 --- a/crates/nu-command/src/formats/to/msgpack.rs +++ b/crates/nu-command/src/formats/to/msgpack.rs @@ -168,7 +168,7 @@ pub(crate) fn write_value( mp::write_f64(out, *val).err_span(span)?; } Value::Filesize { val, .. } => { - mp::write_sint(out, *val).err_span(span)?; + mp::write_sint(out, val.get()).err_span(span)?; } Value::Duration { val, .. } => { mp::write_sint(out, *val).err_span(span)?; diff --git a/crates/nu-command/src/formats/to/toml.rs b/crates/nu-command/src/formats/to/toml.rs index 9e5728ef1e..5f28284904 100644 --- a/crates/nu-command/src/formats/to/toml.rs +++ b/crates/nu-command/src/formats/to/toml.rs @@ -47,7 +47,7 @@ fn helper(engine_state: &EngineState, v: &Value) -> Result toml::Value::Boolean(*val), Value::Int { val, .. } => toml::Value::Integer(*val), - Value::Filesize { val, .. } => toml::Value::Integer(*val), + Value::Filesize { val, .. } => toml::Value::Integer(val.get()), Value::Duration { val, .. } => toml::Value::String(val.to_string()), Value::Date { val, .. } => toml::Value::Datetime(to_toml_datetime(val)), Value::Range { .. } => toml::Value::String("".to_string()), diff --git a/crates/nu-command/src/formats/to/yaml.rs b/crates/nu-command/src/formats/to/yaml.rs index 73c743a056..4fc4e62319 100644 --- a/crates/nu-command/src/formats/to/yaml.rs +++ b/crates/nu-command/src/formats/to/yaml.rs @@ -44,7 +44,9 @@ pub fn value_to_yaml_value(v: &Value) -> Result { Ok(match &v { Value::Bool { val, .. } => serde_yaml::Value::Bool(*val), Value::Int { val, .. } => serde_yaml::Value::Number(serde_yaml::Number::from(*val)), - Value::Filesize { val, .. } => serde_yaml::Value::Number(serde_yaml::Number::from(*val)), + Value::Filesize { val, .. } => { + serde_yaml::Value::Number(serde_yaml::Number::from(val.get())) + } Value::Duration { val, .. } => serde_yaml::Value::String(val.to_string()), Value::Date { val, .. } => serde_yaml::Value::String(val.to_string()), Value::Range { .. } => serde_yaml::Value::Null, diff --git a/crates/nu-command/src/math/avg.rs b/crates/nu-command/src/math/avg.rs index 07e2ed00bf..a7f6d3088a 100644 --- a/crates/nu-command/src/math/avg.rs +++ b/crates/nu-command/src/math/avg.rs @@ -90,7 +90,7 @@ pub fn average(values: &[Value], span: Span, head: Span) -> Result 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), } diff --git a/crates/nu-command/src/math/mode.rs b/crates/nu-command/src/math/mode.rs index b89896ca88..cf4ae36b2d 100644 --- a/crates/nu-command/src/math/mode.rs +++ b/crates/nu-command/src/math/mode.rs @@ -142,9 +142,10 @@ pub fn mode(values: &[Value], _span: Span, head: Span) -> Result { 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(), diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index e4d3765e17..52b8a6c23f 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -11,9 +11,9 @@ use itertools::Itertools; use log::trace; use nu_engine::DIR_VAR_PARSER_INFO; use nu_protocol::{ - ast::*, engine::StateWorkingSet, eval_const::eval_constant, BlockId, DeclId, DidYouMean, Flag, - ParseError, PositionalArg, Signature, Span, Spanned, SyntaxShape, Type, VarId, ENV_VARIABLE_ID, - IN_VARIABLE_ID, + ast::*, engine::StateWorkingSet, eval_const::eval_constant, BlockId, DeclId, DidYouMean, + FilesizeUnit, Flag, ParseError, PositionalArg, Signature, Span, Spanned, SyntaxShape, Type, + VarId, ENV_VARIABLE_ID, IN_VARIABLE_ID, }; use std::{ collections::{HashMap, HashSet}, @@ -2572,19 +2572,67 @@ pub fn parse_unit_value<'res>( } pub const FILESIZE_UNIT_GROUPS: &[UnitGroup] = &[ - (Unit::Kilobyte, "KB", Some((Unit::Byte, 1000))), - (Unit::Megabyte, "MB", Some((Unit::Kilobyte, 1000))), - (Unit::Gigabyte, "GB", Some((Unit::Megabyte, 1000))), - (Unit::Terabyte, "TB", Some((Unit::Gigabyte, 1000))), - (Unit::Petabyte, "PB", Some((Unit::Terabyte, 1000))), - (Unit::Exabyte, "EB", Some((Unit::Petabyte, 1000))), - (Unit::Kibibyte, "KIB", Some((Unit::Byte, 1024))), - (Unit::Mebibyte, "MIB", Some((Unit::Kibibyte, 1024))), - (Unit::Gibibyte, "GIB", Some((Unit::Mebibyte, 1024))), - (Unit::Tebibyte, "TIB", Some((Unit::Gibibyte, 1024))), - (Unit::Pebibyte, "PIB", Some((Unit::Tebibyte, 1024))), - (Unit::Exbibyte, "EIB", Some((Unit::Pebibyte, 1024))), - (Unit::Byte, "B", None), + ( + Unit::Filesize(FilesizeUnit::KB), + "KB", + Some((Unit::Filesize(FilesizeUnit::B), 1000)), + ), + ( + Unit::Filesize(FilesizeUnit::MB), + "MB", + Some((Unit::Filesize(FilesizeUnit::KB), 1000)), + ), + ( + Unit::Filesize(FilesizeUnit::GB), + "GB", + Some((Unit::Filesize(FilesizeUnit::MB), 1000)), + ), + ( + Unit::Filesize(FilesizeUnit::TB), + "TB", + Some((Unit::Filesize(FilesizeUnit::GB), 1000)), + ), + ( + Unit::Filesize(FilesizeUnit::PB), + "PB", + Some((Unit::Filesize(FilesizeUnit::TB), 1000)), + ), + ( + Unit::Filesize(FilesizeUnit::EB), + "EB", + Some((Unit::Filesize(FilesizeUnit::PB), 1000)), + ), + ( + Unit::Filesize(FilesizeUnit::KiB), + "KIB", + Some((Unit::Filesize(FilesizeUnit::B), 1024)), + ), + ( + Unit::Filesize(FilesizeUnit::MiB), + "MIB", + Some((Unit::Filesize(FilesizeUnit::KiB), 1024)), + ), + ( + Unit::Filesize(FilesizeUnit::GiB), + "GIB", + Some((Unit::Filesize(FilesizeUnit::MiB), 1024)), + ), + ( + Unit::Filesize(FilesizeUnit::TiB), + "TIB", + Some((Unit::Filesize(FilesizeUnit::GiB), 1024)), + ), + ( + Unit::Filesize(FilesizeUnit::PiB), + "PIB", + Some((Unit::Filesize(FilesizeUnit::TiB), 1024)), + ), + ( + Unit::Filesize(FilesizeUnit::EiB), + "EIB", + Some((Unit::Filesize(FilesizeUnit::EiB), 1024)), + ), + (Unit::Filesize(FilesizeUnit::B), "B", None), ]; pub const DURATION_UNIT_GROUPS: &[UnitGroup] = &[ diff --git a/crates/nu-protocol/src/ir/mod.rs b/crates/nu-protocol/src/ir/mod.rs index 428a2667e0..9d657ba080 100644 --- a/crates/nu-protocol/src/ir/mod.rs +++ b/crates/nu-protocol/src/ir/mod.rs @@ -1,13 +1,11 @@ -use std::{fmt, sync::Arc}; - use crate::{ ast::{CellPath, Expression, Operator, Pattern, RangeInclusion}, engine::EngineState, - BlockId, DeclId, RegId, Span, Value, VarId, + BlockId, DeclId, Filesize, RegId, Span, Value, VarId, }; - use chrono::{DateTime, FixedOffset}; use serde::{Deserialize, Serialize}; +use std::{fmt, sync::Arc}; mod call; mod display; @@ -397,7 +395,7 @@ pub enum Literal { Bool(bool), Int(i64), Float(f64), - Filesize(i64), + Filesize(Filesize), Duration(i64), Binary(DataSlice), Block(BlockId), diff --git a/crates/nu_plugin_formats/src/to/plist.rs b/crates/nu_plugin_formats/src/to/plist.rs index caf039263e..b7a15e03fe 100644 --- a/crates/nu_plugin_formats/src/to/plist.rs +++ b/crates/nu_plugin_formats/src/to/plist.rs @@ -1,10 +1,8 @@ -use std::time::SystemTime; - +use crate::FormatCmdsPlugin; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand, SimplePluginCommand}; use nu_protocol::{Category, Example, LabeledError, Record, Signature, Span, Value as NuValue}; -use plist::{Integer, Value as PlistValue}; - -use crate::FormatCmdsPlugin; +use plist::Value as PlistValue; +use std::time::SystemTime; pub(crate) struct IntoPlist; @@ -68,7 +66,7 @@ fn convert_nu_value(nu_val: &NuValue) -> Result { NuValue::String { val, .. } => Ok(PlistValue::String(val.to_owned())), NuValue::Bool { val, .. } => Ok(PlistValue::Boolean(*val)), NuValue::Float { val, .. } => Ok(PlistValue::Real(*val)), - NuValue::Int { val, .. } => Ok(PlistValue::Integer(Into::::into(*val))), + NuValue::Int { val, .. } => Ok(PlistValue::Integer((*val).into())), NuValue::Binary { val, .. } => Ok(PlistValue::Data(val.to_owned())), NuValue::Record { val, .. } => convert_nu_dict(val), NuValue::List { vals, .. } => Ok(PlistValue::Array( @@ -77,7 +75,7 @@ fn convert_nu_value(nu_val: &NuValue) -> Result { .collect::>()?, )), NuValue::Date { val, .. } => Ok(PlistValue::Date(SystemTime::from(val.to_owned()).into())), - NuValue::Filesize { val, .. } => Ok(PlistValue::Integer(Into::::into(*val))), + NuValue::Filesize { val, .. } => Ok(PlistValue::Integer(val.get().into())), _ => Err(build_label_error( format!("{:?} is not convertible", nu_val), span, diff --git a/crates/nuon/src/from.rs b/crates/nuon/src/from.rs index ded1512a6a..0c94434a1a 100644 --- a/crates/nuon/src/from.rs +++ b/crates/nuon/src/from.rs @@ -1,7 +1,7 @@ use nu_protocol::{ ast::{Expr, Expression, ListItem, RecordItem}, engine::{EngineState, StateWorkingSet}, - Range, Record, ShellError, Span, Type, Unit, Value, + Filesize, IntoValue, Range, Record, ShellError, Span, Type, Unit, Value, }; use std::sync::Arc; @@ -9,7 +9,7 @@ use std::sync::Arc; /// // WARNING: please leave the following two trailing spaces, they matter for the documentation // formatting -/// > **Note** +/// > **Note** /// > [`Span`] can be passed to [`from_nuon`] if there is context available to the caller, e.g. when /// > using this function in a command implementation such as /// > [`from nuon`](https://www.nushell.sh/commands/docs/from_nuon.html). @@ -407,32 +407,15 @@ fn convert_to_value( }; match value.unit.item { - Unit::Byte => Ok(Value::filesize(size, span)), - Unit::Kilobyte => Ok(Value::filesize(size * 1000, span)), - Unit::Megabyte => Ok(Value::filesize(size * 1000 * 1000, span)), - Unit::Gigabyte => Ok(Value::filesize(size * 1000 * 1000 * 1000, span)), - Unit::Terabyte => Ok(Value::filesize(size * 1000 * 1000 * 1000 * 1000, span)), - Unit::Petabyte => Ok(Value::filesize( - size * 1000 * 1000 * 1000 * 1000 * 1000, - span, - )), - Unit::Exabyte => Ok(Value::filesize( - size * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, - span, - )), - - Unit::Kibibyte => Ok(Value::filesize(size * 1024, span)), - Unit::Mebibyte => Ok(Value::filesize(size * 1024 * 1024, span)), - Unit::Gibibyte => Ok(Value::filesize(size * 1024 * 1024 * 1024, span)), - Unit::Tebibyte => Ok(Value::filesize(size * 1024 * 1024 * 1024 * 1024, span)), - Unit::Pebibyte => Ok(Value::filesize( - size * 1024 * 1024 * 1024 * 1024 * 1024, - span, - )), - Unit::Exbibyte => Ok(Value::filesize( - size * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, - span, - )), + Unit::Filesize(unit) => match Filesize::from_unit(size, unit) { + Some(val) => Ok(val.into_value(span)), + None => Err(ShellError::OutsideSpannedLabeledError { + src: original_text.into(), + error: "filesize too large".into(), + msg: "filesize too large".into(), + span: expr.span, + }), + }, Unit::Nanosecond => Ok(Value::duration(size, span)), Unit::Microsecond => Ok(Value::duration(size * 1000, span)),