From d79a3130b83e119408ca9a167b4a473bdda9e468 Mon Sep 17 00:00:00 2001 From: JT Date: Fri, 14 May 2021 20:35:09 +1200 Subject: [PATCH] Make the default int an i64 (#3428) * Make the default int an i64 * fmt * Fix random integer * Treat pids as i64 for now --- crates/nu-cli/src/types/deduction.rs | 1 + .../src/commands/autoview/command.rs | 6 + .../nu-command/src/commands/each/command.rs | 2 +- crates/nu-command/src/commands/echo.rs | 7 +- .../src/commands/format/format_filesize.rs | 2 +- crates/nu-command/src/commands/from_json.rs | 2 +- crates/nu-command/src/commands/into/binary.rs | 23 +- crates/nu-command/src/commands/into/int.rs | 40 +++- crates/nu-command/src/commands/into/string.rs | 26 ++- crates/nu-command/src/commands/last.rs | 4 +- crates/nu-command/src/commands/length.rs | 2 +- crates/nu-command/src/commands/math/abs.rs | 5 +- crates/nu-command/src/commands/math/avg.rs | 24 +-- crates/nu-command/src/commands/math/ceil.rs | 25 ++- crates/nu-command/src/commands/math/floor.rs | 15 +- crates/nu-command/src/commands/math/median.rs | 20 +- .../nu-command/src/commands/math/product.rs | 4 +- crates/nu-command/src/commands/math/round.rs | 18 +- crates/nu-command/src/commands/math/sqrt.rs | 14 +- crates/nu-command/src/commands/math/sum.rs | 4 +- crates/nu-command/src/commands/math/utils.rs | 6 +- .../nu-command/src/commands/math/variance.rs | 2 +- .../nu-command/src/commands/random/integer.rs | 6 +- crates/nu-command/src/commands/str_/from.rs | 27 ++- .../nu-command/src/commands/str_/index_of.rs | 4 +- crates/nu-command/src/commands/str_/length.rs | 2 +- .../src/commands/str_/to_integer.rs | 9 +- crates/nu-command/src/commands/termsize.rs | 18 +- .../src/commands/to_delimited_data.rs | 6 +- crates/nu-command/src/commands/to_json.rs | 12 +- crates/nu-command/src/commands/to_toml.rs | 8 +- crates/nu-command/src/commands/to_yaml.rs | 12 +- crates/nu-command/src/commands/uniq.rs | 4 +- crates/nu-data/src/base.rs | 37 +++- crates/nu-data/src/base/shape.rs | 15 +- crates/nu-data/src/config.rs | 8 +- crates/nu-data/src/primitive.rs | 1 + crates/nu-data/src/types.rs | 11 +- crates/nu-data/src/utils/internal.rs | 4 +- crates/nu-data/src/value.rs | 201 +++++++++++++++++- crates/nu-engine/src/deserializer.rs | 4 + crates/nu-engine/src/evaluate/evaluator.rs | 5 +- crates/nu-engine/src/from_value.rs | 9 +- crates/nu-parser/src/parse.rs | 40 ++-- crates/nu-protocol/src/hir.rs | 112 ++++++---- crates/nu-protocol/src/type_shape.rs | 4 + crates/nu-protocol/src/value.rs | 54 ++--- crates/nu-protocol/src/value/column_path.rs | 15 +- crates/nu-protocol/src/value/convert.rs | 3 +- crates/nu-protocol/src/value/debug.rs | 2 + crates/nu-protocol/src/value/primitive.rs | 26 ++- crates/nu-protocol/src/value/range.rs | 30 +++ crates/nu-test-support/src/value.rs | 6 +- crates/nu-value-ext/src/lib.rs | 2 +- crates/nu_plugin_post/src/post.rs | 12 +- crates/nu_plugin_ps/src/ps.rs | 4 +- crates/nu_plugin_sys/src/sys.rs | 2 +- crates/nu_plugin_to_bson/src/to_bson.rs | 9 +- crates/nu_plugin_to_sqlite/src/to_sqlite.rs | 1 + 59 files changed, 693 insertions(+), 284 deletions(-) diff --git a/crates/nu-cli/src/types/deduction.rs b/crates/nu-cli/src/types/deduction.rs index beee9a436..a67f5e76a 100644 --- a/crates/nu-cli/src/types/deduction.rs +++ b/crates/nu-cli/src/types/deduction.rs @@ -273,6 +273,7 @@ fn get_shape_of_expr(expr: &SpannedExpression) -> Option { Expression::Literal(literal) => { match literal { nu_protocol::hir::Literal::Number(number) => match number { + nu_protocol::hir::Number::BigInt(_) => Some(SyntaxShape::Int), nu_protocol::hir::Number::Int(_) => Some(SyntaxShape::Int), nu_protocol::hir::Number::Decimal(_) => Some(SyntaxShape::Number), }, diff --git a/crates/nu-command/src/commands/autoview/command.rs b/crates/nu-command/src/commands/autoview/command.rs index 3fce37da8..7fc9c8b8f 100644 --- a/crates/nu-command/src/commands/autoview/command.rs +++ b/crates/nu-command/src/commands/autoview/command.rs @@ -106,6 +106,12 @@ pub fn autoview(args: CommandArgs) -> Result { } => { out!("{}", n); } + Value { + value: UntaggedValue::Primitive(Primitive::BigInt(n)), + .. + } => { + out!("{}", n); + } Value { value: UntaggedValue::Primitive(Primitive::Decimal(n)), .. diff --git a/crates/nu-command/src/commands/each/command.rs b/crates/nu-command/src/commands/each/command.rs index 24aa4d6e2..1a62e1507 100644 --- a/crates/nu-command/src/commands/each/command.rs +++ b/crates/nu-command/src/commands/each/command.rs @@ -102,7 +102,7 @@ pub fn process_row( pub(crate) fn make_indexed_item(index: usize, item: Value) -> Value { let mut dict = TaggedDictBuilder::new(item.tag()); - dict.insert_untagged("index", UntaggedValue::int(index)); + dict.insert_untagged("index", UntaggedValue::int(index as i64)); dict.insert_value("item", item); dict.into_value() diff --git a/crates/nu-command/src/commands/echo.rs b/crates/nu-command/src/commands/echo.rs index e30a4acaa..e5f2f5d7c 100644 --- a/crates/nu-command/src/commands/echo.rs +++ b/crates/nu-command/src/commands/echo.rs @@ -1,5 +1,4 @@ use crate::prelude::*; -use bigdecimal::Zero; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::hir::Operator; @@ -81,7 +80,7 @@ impl RangeIterator { }; let end = match range.to.0.item { - Primitive::Nothing => Primitive::Int(u64::MAX.into()), + Primitive::Nothing => Primitive::Int(i64::MAX), x => x, }; @@ -121,11 +120,11 @@ impl Iterator for RangeIterator { ( UntaggedValue::Primitive(Primitive::Decimal(x)), UntaggedValue::Primitive(Primitive::Int(y)), - ) => x.cmp(&(BigDecimal::zero() + y)), + ) => x.cmp(&(BigDecimal::from(*y))), ( UntaggedValue::Primitive(Primitive::Int(x)), UntaggedValue::Primitive(Primitive::Decimal(y)), - ) => (BigDecimal::zero() + x).cmp(y), + ) => (BigDecimal::from(*x)).cmp(y), _ => { self.done = true; return Some( diff --git a/crates/nu-command/src/commands/format/format_filesize.rs b/crates/nu-command/src/commands/format/format_filesize.rs index ef70bae96..2f1947722 100644 --- a/crates/nu-command/src/commands/format/format_filesize.rs +++ b/crates/nu-command/src/commands/format/format_filesize.rs @@ -64,7 +64,7 @@ fn process_row( .. } = s { - let byte_format = InlineShape::format_bytes(&fs, Some(&format.item)); + let byte_format = InlineShape::format_bytes(fs, Some(&format.item)); let byte_value = Value::from(byte_format.1); Ok(input .replace_data_at_column_path(&field, byte_value) diff --git a/crates/nu-command/src/commands/from_json.rs b/crates/nu-command/src/commands/from_json.rs index 83caaa396..d48936f75 100644 --- a/crates/nu-command/src/commands/from_json.rs +++ b/crates/nu-command/src/commands/from_json.rs @@ -35,7 +35,7 @@ fn convert_json_value_to_nu_value(v: &nu_json::Value, tag: impl Into) -> Va nu_json::Value::Null => UntaggedValue::Primitive(Primitive::Nothing).into_value(&tag), nu_json::Value::Bool(b) => UntaggedValue::boolean(*b).into_value(&tag), nu_json::Value::F64(n) => UntaggedValue::decimal_from_float(*n, span).into_value(&tag), - nu_json::Value::U64(n) => UntaggedValue::int(*n).into_value(&tag), + nu_json::Value::U64(n) => UntaggedValue::big_int(*n).into_value(&tag), nu_json::Value::I64(n) => UntaggedValue::int(*n).into_value(&tag), nu_json::Value::String(s) => { UntaggedValue::Primitive(Primitive::String(String::from(s))).into_value(&tag) diff --git a/crates/nu-command/src/commands/into/binary.rs b/crates/nu-command/src/commands/into/binary.rs index 77f65ac93..66512fb99 100644 --- a/crates/nu-command/src/commands/into/binary.rs +++ b/crates/nu-command/src/commands/into/binary.rs @@ -90,14 +90,14 @@ impl WholeStreamCommand for SubCommand { description: "convert a number to a nushell binary primitive", example: "echo 1 | into binary", result: Some(vec![ - UntaggedValue::binary(BigInt::from(1).to_bytes_le().1).into() + UntaggedValue::binary(i64::from(1).to_le_bytes().to_vec()).into() ]), }, Example { description: "convert a boolean to a nushell binary primitive", example: "echo $true | into binary", result: Some(vec![ - UntaggedValue::binary(BigInt::from(1).to_bytes_le().1).into() + UntaggedValue::binary(i64::from(1).to_le_bytes().to_vec()).into() ]), }, Example { @@ -152,7 +152,17 @@ fn into_binary(args: CommandArgs) -> Result { .to_action_stream()) } -pub fn bigint_to_endian(n: &BigInt) -> Vec { +fn int_to_endian(n: i64) -> Vec { + if cfg!(target_endian = "little") { + // eprintln!("Little Endian"); + n.to_le_bytes().to_vec() + } else { + // eprintln!("Big Endian"); + n.to_be_bytes().to_vec() + } +} + +fn bigint_to_endian(n: &BigInt) -> Vec { if cfg!(target_endian = "little") { // eprintln!("Little Endian"); n.to_bytes_le().1 @@ -192,7 +202,8 @@ pub fn action( .collect() } } - Primitive::Int(n_ref) => bigint_to_endian(n_ref), + Primitive::Int(n_ref) => int_to_endian(*n_ref), + Primitive::BigInt(n_ref) => bigint_to_endian(n_ref), Primitive::Decimal(dec) => match dec.to_bigint() { Some(n) => bigint_to_endian(&n), None => { @@ -229,8 +240,8 @@ pub fn action( } } Primitive::Boolean(a_bool) => match a_bool { - false => bigint_to_endian(&BigInt::from(0)), - true => bigint_to_endian(&BigInt::from(1)), + false => int_to_endian(0), + true => int_to_endian(1), }, Primitive::Date(a_date) => a_date.format("%c").to_string().as_bytes().to_vec(), Primitive::FilePath(a_filepath) => a_filepath diff --git a/crates/nu-command/src/commands/into/int.rs b/crates/nu-command/src/commands/into/int.rs index 918b07131..28aded464 100644 --- a/crates/nu-command/src/commands/into/int.rs +++ b/crates/nu-command/src/commands/into/int.rs @@ -4,7 +4,7 @@ use nu_errors::ShellError; use nu_protocol::{ ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, }; -use num_bigint::{BigInt, ToBigInt}; +use num_bigint::ToBigInt; pub struct SubCommand; @@ -118,20 +118,34 @@ pub fn action(input: &Value, tag: impl Into) -> Result { } }, Primitive::Decimal(dec) => match dec.to_bigint() { - Some(n) => n, + Some(n) => match n.to_i64() { + Some(i) => i, + None => { + return Err(ShellError::unimplemented( + "failed to convert decimal to int", + )); + } + }, None => { return Err(ShellError::unimplemented( "failed to convert decimal to int", )); } }, - Primitive::Int(n_ref) => n_ref.to_owned(), + Primitive::Int(n_ref) => *n_ref, Primitive::Boolean(a_bool) => match a_bool { - false => BigInt::from(0), - true => BigInt::from(1), + false => 0, + true => 1, }, Primitive::Filesize(a_filesize) => match a_filesize.to_bigint() { - Some(n) => n, + Some(n) => match n.to_i64() { + Some(i) => i, + None => { + return Err(ShellError::unimplemented( + "failed to convert filesize to bigint", + )); + } + }, None => { return Err(ShellError::unimplemented( "failed to convert filesize to bigint", @@ -154,13 +168,17 @@ pub fn action(input: &Value, tag: impl Into) -> Result { } } -fn int_from_string(a_string: &str, tag: &Tag) -> Result { - match a_string.parse::() { +fn int_from_string(a_string: &str, tag: &Tag) -> Result { + match a_string.parse::() { Ok(n) => Ok(n), Err(_) => match a_string.parse::() { - Ok(res_float) => match res_float.to_bigint() { - Some(n) => Ok(n), - None => Err(ShellError::unimplemented("failed to convert f64 to int")), + Ok(f) => match f.to_i64() { + Some(i) => Ok(i), + None => Err(ShellError::labeled_error( + "Could not convert string value to int", + "original value", + tag.clone(), + )), }, Err(_) => Err(ShellError::labeled_error( "Could not convert string value to int", diff --git a/crates/nu-command/src/commands/into/string.rs b/crates/nu-command/src/commands/into/string.rs index 73a9b52e1..aa7032bc3 100644 --- a/crates/nu-command/src/commands/into/string.rs +++ b/crates/nu-command/src/commands/into/string.rs @@ -129,6 +129,13 @@ pub fn action( match &input.value { UntaggedValue::Primitive(prim) => Ok(UntaggedValue::string(match prim { Primitive::Int(int) => { + if group_digits { + format_int(*int) // int.to_formatted_string(*locale) + } else { + int.to_string() + } + } + Primitive::BigInt(int) => { if group_digits { format_bigint(int) // int.to_formatted_string(*locale) } else { @@ -141,7 +148,7 @@ pub fn action( Primitive::Date(a_date) => a_date.format("%c").to_string(), Primitive::FilePath(a_filepath) => a_filepath.as_path().display().to_string(), Primitive::Filesize(a_filesize) => { - let byte_string = InlineShape::format_bytes(a_filesize, None); + let byte_string = InlineShape::format_bytes(*a_filesize, None); byte_string.1 } _ => { @@ -162,6 +169,23 @@ pub fn action( } } +fn format_int(int: i64) -> String { + format!("{}", int) + + // TODO once platform-specific dependencies are stable (see Cargo.toml) + // #[cfg(windows)] + // { + // int.to_formatted_string(&Locale::en) + // } + // #[cfg(not(windows))] + // { + // match SystemLocale::default() { + // Ok(locale) => int.to_formatted_string(&locale), + // Err(_) => int.to_formatted_string(&Locale::en), + // } + // } +} + fn format_bigint(int: &BigInt) -> String { format!("{}", int) diff --git a/crates/nu-command/src/commands/last.rs b/crates/nu-command/src/commands/last.rs index 6538fc49b..65ccea504 100644 --- a/crates/nu-command/src/commands/last.rs +++ b/crates/nu-command/src/commands/last.rs @@ -1,7 +1,7 @@ use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; +use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; pub struct Last; @@ -31,7 +31,7 @@ impl WholeStreamCommand for Last { Example { description: "Get the last row", example: "echo [1 2 3] | last", - result: Some(vec![Value::from(UntaggedValue::from(BigInt::from(3)))]), + result: Some(vec![UntaggedValue::int(3).into()]), }, Example { description: "Get the last three rows", diff --git a/crates/nu-command/src/commands/length.rs b/crates/nu-command/src/commands/length.rs index 4dffcd8ac..732d6fff4 100644 --- a/crates/nu-command/src/commands/length.rs +++ b/crates/nu-command/src/commands/length.rs @@ -94,7 +94,7 @@ impl Iterator for CountIterator { input.count() }; - Some(UntaggedValue::int(length).into_value(self.tag.clone())) + Some(UntaggedValue::int(length as i64).into_value(self.tag.clone())) } } diff --git a/crates/nu-command/src/commands/math/abs.rs b/crates/nu-command/src/commands/math/abs.rs index bec1cb633..8f1133003 100644 --- a/crates/nu-command/src/commands/math/abs.rs +++ b/crates/nu-command/src/commands/math/abs.rs @@ -20,8 +20,9 @@ impl WholeStreamCommand for SubCommand { fn run(&self, args: CommandArgs) -> Result { let mapped = args.input.map(move |val| match val.value { - UntaggedValue::Primitive(Primitive::Int(val)) => { - UntaggedValue::int(val.magnitude().clone()).into() + UntaggedValue::Primitive(Primitive::Int(val)) => UntaggedValue::int(val.abs()).into(), + UntaggedValue::Primitive(Primitive::BigInt(val)) => { + UntaggedValue::big_int(val.magnitude().clone()).into() } UntaggedValue::Primitive(Primitive::Decimal(val)) => { UntaggedValue::decimal(val.abs()).into() diff --git a/crates/nu-command/src/commands/math/avg.rs b/crates/nu-command/src/commands/math/avg.rs index 13f7ccd74..5a93219ae 100644 --- a/crates/nu-command/src/commands/math/avg.rs +++ b/crates/nu-command/src/commands/math/avg.rs @@ -5,10 +5,7 @@ use crate::commands::math::utils::run_with_function; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{ - hir::{convert_number_to_u64, Number, Operator}, - Primitive, Signature, UntaggedValue, Value, -}; +use nu_protocol::{hir::Operator, Primitive, Signature, UntaggedValue, Value}; use bigdecimal::FromPrimitive; @@ -47,7 +44,7 @@ impl WholeStreamCommand for SubCommand { fn to_byte(value: &Value) -> Option { match &value.value { UntaggedValue::Primitive(Primitive::Int(num)) => { - Some(UntaggedValue::Primitive(Primitive::Filesize(num.clone())).into_untagged_value()) + Some(UntaggedValue::Primitive(Primitive::Filesize(*num as u64)).into_untagged_value()) } _ => None, } @@ -79,7 +76,7 @@ pub fn average(values: &[Value], name: &Tag) -> Result { Value { value: UntaggedValue::Primitive(Primitive::Filesize(num)), .. - } => UntaggedValue::int(num.clone()).into_untagged_value(), + } => UntaggedValue::int(*num as i64).into_untagged_value(), other => other.clone(), }) .collect::>(), @@ -100,15 +97,18 @@ pub fn average(values: &[Value], name: &Tag) -> Result { value: UntaggedValue::Primitive(Primitive::Filesize(num)), .. } => { - let left = UntaggedValue::from(Primitive::Int(num)); + let left = UntaggedValue::from(Primitive::Int(num as i64)); let result = nu_data::value::compute_values(Operator::Divide, &left, &total_rows); match result { - Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) => { - let number = Number::Decimal(result); - let number = convert_number_to_u64(&number); - Ok(UntaggedValue::filesize(number).into_value(name)) - } + Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) => match result.to_u64() { + Some(number) => Ok(UntaggedValue::filesize(number).into_value(name)), + None => Err(ShellError::labeled_error( + "could not calculate average of non-integer or unrelated types", + "source", + name, + )), + }, Ok(_) => Err(ShellError::labeled_error( "could not calculate average of non-integer or unrelated types", "source", diff --git a/crates/nu-command/src/commands/math/ceil.rs b/crates/nu-command/src/commands/math/ceil.rs index c981592a5..fe54c4434 100644 --- a/crates/nu-command/src/commands/math/ceil.rs +++ b/crates/nu-command/src/commands/math/ceil.rs @@ -23,7 +23,13 @@ impl WholeStreamCommand for SubCommand { fn run(&self, args: CommandArgs) -> Result { let input = args.input; - run_with_numerical_functions_on_stream(input, ceil_big_int, ceil_big_decimal, ceil_default) + run_with_numerical_functions_on_stream( + input, + ceil_int, + ceil_big_int, + ceil_big_decimal, + ceil_default, + ) } fn examples(&self) -> Vec { @@ -39,17 +45,28 @@ impl WholeStreamCommand for SubCommand { } } -fn ceil_big_int(val: BigInt) -> Value { +fn ceil_int(val: i64) -> Value { UntaggedValue::int(val).into() } +fn ceil_big_int(val: BigInt) -> Value { + UntaggedValue::big_int(val).into() +} + fn ceil_big_decimal(val: BigDecimal) -> Value { let mut maybe_ceiled = val.round(0); if maybe_ceiled < val { maybe_ceiled += BigDecimal::one(); } - let (ceiled, _) = maybe_ceiled.into_bigint_and_exponent(); - UntaggedValue::int(ceiled).into() + let ceiling = maybe_ceiled.to_i64(); + + match ceiling { + Some(x) => UntaggedValue::int(x).into(), + None => UntaggedValue::Error(ShellError::untagged_runtime_error( + "Value too big to ceiling to an 64-bit integer", + )) + .into(), + } } fn ceil_default(_: UntaggedValue) -> Value { diff --git a/crates/nu-command/src/commands/math/floor.rs b/crates/nu-command/src/commands/math/floor.rs index d5da9f6a0..3ec9ebf21 100644 --- a/crates/nu-command/src/commands/math/floor.rs +++ b/crates/nu-command/src/commands/math/floor.rs @@ -25,6 +25,7 @@ impl WholeStreamCommand for SubCommand { run_with_numerical_functions_on_stream( input, + floor_int, floor_big_int, floor_big_decimal, floor_default, @@ -36,25 +37,29 @@ impl WholeStreamCommand for SubCommand { description: "Apply the floor function to a list of numbers", example: "echo [1.5 2.3 -3.1] | math floor", result: Some(vec![ - UntaggedValue::int(1).into(), - UntaggedValue::int(2).into(), - UntaggedValue::int(-4).into(), + UntaggedValue::big_int(1).into(), + UntaggedValue::big_int(2).into(), + UntaggedValue::big_int(-4).into(), ]), }] } } -fn floor_big_int(val: BigInt) -> Value { +fn floor_int(val: i64) -> Value { UntaggedValue::int(val).into() } +fn floor_big_int(val: BigInt) -> Value { + UntaggedValue::big_int(val).into() +} + fn floor_big_decimal(val: BigDecimal) -> Value { let mut maybe_floored = val.round(0); if maybe_floored > val { maybe_floored -= BigDecimal::one(); } let (floored, _) = maybe_floored.into_bigint_and_exponent(); - UntaggedValue::int(floored).into() + UntaggedValue::big_int(floored).into() } fn floor_default(_: UntaggedValue) -> Value { diff --git a/crates/nu-command/src/commands/math/median.rs b/crates/nu-command/src/commands/math/median.rs index de7a4a9b7..013a8f10a 100644 --- a/crates/nu-command/src/commands/math/median.rs +++ b/crates/nu-command/src/commands/math/median.rs @@ -4,10 +4,7 @@ use crate::prelude::*; use bigdecimal::FromPrimitive; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{ - hir::{convert_number_to_u64, Number, Operator}, - Primitive, Signature, UntaggedValue, Value, -}; +use nu_protocol::{hir::Operator, Primitive, Signature, UntaggedValue, Value}; pub struct SubCommand; @@ -124,14 +121,21 @@ fn compute_average(values: &[Value], name: impl Into) -> Result { - let left = UntaggedValue::from(Primitive::Int(num)); + let left = UntaggedValue::from(Primitive::Int(num as i64)); let result = nu_data::value::compute_values(Operator::Divide, &left, &total_rows); match result { Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) => { - let number = Number::Decimal(result); - let number = convert_number_to_u64(&number); - Ok(UntaggedValue::filesize(number).into_value(name)) + let (bi, _) = result.as_bigint_and_exponent(); + let number = bi.to_u64(); + match number { + Some(number) => Ok(UntaggedValue::filesize(number).into_value(name)), + None => Err(ShellError::labeled_error( + "Can't convert to filesize", + "can't convert to filesize", + name, + )), + } } Ok(_) => Err(ShellError::labeled_error( "could not calculate median of non-numeric or unrelated types", diff --git a/crates/nu-command/src/commands/math/product.rs b/crates/nu-command/src/commands/math/product.rs index 62decd9ba..880eb2920 100644 --- a/crates/nu-command/src/commands/math/product.rs +++ b/crates/nu-command/src/commands/math/product.rs @@ -36,7 +36,7 @@ impl WholeStreamCommand for SubCommand { fn to_byte(value: &Value) -> Option { match &value.value { UntaggedValue::Primitive(Primitive::Int(num)) => { - Some(UntaggedValue::Primitive(Primitive::Filesize(num.clone())).into_untagged_value()) + Some(UntaggedValue::Primitive(Primitive::Filesize(*num as u64)).into_untagged_value()) } _ => None, } @@ -59,7 +59,7 @@ pub fn product(values: &[Value], name: &Tag) -> Result { Value { value: UntaggedValue::Primitive(Primitive::Filesize(num)), .. - } => UntaggedValue::int(num.clone()).into_untagged_value(), + } => UntaggedValue::int(*num as i64).into_untagged_value(), other => other.clone(), }) .collect::>(), diff --git a/crates/nu-command/src/commands/math/round.rs b/crates/nu-command/src/commands/math/round.rs index ff6a69871..0201b3bd4 100644 --- a/crates/nu-command/src/commands/math/round.rs +++ b/crates/nu-command/src/commands/math/round.rs @@ -62,7 +62,7 @@ fn operate(args: CommandArgs) -> Result { 0 }; let mapped = input.map(move |val| match val.value { - UntaggedValue::Primitive(Primitive::Int(val)) => round_big_int(val), + UntaggedValue::Primitive(Primitive::BigInt(val)) => round_big_int(val), UntaggedValue::Primitive(Primitive::Decimal(val)) => { round_big_decimal(val, precision.into()) } @@ -72,18 +72,22 @@ fn operate(args: CommandArgs) -> Result { } fn round_big_int(val: BigInt) -> Value { - UntaggedValue::int(val).into() + UntaggedValue::big_int(val).into() } fn round_big_decimal(val: BigDecimal, precision: i64) -> Value { if precision > 0 { UntaggedValue::decimal(val.with_scale(precision + 1).round(precision)).into() } else { - let (rounded, _) = val - .with_scale(precision + 1) - .round(precision) - .as_bigint_and_exponent(); - UntaggedValue::int(rounded).into() + let rounded = val.with_scale(precision + 1).round(precision).to_i64(); + + match rounded { + Some(x) => UntaggedValue::int(x).into(), + None => UntaggedValue::Error(ShellError::untagged_runtime_error( + "Number too larger to round to 64-bit int", + )) + .into(), + } } } diff --git a/crates/nu-command/src/commands/math/sqrt.rs b/crates/nu-command/src/commands/math/sqrt.rs index 92449a3b0..1aa89b2eb 100644 --- a/crates/nu-command/src/commands/math/sqrt.rs +++ b/crates/nu-command/src/commands/math/sqrt.rs @@ -46,10 +46,18 @@ fn operate(args: CommandArgs) -> OutputStream { fn sqrt_big_decimal(val: BigDecimal) -> Value { let squared = val.sqrt(); match squared { - None => UntaggedValue::Error(ShellError::unexpected("Cant square root a negative number")) - .into(), + None => UntaggedValue::Error(ShellError::untagged_runtime_error( + "Can't square root a negative number", + )) + .into(), Some(val) if !val.is_integer() => UntaggedValue::decimal(val.normalized()).into(), - Some(val) => UntaggedValue::int(val.with_scale(0).as_bigint_and_exponent().0).into(), + Some(val) => match val.to_i64() { + Some(x) => UntaggedValue::int(x).into(), + None => UntaggedValue::Error(ShellError::untagged_runtime_error( + "Value too large to convert to 64-bit integer", + )) + .into(), + }, } } diff --git a/crates/nu-command/src/commands/math/sum.rs b/crates/nu-command/src/commands/math/sum.rs index 0f09a1983..30ba8c22a 100644 --- a/crates/nu-command/src/commands/math/sum.rs +++ b/crates/nu-command/src/commands/math/sum.rs @@ -44,7 +44,7 @@ impl WholeStreamCommand for SubCommand { fn to_byte(value: &Value) -> Option { match &value.value { UntaggedValue::Primitive(Primitive::Int(num)) => { - Some(UntaggedValue::Primitive(Primitive::Filesize(num.clone())).into_untagged_value()) + Some(UntaggedValue::Primitive(Primitive::Filesize(*num as u64)).into_untagged_value()) } _ => None, } @@ -71,7 +71,7 @@ pub fn summation(values: &[Value], name: &Tag) -> Result { Value { value: UntaggedValue::Primitive(Primitive::Filesize(num)), .. - } => UntaggedValue::int(num.clone()).into_untagged_value(), + } => UntaggedValue::int(*num as i64).into_untagged_value(), other => other.clone(), }) .collect::>(), diff --git a/crates/nu-command/src/commands/math/utils.rs b/crates/nu-command/src/commands/math/utils.rs index 49378a662..fd5d082e6 100644 --- a/crates/nu-command/src/commands/math/utils.rs +++ b/crates/nu-command/src/commands/math/utils.rs @@ -34,7 +34,9 @@ pub fn run_with_function( } } -pub type IntFunction = fn(val: BigInt) -> Value; +pub type BigIntFunction = fn(val: BigInt) -> Value; + +pub type IntFunction = fn(val: i64) -> Value; pub type DecimalFunction = fn(val: BigDecimal) -> Value; @@ -43,11 +45,13 @@ pub type DefaultFunction = fn(val: UntaggedValue) -> Value; pub fn run_with_numerical_functions_on_stream( input: InputStream, int_function: IntFunction, + big_int_function: BigIntFunction, decimal_function: DecimalFunction, default_function: DefaultFunction, ) -> Result { let mapped = input.map(move |val| match val.value { UntaggedValue::Primitive(Primitive::Int(val)) => int_function(val), + UntaggedValue::Primitive(Primitive::BigInt(val)) => big_int_function(val), UntaggedValue::Primitive(Primitive::Decimal(val)) => decimal_function(val), other => default_function(other), }); diff --git a/crates/nu-command/src/commands/math/variance.rs b/crates/nu-command/src/commands/math/variance.rs index 7e991420d..f68702a75 100644 --- a/crates/nu-command/src/commands/math/variance.rs +++ b/crates/nu-command/src/commands/math/variance.rs @@ -120,7 +120,7 @@ fn sum_of_squares(values: &[Value], name: &Tag) -> Result { value: UntaggedValue::Primitive(Primitive::Filesize(num)), .. } => { - UntaggedValue::from(Primitive::Int(num.clone())) + UntaggedValue::from(Primitive::Int(*num as i64)) }, Value { value: UntaggedValue::Primitive(num), diff --git a/crates/nu-command/src/commands/random/integer.rs b/crates/nu-command/src/commands/random/integer.rs index a4ef074c7..af1bd6ebc 100644 --- a/crates/nu-command/src/commands/random/integer.rs +++ b/crates/nu-command/src/commands/random/integer.rs @@ -62,9 +62,9 @@ pub fn integer(args: CommandArgs) -> Result { }; let (min, max) = if let Some(range) = &cmd_args.range { - (range.min_u64()?, range.max_u64()?) + (range.min_i64()?, range.max_i64()?) } else { - (0, u64::MAX) + (0, i64::MAX) }; match min.cmp(&max) { @@ -83,7 +83,7 @@ pub fn integer(args: CommandArgs) -> Result { let mut thread_rng = thread_rng(); // add 1 to max, because gen_range is right-exclusive let max = max.saturating_add(1); - let result: u64 = thread_rng.gen_range(min, max); + let result: i64 = thread_rng.gen_range(min, max); Ok(OutputStream::one( UntaggedValue::int(result).into_value(Tag::unknown()), diff --git a/crates/nu-command/src/commands/str_/from.rs b/crates/nu-command/src/commands/str_/from.rs index bdfa95ac7..e9045e722 100644 --- a/crates/nu-command/src/commands/str_/from.rs +++ b/crates/nu-command/src/commands/str_/from.rs @@ -109,6 +109,14 @@ pub fn action( match &input.value { UntaggedValue::Primitive(prim) => Ok(UntaggedValue::string(match prim { Primitive::Int(int) => { + if group_digits { + format_int(*int) // int.to_formatted_string(*locale) + } else { + int.to_string() + } + } + + Primitive::BigInt(int) => { if group_digits { format_bigint(int) // int.to_formatted_string(*locale) } else { @@ -121,7 +129,7 @@ pub fn action( Primitive::Date(a_date) => a_date.format("%c").to_string(), Primitive::FilePath(a_filepath) => a_filepath.as_path().display().to_string(), Primitive::Filesize(a_filesize) => { - let byte_string = InlineShape::format_bytes(a_filesize, None); + let byte_string = InlineShape::format_bytes(*a_filesize, None); byte_string.1 } _ => { @@ -142,6 +150,23 @@ pub fn action( } } +fn format_int(int: i64) -> String { + format!("{}", int) + + // TODO once platform-specific dependencies are stable (see Cargo.toml) + // #[cfg(windows)] + // { + // int.to_formatted_string(&Locale::en) + // } + // #[cfg(not(windows))] + // { + // match SystemLocale::default() { + // Ok(locale) => int.to_formatted_string(&locale), + // Err(_) => int.to_formatted_string(&Locale::en), + // } + // } +} + fn format_bigint(int: &BigInt) -> String { format!("{}", int) diff --git a/crates/nu-command/src/commands/str_/index_of.rs b/crates/nu-command/src/commands/str_/index_of.rs index 035d0af4c..551352df0 100644 --- a/crates/nu-command/src/commands/str_/index_of.rs +++ b/crates/nu-command/src/commands/str_/index_of.rs @@ -147,12 +147,12 @@ fn action( if *end { if let Some(result) = s[start_index..end_index].rfind(&**pattern) { - Ok(UntaggedValue::int(result + start_index).into_value(tag)) + Ok(UntaggedValue::int(result as i64 + start_index as i64).into_value(tag)) } else { Ok(UntaggedValue::int(-1).into_value(tag)) } } else if let Some(result) = s[start_index..end_index].find(&**pattern) { - Ok(UntaggedValue::int(result + start_index).into_value(tag)) + Ok(UntaggedValue::int(result as i64 + start_index as i64).into_value(tag)) } else { Ok(UntaggedValue::int(-1).into_value(tag)) } diff --git a/crates/nu-command/src/commands/str_/length.rs b/crates/nu-command/src/commands/str_/length.rs index d19fba3c1..504aae91b 100644 --- a/crates/nu-command/src/commands/str_/length.rs +++ b/crates/nu-command/src/commands/str_/length.rs @@ -81,7 +81,7 @@ fn operate(args: CommandArgs) -> Result { fn action(input: &Value, tag: impl Into) -> Result { match &input.value { UntaggedValue::Primitive(Primitive::String(s)) => { - Ok(UntaggedValue::int(s.len()).into_value(tag)) + Ok(UntaggedValue::int(s.len() as i64).into_value(tag)) } other => { let got = format!("got {}", other.type_name()); diff --git a/crates/nu-command/src/commands/str_/to_integer.rs b/crates/nu-command/src/commands/str_/to_integer.rs index ae0e23073..d70c86d44 100644 --- a/crates/nu-command/src/commands/str_/to_integer.rs +++ b/crates/nu-command/src/commands/str_/to_integer.rs @@ -9,9 +9,6 @@ use nu_protocol::{ use nu_source::{Tag, Tagged}; use nu_value_ext::ValueExt; -use num_bigint::BigInt; -use num_traits::Num; - struct Arguments { radix: Option>, column_paths: Vec, @@ -106,7 +103,7 @@ fn action(input: &Value, tag: impl Into, radix: u32) -> Result { - let num = match BigInt::from_str_radix(b.trim_start_matches("0b"), 2) { + let num = match i64::from_str_radix(b.trim_start_matches("0b"), 2) { Ok(n) => n, Err(reason) => { return Err(ShellError::labeled_error( @@ -119,7 +116,7 @@ fn action(input: &Value, tag: impl Into, radix: u32) -> Result { - let num = match BigInt::from_str_radix(h.trim_start_matches("0x"), 16) { + let num = match i64::from_str_radix(h.trim_start_matches("0x"), 16) { Ok(n) => n, Err(reason) => { return Err(ShellError::labeled_error( @@ -132,7 +129,7 @@ fn action(input: &Value, tag: impl Into, radix: u32) -> Result { - let num = match BigInt::from_str_radix(trimmed, radix) { + let num = match i64::from_str_radix(trimmed, radix) { Ok(n) => n, Err(reason) => { return Err(ShellError::labeled_error( diff --git a/crates/nu-command/src/commands/termsize.rs b/crates/nu-command/src/commands/termsize.rs index 559c2077a..ebabc8f93 100644 --- a/crates/nu-command/src/commands/termsize.rs +++ b/crates/nu-command/src/commands/termsize.rs @@ -35,13 +35,23 @@ impl WholeStreamCommand for TermSize { match size { Some((w, h)) => { if wide && !tall { - Ok(ActionStream::one(UntaggedValue::int(w).into_value(tag))) + Ok(ActionStream::one( + UntaggedValue::int(w as i64).into_value(tag), + )) } else if !wide && tall { - Ok(ActionStream::one(UntaggedValue::int(h).into_value(tag))) + Ok(ActionStream::one( + UntaggedValue::int(h as i64).into_value(tag), + )) } else { let mut indexmap = IndexMap::with_capacity(2); - indexmap.insert("width".to_string(), UntaggedValue::int(w).into_value(&tag)); - indexmap.insert("height".to_string(), UntaggedValue::int(h).into_value(&tag)); + indexmap.insert( + "width".to_string(), + UntaggedValue::int(w as i64).into_value(&tag), + ); + indexmap.insert( + "height".to_string(), + UntaggedValue::int(h as i64).into_value(&tag), + ); let value = UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag); Ok(ActionStream::one(value)) } diff --git a/crates/nu-command/src/commands/to_delimited_data.rs b/crates/nu-command/src/commands/to_delimited_data.rs index b121393d7..19d2703ff 100644 --- a/crates/nu-command/src/commands/to_delimited_data.rs +++ b/crates/nu-command/src/commands/to_delimited_data.rs @@ -109,14 +109,12 @@ pub fn clone_tagged_value(v: &Value) -> Value { UntaggedValue::Primitive(Primitive::Decimal(f)) => { UntaggedValue::Primitive(Primitive::Decimal(f.clone())) } - UntaggedValue::Primitive(Primitive::Int(i)) => { - UntaggedValue::Primitive(Primitive::Int(i.clone())) - } + UntaggedValue::Primitive(Primitive::Int(i)) => UntaggedValue::Primitive(Primitive::Int(*i)), UntaggedValue::Primitive(Primitive::FilePath(x)) => { UntaggedValue::Primitive(Primitive::FilePath(x.clone())) } UntaggedValue::Primitive(Primitive::Filesize(b)) => { - UntaggedValue::Primitive(Primitive::Filesize(b.clone())) + UntaggedValue::Primitive(Primitive::Filesize(*b)) } UntaggedValue::Primitive(Primitive::Date(d)) => { UntaggedValue::Primitive(Primitive::Date(*d)) diff --git a/crates/nu-command/src/commands/to_json.rs b/crates/nu-command/src/commands/to_json.rs index 46630dc14..7317ab251 100644 --- a/crates/nu-command/src/commands/to_json.rs +++ b/crates/nu-command/src/commands/to_json.rs @@ -82,6 +82,9 @@ pub fn value_to_json_value(v: &Value) -> Result { } UntaggedValue::Primitive(Primitive::Int(i)) => { + serde_json::Value::Number(serde_json::Number::from(*i)) + } + UntaggedValue::Primitive(Primitive::BigInt(i)) => { serde_json::Value::Number(serde_json::Number::from(CoerceInto::::coerce_into( i.tagged(&v.tag), "converting to JSON number", @@ -96,12 +99,9 @@ pub fn value_to_json_value(v: &Value) -> Result { UnspannedPathMember::String(string) => { Ok(serde_json::Value::String(string.clone())) } - UnspannedPathMember::Int(int) => Ok(serde_json::Value::Number( - serde_json::Number::from(CoerceInto::::coerce_into( - int.tagged(&v.tag), - "converting to JSON number", - )?), - )), + UnspannedPathMember::Int(int) => { + Ok(serde_json::Value::Number(serde_json::Number::from(*int))) + } }) .collect::, ShellError>>()?, ), diff --git a/crates/nu-command/src/commands/to_toml.rs b/crates/nu-command/src/commands/to_toml.rs index 741a6c88a..6c92ab9c6 100644 --- a/crates/nu-command/src/commands/to_toml.rs +++ b/crates/nu-command/src/commands/to_toml.rs @@ -50,7 +50,8 @@ fn helper(v: &Value) -> Result { UntaggedValue::Primitive(Primitive::Decimal(f)) => { toml::Value::Float(f.tagged(&v.tag).coerce_into("converting to TOML float")?) } - UntaggedValue::Primitive(Primitive::Int(i)) => { + UntaggedValue::Primitive(Primitive::Int(i)) => toml::Value::Integer(*i), + UntaggedValue::Primitive(Primitive::BigInt(i)) => { toml::Value::Integer(i.tagged(&v.tag).coerce_into("converting to TOML integer")?) } UntaggedValue::Primitive(Primitive::Nothing) => { @@ -65,10 +66,7 @@ fn helper(v: &Value) -> Result { path.iter() .map(|x| match &x.unspanned { UnspannedPathMember::String(string) => Ok(toml::Value::String(string.clone())), - UnspannedPathMember::Int(int) => Ok(toml::Value::Integer( - int.tagged(&v.tag) - .coerce_into("converting to TOML integer")?, - )), + UnspannedPathMember::Int(int) => Ok(toml::Value::Integer(*int)), }) .collect::, ShellError>>()?, ), diff --git a/crates/nu-command/src/commands/to_yaml.rs b/crates/nu-command/src/commands/to_yaml.rs index bd7a9bcec..fd15e9c5d 100644 --- a/crates/nu-command/src/commands/to_yaml.rs +++ b/crates/nu-command/src/commands/to_yaml.rs @@ -51,6 +51,9 @@ pub fn value_to_yaml_value(v: &Value) -> Result { })?)) } UntaggedValue::Primitive(Primitive::Int(i)) => { + serde_yaml::Value::Number(serde_yaml::Number::from(*i)) + } + UntaggedValue::Primitive(Primitive::BigInt(i)) => { serde_yaml::Value::Number(serde_yaml::Number::from(CoerceInto::::coerce_into( i.tagged(&v.tag), "converting to YAML number", @@ -67,12 +70,9 @@ pub fn value_to_yaml_value(v: &Value) -> Result { UnspannedPathMember::String(string) => { out.push(serde_yaml::Value::String(string.clone())) } - UnspannedPathMember::Int(int) => out.push(serde_yaml::Value::Number( - serde_yaml::Number::from(CoerceInto::::coerce_into( - int.tagged(&member.span), - "converting to YAML number", - )?), - )), + UnspannedPathMember::Int(int) => { + out.push(serde_yaml::Value::Number(serde_yaml::Number::from(*int))) + } } } diff --git a/crates/nu-command/src/commands/uniq.rs b/crates/nu-command/src/commands/uniq.rs index 69cebbd79..9a608765c 100644 --- a/crates/nu-command/src/commands/uniq.rs +++ b/crates/nu-command/src/commands/uniq.rs @@ -126,7 +126,7 @@ fn uniq(args: CommandArgs) -> Result { UntaggedValue::Row(mut row) => { row.entries.insert( "count".to_string(), - UntaggedValue::int(item.1).into_untagged_value(), + UntaggedValue::int(item.1 as i64).into_untagged_value(), ); Value { value: UntaggedValue::Row(row), @@ -141,7 +141,7 @@ fn uniq(args: CommandArgs) -> Result { ); map.insert( "count".to_string(), - UntaggedValue::int(item.1).into_untagged_value(), + UntaggedValue::int(item.1 as i64).into_untagged_value(), ); Value { value: UntaggedValue::row(map), diff --git a/crates/nu-data/src/base.rs b/crates/nu-data/src/base.rs index 001895b7d..0f020ed3f 100644 --- a/crates/nu-data/src/base.rs +++ b/crates/nu-data/src/base.rs @@ -10,7 +10,6 @@ use nu_protocol::{ use nu_source::{Span, Tag}; use nu_value_ext::ValueExt; use num_bigint::BigInt; -use num_traits::Zero; use serde::{Deserialize, Serialize}; #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, new, Serialize)] @@ -72,7 +71,9 @@ pub fn reject_fields(obj: &Value, fields: &[String], tag: impl Into) -> Val } pub enum CompareValues { - Ints(BigInt, BigInt), + Ints(i64, i64), + Filesizes(u64, u64), + BigInts(BigInt, BigInt), Decimals(BigDecimal, BigDecimal), String(String, String), Date(DateTime, DateTime), @@ -83,7 +84,9 @@ pub enum CompareValues { impl CompareValues { pub fn compare(&self) -> std::cmp::Ordering { match self { + CompareValues::BigInts(left, right) => left.cmp(right), CompareValues::Ints(left, right) => left.cmp(right), + CompareValues::Filesizes(left, right) => left.cmp(right), CompareValues::Decimals(left, right) => left.cmp(right), CompareValues::String(left, right) => left.cmp(right), CompareValues::Date(left, right) => left.cmp(right), @@ -125,22 +128,34 @@ pub fn coerce_compare_primitive( use Primitive::*; Ok(match (left, right) { - (Int(left), Int(right)) => CompareValues::Ints(left.clone(), right.clone()), - (Int(left), Decimal(right)) => { - CompareValues::Decimals(BigDecimal::zero() + left, right.clone()) + (Int(left), Int(right)) => CompareValues::Ints(*left, *right), + (Int(left), BigInt(right)) => { + CompareValues::BigInts(num_bigint::BigInt::from(*left), right.clone()) + } + (BigInt(left), Int(right)) => { + CompareValues::BigInts(left.clone(), num_bigint::BigInt::from(*right)) + } + (BigInt(left), BigInt(right)) => CompareValues::BigInts(left.clone(), right.clone()), + + (Int(left), Decimal(right)) => { + CompareValues::Decimals(BigDecimal::from(*left), right.clone()) + } + (BigInt(left), Decimal(right)) => { + CompareValues::Decimals(BigDecimal::from(left.clone()), right.clone()) } - (Int(left), Filesize(right)) => CompareValues::Ints(left.clone(), right.clone()), (Decimal(left), Decimal(right)) => CompareValues::Decimals(left.clone(), right.clone()), (Decimal(left), Int(right)) => { - CompareValues::Decimals(left.clone(), BigDecimal::zero() + right) + CompareValues::Decimals(left.clone(), BigDecimal::from(*right)) } - (Decimal(left), Filesize(right)) => { + (Decimal(left), BigInt(right)) => { CompareValues::Decimals(left.clone(), BigDecimal::from(right.clone())) } - (Filesize(left), Filesize(right)) => CompareValues::Ints(left.clone(), right.clone()), - (Filesize(left), Int(right)) => CompareValues::Ints(left.clone(), right.clone()), + (Decimal(left), Filesize(right)) => { + CompareValues::Decimals(left.clone(), BigDecimal::from(*right)) + } + (Filesize(left), Filesize(right)) => CompareValues::Filesizes(*left, *right), (Filesize(left), Decimal(right)) => { - CompareValues::Decimals(BigDecimal::from(left.clone()), right.clone()) + CompareValues::Decimals(BigDecimal::from(*left), right.clone()) } (Nothing, Nothing) => CompareValues::Booleans(true, true), (String(left), String(right)) => CompareValues::String(left.clone(), right.clone()), diff --git a/crates/nu-data/src/base/shape.rs b/crates/nu-data/src/base/shape.rs index 9afbe09f0..0f530c384 100644 --- a/crates/nu-data/src/base/shape.rs +++ b/crates/nu-data/src/base/shape.rs @@ -21,10 +21,11 @@ pub struct InlineRange { #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)] pub enum InlineShape { Nothing, - Int(BigInt), + Int(i64), + BigInt(BigInt), Decimal(BigDecimal), Range(Box), - Bytesize(BigInt), + Bytesize(u64), String(String), Line(String), ColumnPath(ColumnPath), @@ -74,7 +75,8 @@ impl InlineShape { pub fn from_primitive(primitive: &Primitive) -> InlineShape { match primitive { Primitive::Nothing => InlineShape::Nothing, - Primitive::Int(int) => InlineShape::Int(int.clone()), + Primitive::Int(int) => InlineShape::Int(*int), + Primitive::BigInt(int) => InlineShape::BigInt(int.clone()), Primitive::Range(range) => { let (left, left_inclusion) = &range.from; let (right, right_inclusion) = &range.to; @@ -85,7 +87,7 @@ impl InlineShape { })) } Primitive::Decimal(decimal) => InlineShape::Decimal(decimal.clone()), - Primitive::Filesize(bytesize) => InlineShape::Bytesize(bytesize.clone()), + Primitive::Filesize(bytesize) => InlineShape::Bytesize(*bytesize), Primitive::String(string) => InlineShape::String(string.clone()), Primitive::ColumnPath(path) => InlineShape::ColumnPath(path.clone()), Primitive::GlobPattern(pattern) => InlineShape::GlobPattern(pattern.clone()), @@ -147,7 +149,7 @@ impl InlineShape { } } - pub fn format_bytes(bytesize: &BigInt, forced_format: Option<&str>) -> (DbgDocBldr, String) { + pub fn format_bytes(bytesize: u64, forced_format: Option<&str>) -> (DbgDocBldr, String) { use bigdecimal::ToPrimitive; // get the config value, if it doesn't exist make it 'auto' so it works how it originally did @@ -236,6 +238,7 @@ impl PrettyDebug for FormatInlineShape { match &self.shape { InlineShape::Nothing => DbgDocBldr::blank(), InlineShape::Int(int) => DbgDocBldr::primitive(format!("{}", int)), + InlineShape::BigInt(int) => DbgDocBldr::primitive(format!("{}", int)), InlineShape::Decimal(decimal) => DbgDocBldr::description(format_primitive( &Primitive::Decimal(decimal.clone()), None, @@ -258,7 +261,7 @@ impl PrettyDebug for FormatInlineShape { + right.clone().format().pretty() } InlineShape::Bytesize(bytesize) => { - let bytes = InlineShape::format_bytes(bytesize, None); + let bytes = InlineShape::format_bytes(*bytesize, None); bytes.0 } InlineShape::String(string) => DbgDocBldr::primitive(string), diff --git a/crates/nu-data/src/config.rs b/crates/nu-data/src/config.rs index 0667d6c84..6848d105a 100644 --- a/crates/nu-data/src/config.rs +++ b/crates/nu-data/src/config.rs @@ -93,7 +93,8 @@ fn helper(v: &Value) -> Result { UntaggedValue::Primitive(Primitive::Decimal(f)) => { toml::Value::Float(f.tagged(&v.tag).coerce_into("converting to TOML float")?) } - UntaggedValue::Primitive(Primitive::Int(i)) => { + UntaggedValue::Primitive(Primitive::Int(i)) => toml::Value::Integer(*i), + UntaggedValue::Primitive(Primitive::BigInt(i)) => { toml::Value::Integer(i.tagged(&v.tag).coerce_into("converting to TOML integer")?) } UntaggedValue::Primitive(Primitive::Nothing) => { @@ -108,10 +109,7 @@ fn helper(v: &Value) -> Result { path.iter() .map(|x| match &x.unspanned { UnspannedPathMember::String(string) => Ok(toml::Value::String(string.clone())), - UnspannedPathMember::Int(int) => Ok(toml::Value::Integer( - int.tagged(&v.tag) - .coerce_into("converting to TOML integer")?, - )), + UnspannedPathMember::Int(int) => Ok(toml::Value::Integer(*int)), }) .collect::, ShellError>>()?, ), diff --git a/crates/nu-data/src/primitive.rs b/crates/nu-data/src/primitive.rs index 8842c83db..d4940b3a4 100644 --- a/crates/nu-data/src/primitive.rs +++ b/crates/nu-data/src/primitive.rs @@ -8,6 +8,7 @@ pub fn number(number: impl Into) -> Primitive { let number = number.into(); match number { + Number::BigInt(int) => Primitive::BigInt(int), Number::Int(int) => Primitive::Int(int), Number::Decimal(decimal) => Primitive::Decimal(decimal), } diff --git a/crates/nu-data/src/types.rs b/crates/nu-data/src/types.rs index 2236a6e69..cd91e0c86 100644 --- a/crates/nu-data/src/types.rs +++ b/crates/nu-data/src/types.rs @@ -52,6 +52,10 @@ impl ExtractType for i64 { &Value { value: UntaggedValue::Primitive(Primitive::Int(int)), .. + } => Ok(*int), + &Value { + value: UntaggedValue::Primitive(Primitive::BigInt(int)), + .. } => Ok(int.tagged(&value.tag).coerce_into("converting to i64")?), other => Err(ShellError::type_error("Integer", other.spanned_type_name())), } @@ -64,10 +68,13 @@ impl ExtractType for u64 { match &value { &Value { - value: UntaggedValue::Primitive(Primitive::Int(int)), + value: UntaggedValue::Primitive(Primitive::BigInt(int)), .. } => Ok(int.tagged(&value.tag).coerce_into("converting to u64")?), - other => Err(ShellError::type_error("Integer", other.spanned_type_name())), + other => Err(ShellError::type_error( + "Big Integer", + other.spanned_type_name(), + )), } } } diff --git a/crates/nu-data/src/utils/internal.rs b/crates/nu-data/src/utils/internal.rs index 8ab874430..c1ec0feca 100644 --- a/crates/nu-data/src/utils/internal.rs +++ b/crates/nu-data/src/utils/internal.rs @@ -28,7 +28,7 @@ impl Labels { } pub fn grouping_total(&self) -> Value { - UntaggedValue::int(self.x.len()).into_untagged_value() + UntaggedValue::int(self.x.len() as i64).into_untagged_value() } pub fn splits(&self) -> impl Iterator { @@ -36,7 +36,7 @@ impl Labels { } pub fn splits_total(&self) -> Value { - UntaggedValue::int(self.y.len()).into_untagged_value() + UntaggedValue::big_int(self.y.len()).into_untagged_value() } } diff --git a/crates/nu-data/src/value.rs b/crates/nu-data/src/value.rs index 2948c8718..43aee2159 100644 --- a/crates/nu-data/src/value.rs +++ b/crates/nu-data/src/value.rs @@ -8,6 +8,7 @@ use nu_protocol::ShellTypeName; use nu_protocol::{Primitive, Type, UntaggedValue}; use nu_source::{DebugDocBuilder, PrettyDebug, Span, Tagged}; use nu_table::TextStyle; +use num_bigint::BigInt; use num_traits::{ToPrimitive, Zero}; use std::collections::HashMap; @@ -81,12 +82,18 @@ pub fn unsafe_compute_values( match (left, right) { (UntaggedValue::Primitive(lhs), UntaggedValue::Primitive(rhs)) => match (lhs, rhs) { (Primitive::Filesize(x), Primitive::Int(y)) => match operator { - Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::Filesize(x * y))), - Operator::Divide => Ok(UntaggedValue::Primitive(Primitive::Filesize(x / y))), + Operator::Multiply => { + Ok(UntaggedValue::Primitive(Primitive::Filesize(x * *y as u64))) + } + Operator::Divide => { + Ok(UntaggedValue::Primitive(Primitive::Filesize(x / *y as u64))) + } _ => Err((left.type_name(), right.type_name())), }, (Primitive::Int(x), Primitive::Filesize(y)) => match operator { - Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::Filesize(x * y))), + Operator::Multiply => { + Ok(UntaggedValue::Primitive(Primitive::Filesize(*x as u64 * y))) + } _ => Err((left.type_name(), right.type_name())), }, _ => Err((left.type_name(), right.type_name())), @@ -111,12 +118,18 @@ pub fn compute_values( Ok(UntaggedValue::Primitive(Primitive::Filesize(result))) } (Primitive::Filesize(x), Primitive::Int(y)) => match operator { - Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::Filesize(x * y))), - Operator::Divide => Ok(UntaggedValue::Primitive(Primitive::Filesize(x / y))), + Operator::Multiply => { + Ok(UntaggedValue::Primitive(Primitive::Filesize(x * *y as u64))) + } + Operator::Divide => { + Ok(UntaggedValue::Primitive(Primitive::Filesize(x / *y as u64))) + } _ => Err((left.type_name(), right.type_name())), }, (Primitive::Int(x), Primitive::Filesize(y)) => match operator { - Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::Filesize(x * y))), + Operator::Multiply => { + Ok(UntaggedValue::Primitive(Primitive::Filesize(*x as u64 * y))) + } _ => Err((left.type_name(), right.type_name())), }, (Primitive::Int(x), Primitive::Int(y)) => match operator { @@ -126,12 +139,11 @@ pub fn compute_values( Operator::Divide => { if y.is_zero() { Ok(zero_division_error()) - } else if x - (y * (x / y)) == num_bigint::BigInt::from(0) { + } else if x - (y * (x / y)) == 0 { Ok(UntaggedValue::Primitive(Primitive::Int(x / y))) } else { Ok(UntaggedValue::Primitive(Primitive::Decimal( - bigdecimal::BigDecimal::from(x.clone()) - / bigdecimal::BigDecimal::from(y.clone()), + bigdecimal::BigDecimal::from(*x) / bigdecimal::BigDecimal::from(*y), ))) } } @@ -151,7 +163,176 @@ pub fn compute_values( } _ => Err((left.type_name(), right.type_name())), }, + (Primitive::Int(x), Primitive::BigInt(y)) => match operator { + Operator::Plus => Ok(UntaggedValue::Primitive(Primitive::BigInt( + BigInt::from(*x) + y, + ))), + Operator::Minus => Ok(UntaggedValue::Primitive(Primitive::BigInt( + BigInt::from(*x) - y, + ))), + Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::BigInt( + BigInt::from(*x) * y, + ))), + Operator::Divide => { + if y.is_zero() { + Ok(zero_division_error()) + } else if x - (y * (x / y)) == BigInt::from(0) { + Ok(UntaggedValue::Primitive(Primitive::BigInt( + BigInt::from(*x) / y, + ))) + } else { + Ok(UntaggedValue::Primitive(Primitive::Decimal( + bigdecimal::BigDecimal::from(*x) + / bigdecimal::BigDecimal::from(y.clone()), + ))) + } + } + Operator::Modulo => { + if y.is_zero() { + Ok(zero_division_error()) + } else { + Ok(UntaggedValue::Primitive(Primitive::BigInt(x % y))) + } + } + Operator::Pow => { + let prim_u32 = ToPrimitive::to_u32(y); + match prim_u32 { + Some(num) => Ok(UntaggedValue::Primitive(Primitive::Int(x.pow(num)))), + _ => Err((left.type_name(), right.type_name())), + } + } + _ => Err((left.type_name(), right.type_name())), + }, + (Primitive::BigInt(x), Primitive::Int(y)) => match operator { + Operator::Plus => Ok(UntaggedValue::Primitive(Primitive::BigInt( + x + BigInt::from(*y), + ))), + Operator::Minus => Ok(UntaggedValue::Primitive(Primitive::BigInt( + x - BigInt::from(*y), + ))), + Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::BigInt( + x * BigInt::from(*y), + ))), + Operator::Divide => { + if y.is_zero() { + Ok(zero_division_error()) + } else if x - (y * (x / y)) == BigInt::from(0) { + Ok(UntaggedValue::Primitive(Primitive::BigInt( + x / BigInt::from(*y), + ))) + } else { + Ok(UntaggedValue::Primitive(Primitive::Decimal( + bigdecimal::BigDecimal::from(x.clone()) + / bigdecimal::BigDecimal::from(*y), + ))) + } + } + Operator::Modulo => { + if y.is_zero() { + Ok(zero_division_error()) + } else { + Ok(UntaggedValue::Primitive(Primitive::BigInt(x % y))) + } + } + Operator::Pow => { + let prim_u32 = ToPrimitive::to_u32(y); + match prim_u32 { + Some(num) => Ok(UntaggedValue::Primitive(Primitive::BigInt(x.pow(num)))), + _ => Err((left.type_name(), right.type_name())), + } + } + _ => Err((left.type_name(), right.type_name())), + }, + (Primitive::BigInt(x), Primitive::BigInt(y)) => match operator { + Operator::Plus => Ok(UntaggedValue::Primitive(Primitive::BigInt(x + y))), + Operator::Minus => Ok(UntaggedValue::Primitive(Primitive::BigInt(x - y))), + Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::BigInt(x * y))), + Operator::Divide => { + if y.is_zero() { + Ok(zero_division_error()) + } else if x - (y * (x / y)) == BigInt::from(0) { + Ok(UntaggedValue::Primitive(Primitive::BigInt(x / y))) + } else { + Ok(UntaggedValue::Primitive(Primitive::Decimal( + bigdecimal::BigDecimal::from(x.clone()) + / bigdecimal::BigDecimal::from(y.clone()), + ))) + } + } + Operator::Modulo => { + if y.is_zero() { + Ok(zero_division_error()) + } else { + Ok(UntaggedValue::Primitive(Primitive::BigInt(x % y))) + } + } + Operator::Pow => { + let prim_u32 = ToPrimitive::to_u32(y); + match prim_u32 { + Some(num) => Ok(UntaggedValue::Primitive(Primitive::BigInt(x.pow(num)))), + _ => Err((left.type_name(), right.type_name())), + } + } + _ => Err((left.type_name(), right.type_name())), + }, (Primitive::Decimal(x), Primitive::Int(y)) => { + let result = match operator { + Operator::Plus => Ok(x + bigdecimal::BigDecimal::from(*y)), + Operator::Minus => Ok(x - bigdecimal::BigDecimal::from(*y)), + Operator::Multiply => Ok(x * bigdecimal::BigDecimal::from(*y)), + Operator::Divide => { + if y.is_zero() { + return Ok(zero_division_error()); + } + Ok(x / bigdecimal::BigDecimal::from(*y)) + } + Operator::Modulo => { + if y.is_zero() { + return Ok(zero_division_error()); + } + Ok(x % bigdecimal::BigDecimal::from(*y)) + } + // leaving this here for the hope that bigdecimal will one day support pow/powf/fpow + // Operator::Pow => { + // let xp = bigdecimal::ToPrimitive::to_f64(x).unwrap_or(0.0); + // let yp = bigdecimal::ToPrimitive::to_f64(y).unwrap_or(0.0); + // let pow = bigdecimal::FromPrimitive::from_f64(xp.powf(yp)); + // match pow { + // Some(p) => Ok(p), + // None => Err((left.type_name(), right.type_name())), + // } + // } + _ => Err((left.type_name(), right.type_name())), + }?; + Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) + } + (Primitive::Int(x), Primitive::Decimal(y)) => { + let result = match operator { + Operator::Plus => Ok(bigdecimal::BigDecimal::from(*x) + y), + Operator::Minus => Ok(bigdecimal::BigDecimal::from(*x) - y), + Operator::Multiply => Ok(bigdecimal::BigDecimal::from(*x) * y), + Operator::Divide => { + if y.is_zero() { + return Ok(zero_division_error()); + } + Ok(bigdecimal::BigDecimal::from(*x) / y) + } + Operator::Modulo => { + if y.is_zero() { + return Ok(zero_division_error()); + } + Ok(bigdecimal::BigDecimal::from(*x) % y) + } + // big decimal doesn't support pow yet + // Operator::Pow => { + // let yp = bigdecimal::ToPrimitive::to_u32(y).unwrap_or(0); + // Ok(bigdecimal::BigDecimal::from(x.pow(yp))) + // } + _ => Err((left.type_name(), right.type_name())), + }?; + Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) + } + (Primitive::Decimal(x), Primitive::BigInt(y)) => { let result = match operator { Operator::Plus => Ok(x + bigdecimal::BigDecimal::from(y.clone())), Operator::Minus => Ok(x - bigdecimal::BigDecimal::from(y.clone())), @@ -182,7 +363,7 @@ pub fn compute_values( }?; Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) } - (Primitive::Int(x), Primitive::Decimal(y)) => { + (Primitive::BigInt(x), Primitive::Decimal(y)) => { let result = match operator { Operator::Plus => Ok(bigdecimal::BigDecimal::from(x.clone()) + y), Operator::Minus => Ok(bigdecimal::BigDecimal::from(x.clone()) - y), diff --git a/crates/nu-engine/src/deserializer.rs b/crates/nu-engine/src/deserializer.rs index 12c14a63d..03a1d6951 100644 --- a/crates/nu-engine/src/deserializer.rs +++ b/crates/nu-engine/src/deserializer.rs @@ -439,6 +439,10 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut ConfigDeserializer<'de> { Value { value: UntaggedValue::Primitive(Primitive::Int(int)), .. + } => visit::, _>(int.tagged(tag), name, fields, visitor), + Value { + value: UntaggedValue::Primitive(Primitive::BigInt(int)), + .. } => { let i: i64 = int.tagged(value.val.tag).coerce_into("converting to i64")?; visit::, _>(i.tagged(tag), name, fields, visitor) diff --git a/crates/nu-engine/src/evaluate/evaluator.rs b/crates/nu-engine/src/evaluate/evaluator.rs index dd6f4e7d1..6a5338a67 100644 --- a/crates/nu-engine/src/evaluate/evaluator.rs +++ b/crates/nu-engine/src/evaluate/evaluator.rs @@ -212,7 +212,10 @@ fn evaluate_literal(literal: &hir::Literal, span: Span) -> Value { .into_value(span) } hir::Literal::Number(int) => match int { - nu_protocol::hir::Number::Int(i) => UntaggedValue::int(i.clone()).into_value(span), + nu_protocol::hir::Number::BigInt(i) => { + UntaggedValue::big_int(i.clone()).into_value(span) + } + nu_protocol::hir::Number::Int(i) => UntaggedValue::int(*i).into_value(span), nu_protocol::hir::Number::Decimal(d) => { UntaggedValue::decimal(d.clone()).into_value(span) } diff --git a/crates/nu-engine/src/from_value.rs b/crates/nu-engine/src/from_value.rs index 2120a357c..396ebc4f7 100644 --- a/crates/nu-engine/src/from_value.rs +++ b/crates/nu-engine/src/from_value.rs @@ -7,6 +7,7 @@ use nu_protocol::{ hir::CapturedBlock, ColumnPath, Dictionary, Primitive, Range, UntaggedValue, Value, }; use nu_source::{Tagged, TaggedItem}; +use num_bigint::BigInt; pub trait FromValue: Sized { fn from_value(v: &Value) -> Result; @@ -24,12 +25,12 @@ impl FromValue for num_bigint::BigInt { Value { value: UntaggedValue::Primitive(Primitive::Int(i)), .. - } - | Value { + } => Ok(BigInt::from(*i)), + Value { value: UntaggedValue::Primitive(Primitive::Filesize(i)), .. - } - | Value { + } => Ok(BigInt::from(*i)), + Value { value: UntaggedValue::Primitive(Primitive::Duration(i)), .. } => Ok(i.clone()), diff --git a/crates/nu-parser/src/parse.rs b/crates/nu-parser/src/parse.rs index 191a04751..334a5f5b1 100644 --- a/crates/nu-parser/src/parse.rs +++ b/crates/nu-parser/src/parse.rs @@ -57,8 +57,8 @@ pub fn parse_simple_column_path( lite_arg.span.start() + idx, ); - if let Ok(row_number) = current_part.parse::() { - output.push(Member::Int(BigInt::from(row_number), part_span)); + if let Ok(row_number) = current_part.parse::() { + output.push(Member::Int(row_number, part_span)); } else { let trimmed = trim_quotes(¤t_part); output.push(Member::Bare(trimmed.clone().spanned(part_span))); @@ -77,8 +77,8 @@ pub fn parse_simple_column_path( lite_arg.span.start() + start_index, lite_arg.span.start() + last_index + 1, ); - if let Ok(row_number) = current_part.parse::() { - output.push(Member::Int(BigInt::from(row_number), part_span)); + if let Ok(row_number) = current_part.parse::() { + output.push(Member::Int(row_number, part_span)); } else { let current_part = trim_quotes(¤t_part); output.push(Member::Bare(current_part.spanned(part_span))); @@ -131,10 +131,8 @@ pub fn parse_full_column_path( } else if head.is_none() && current_part.starts_with('$') { // We have the variable head head = Some(Expression::variable(current_part.clone(), part_span)) - } else if let Ok(row_number) = current_part.parse::() { - output.push( - UnspannedPathMember::Int(BigInt::from(row_number)).into_path_member(part_span), - ); + } else if let Ok(row_number) = current_part.parse::() { + output.push(UnspannedPathMember::Int(row_number).into_path_member(part_span)); } else { let current_part = trim_quotes(¤t_part); output.push( @@ -165,18 +163,14 @@ pub fn parse_full_column_path( head = Some(invoc.expr); } else if current_part.starts_with('$') { head = Some(Expression::variable(current_part, lite_arg.span)); - } else if let Ok(row_number) = current_part.parse::() { - output.push( - UnspannedPathMember::Int(BigInt::from(row_number)).into_path_member(part_span), - ); + } else if let Ok(row_number) = current_part.parse::() { + output.push(UnspannedPathMember::Int(row_number).into_path_member(part_span)); } else { let current_part = trim_quotes(¤t_part); output.push(UnspannedPathMember::String(current_part).into_path_member(part_span)); } - } else if let Ok(row_number) = current_part.parse::() { - output.push( - UnspannedPathMember::Int(BigInt::from(row_number)).into_path_member(part_span), - ); + } else if let Ok(row_number) = current_part.parse::() { + output.push(UnspannedPathMember::Int(row_number).into_path_member(part_span)); } else { let current_part = trim_quotes(¤t_part); output.push(UnspannedPathMember::String(current_part).into_path_member(part_span)); @@ -771,11 +765,16 @@ fn parse_arg( match expected_type { SyntaxShape::Number => { - if let Ok(x) = lite_arg.item.parse::() { + if let Ok(x) = lite_arg.item.parse::() { ( SpannedExpression::new(Expression::integer(x), lite_arg.span), None, ) + } else if let Ok(x) = lite_arg.item.parse::() { + ( + SpannedExpression::new(Expression::big_integer(x), lite_arg.span), + None, + ) } else if let Ok(x) = lite_arg.item.parse::() { ( SpannedExpression::new(Expression::decimal(x), lite_arg.span), @@ -789,11 +788,16 @@ fn parse_arg( } } SyntaxShape::Int => { - if let Ok(x) = lite_arg.item.parse::() { + if let Ok(x) = lite_arg.item.parse::() { ( SpannedExpression::new(Expression::integer(x), lite_arg.span), None, ) + } else if let Ok(x) = lite_arg.item.parse::() { + ( + SpannedExpression::new(Expression::big_integer(x), lite_arg.span), + None, + ) } else { ( garbage(lite_arg.span), diff --git a/crates/nu-protocol/src/hir.rs b/crates/nu-protocol/src/hir.rs index 516d28e15..80acaf09e 100644 --- a/crates/nu-protocol/src/hir.rs +++ b/crates/nu-protocol/src/hir.rs @@ -10,18 +10,18 @@ use crate::{hir, Dictionary, PositionalType, Primitive, SyntaxShape, UntaggedVal use crate::{PathMember, ShellTypeName}; use derive_new::new; -use nu_errors::ParseError; +use nu_errors::{ParseError, ShellError}; use nu_source::{ DbgDocBldr, DebugDocBuilder, HasSpan, PrettyDebug, PrettyDebugRefineKind, PrettyDebugWithSource, }; use nu_source::{IntoSpanned, Span, Spanned, SpannedItem, Tag}; -use bigdecimal::BigDecimal; +use bigdecimal::{BigDecimal, ToPrimitive}; use indexmap::IndexMap; use log::trace; use num_bigint::{BigInt, ToBigInt}; use num_traits::identities::Zero; -use num_traits::{FromPrimitive, ToPrimitive}; +use num_traits::FromPrimitive; #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)] pub struct InternalCommand { @@ -363,7 +363,7 @@ pub enum Unit { #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Deserialize, Serialize)] pub enum Member { String(/* outer */ Span, /* inner */ Span), - Int(BigInt, Span), + Int(i64, Span), Bare(Spanned), } @@ -371,7 +371,7 @@ impl Member { pub fn to_path_member(&self) -> PathMember { match self { //Member::String(outer, inner) => PathMember::string(inner.slice(source), *outer), - Member::Int(int, span) => PathMember::int(int.clone(), *span), + Member::Int(int, span) => PathMember::int(*int, *span), Member::Bare(spanned_string) => { PathMember::string(spanned_string.item.clone(), spanned_string.span) } @@ -402,13 +402,32 @@ impl HasSpan for Member { #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Deserialize, Serialize)] pub enum Number { - Int(BigInt), + BigInt(BigInt), + Int(i64), Decimal(BigDecimal), } +impl Number { + pub fn to_i64(&self) -> Result { + match self { + Number::BigInt(bi) => match bi.to_i64() { + Some(i) => Ok(i), + None => Err(ShellError::untagged_runtime_error( + "Cannot convert bigint to i64, too large", + )), + }, + Number::Int(i) => Ok(*i), + Number::Decimal(_) => Err(ShellError::untagged_runtime_error( + "Cannont convert decimal to i64", + )), + } + } +} + impl PrettyDebug for Number { fn pretty(&self) -> DebugDocBuilder { match self { + Number::BigInt(int) => DbgDocBldr::primitive(int), Number::Int(int) => DbgDocBldr::primitive(int), Number::Decimal(decimal) => DbgDocBldr::primitive(decimal), } @@ -420,13 +439,13 @@ macro_rules! primitive_int { $( impl From<$ty> for Number { fn from(int: $ty) -> Number { - Number::Int(BigInt::zero() + int) + Number::BigInt(BigInt::zero() + int) } } impl From<&$ty> for Number { fn from(int: &$ty) -> Number { - Number::Int(BigInt::zero() + *int) + Number::BigInt(BigInt::zero() + *int) } } )* @@ -469,8 +488,13 @@ impl std::ops::Mul for Number { fn mul(self, other: Number) -> Number { match (self, other) { (Number::Int(a), Number::Int(b)) => Number::Int(a * b), + (Number::BigInt(a), Number::Int(b)) => Number::BigInt(a * BigInt::from(b)), + (Number::Int(a), Number::BigInt(b)) => Number::BigInt(BigInt::from(a) * b), + (Number::BigInt(a), Number::BigInt(b)) => Number::BigInt(a * b), (Number::Int(a), Number::Decimal(b)) => Number::Decimal(BigDecimal::from(a) * b), (Number::Decimal(a), Number::Int(b)) => Number::Decimal(a * BigDecimal::from(b)), + (Number::BigInt(a), Number::Decimal(b)) => Number::Decimal(BigDecimal::from(a) * b), + (Number::Decimal(a), Number::BigInt(b)) => Number::Decimal(a * BigDecimal::from(b)), (Number::Decimal(a), Number::Decimal(b)) => Number::Decimal(a * b), } } @@ -482,6 +506,7 @@ impl std::ops::Mul for Number { fn mul(self, other: u32) -> Number { match self { + Number::BigInt(left) => Number::BigInt(left * (other as i64)), Number::Int(left) => Number::Int(left * (other as i64)), Number::Decimal(left) => Number::Decimal(left * BigDecimal::from(other)), } @@ -491,7 +516,8 @@ impl std::ops::Mul for Number { impl ToBigInt for Number { fn to_bigint(&self) -> Option { match self { - Number::Int(int) => Some(int.clone()), + Number::Int(int) => Some(BigInt::from(*int)), + Number::BigInt(int) => Some(int.clone()), // The BigDecimal to BigInt conversion always return Some(). // FIXME: This conversion might not be want we want, it just remove the scale. Number::Decimal(decimal) => decimal.to_bigint(), @@ -505,25 +531,6 @@ impl PrettyDebug for Unit { } } -pub fn convert_number_to_u64(number: &Number) -> u64 { - match number { - Number::Int(big_int) => { - if let Some(x) = big_int.to_u64() { - x - } else { - unreachable!("Internal error: convert_number_to_u64 given incompatible number") - } - } - Number::Decimal(big_decimal) => { - if let Some(x) = big_decimal.to_u64() { - x - } else { - unreachable!("Internal error: convert_number_to_u64 given incompatible number") - } - } - } -} - impl Unit { pub fn as_str(self) -> &'static str { match self { @@ -553,22 +560,18 @@ impl Unit { let size = size.clone(); match self { - Unit::Byte => filesize(convert_number_to_u64(&size)), - Unit::Kilobyte => filesize(convert_number_to_u64(&size) * 1000), - Unit::Megabyte => filesize(convert_number_to_u64(&size) * 1000 * 1000), - Unit::Gigabyte => filesize(convert_number_to_u64(&size) * 1000 * 1000 * 1000), - Unit::Terabyte => filesize(convert_number_to_u64(&size) * 1000 * 1000 * 1000 * 1000), - Unit::Petabyte => { - filesize(convert_number_to_u64(&size) * 1000 * 1000 * 1000 * 1000 * 1000) - } + Unit::Byte => filesize(size), + Unit::Kilobyte => filesize(size * 1000), + Unit::Megabyte => filesize(size * 1000 * 1000), + Unit::Gigabyte => filesize(size * 1000 * 1000 * 1000), + Unit::Terabyte => filesize(size * 1000 * 1000 * 1000 * 1000), + Unit::Petabyte => filesize(size * 1000 * 1000 * 1000 * 1000 * 1000), - Unit::Kibibyte => filesize(convert_number_to_u64(&size) * 1024), - Unit::Mebibyte => filesize(convert_number_to_u64(&size) * 1024 * 1024), - Unit::Gibibyte => filesize(convert_number_to_u64(&size) * 1024 * 1024 * 1024), - Unit::Tebibyte => filesize(convert_number_to_u64(&size) * 1024 * 1024 * 1024 * 1024), - Unit::Pebibyte => { - filesize(convert_number_to_u64(&size) * 1024 * 1024 * 1024 * 1024 * 1024) - } + Unit::Kibibyte => filesize(size * 1024), + Unit::Mebibyte => filesize(size * 1024 * 1024), + Unit::Gibibyte => filesize(size * 1024 * 1024 * 1024), + Unit::Tebibyte => filesize(size * 1024 * 1024 * 1024 * 1024), + Unit::Pebibyte => filesize(size * 1024 * 1024 * 1024 * 1024 * 1024), Unit::Nanosecond => duration(size.to_bigint().expect("Conversion should never fail.")), Unit::Microsecond => { @@ -614,8 +617,19 @@ impl Unit { } } -pub fn filesize(size_in_bytes: impl Into) -> UntaggedValue { - UntaggedValue::Primitive(Primitive::Filesize(size_in_bytes.into())) +pub fn filesize(size_in_bytes: Number) -> UntaggedValue { + match size_in_bytes { + Number::Int(i) => UntaggedValue::Primitive(Primitive::Filesize(i as u64)), + Number::BigInt(bi) => match bi.to_u64() { + Some(i) => UntaggedValue::Primitive(Primitive::Filesize(i)), + None => UntaggedValue::Error(ShellError::untagged_runtime_error( + "Big int too large to convert to filesize", + )), + }, + Number::Decimal(_) => UntaggedValue::Error(ShellError::untagged_runtime_error( + "Decimal can't convert to filesize", + )), + } } pub fn duration(nanos: BigInt) -> UntaggedValue { @@ -1069,10 +1083,14 @@ impl IntoSpanned for Expression { } impl Expression { - pub fn integer(i: BigInt) -> Expression { + pub fn integer(i: i64) -> Expression { Expression::Literal(Literal::Number(Number::Int(i))) } + pub fn big_integer(i: BigInt) -> Expression { + Expression::Literal(Literal::Number(Number::BigInt(i))) + } + pub fn decimal(dec: BigDecimal) -> Expression { Expression::Literal(Literal::Number(Number::Decimal(dec))) } @@ -1116,7 +1134,7 @@ impl Expression { pub fn unit(i: Spanned, unit: Spanned) -> Expression { Expression::Literal(Literal::Size( - Number::Int(BigInt::from(i.item)).spanned(i.span), + Number::BigInt(BigInt::from(i.item)).spanned(i.span), unit, )) } diff --git a/crates/nu-protocol/src/type_shape.rs b/crates/nu-protocol/src/type_shape.rs index 30d69d78b..363cadb64 100644 --- a/crates/nu-protocol/src/type_shape.rs +++ b/crates/nu-protocol/src/type_shape.rs @@ -30,6 +30,8 @@ pub enum Type { Nothing, /// An integer-based value Int, + /// An big integer-based value + BigInt, /// A range between two values Range(Box), /// A decimal (floating point) value @@ -131,6 +133,7 @@ impl Type { match primitive { Primitive::Nothing => Type::Nothing, Primitive::Int(_) => Type::Int, + Primitive::BigInt(_) => Type::BigInt, Primitive::Range(range) => { let (left_value, left_inclusion) = &range.from; let (right_value, right_inclusion) = &range.to; @@ -199,6 +202,7 @@ impl PrettyDebug for Type { match self { Type::Nothing => ty("nothing"), Type::Int => ty("integer"), + Type::BigInt => ty("big integer"), Type::Range(range) => { let (left, left_inclusion) = &range.from; let (right, right_inclusion) = &range.to; diff --git a/crates/nu-protocol/src/value.rs b/crates/nu-protocol/src/value.rs index 0fef1e18a..b4bd9caba 100644 --- a/crates/nu-protocol/src/value.rs +++ b/crates/nu-protocol/src/value.rs @@ -178,10 +178,15 @@ impl UntaggedValue { } /// Helper for creating integer values - pub fn int(i: impl Into) -> UntaggedValue { + pub fn int(i: impl Into) -> UntaggedValue { UntaggedValue::Primitive(Primitive::Int(i.into())) } + /// Helper for creating big integer values + pub fn big_int(i: impl Into) -> UntaggedValue { + UntaggedValue::Primitive(Primitive::BigInt(i.into())) + } + /// Helper for creating glob pattern values pub fn glob_pattern(s: impl Into) -> UntaggedValue { UntaggedValue::Primitive(Primitive::String(s.into())) @@ -193,8 +198,8 @@ impl UntaggedValue { } /// Helper for creating filesize values - pub fn filesize(s: impl Into) -> UntaggedValue { - UntaggedValue::Primitive(Primitive::Filesize(s.into())) + pub fn filesize(s: u64) -> UntaggedValue { + UntaggedValue::Primitive(Primitive::Filesize(s)) } /// Helper for creating decimal values @@ -339,22 +344,30 @@ impl Value { } } - /// View the Value as a Int (BigInt), if possible - pub fn as_int(&self) -> Result { + /// View the Value as a Int, if possible + pub fn as_int(&self) -> Result { match &self.value { - UntaggedValue::Primitive(Primitive::Int(n)) => Ok(n.clone()), + UntaggedValue::Primitive(Primitive::Int(n)) => Ok(*n), _ => Err(ShellError::type_error("bigint", self.spanned_type_name())), } } - /// View the Value as a Filesize (BigInt), if possible - pub fn as_filesize(&self) -> Result { + /// View the Value as a Int, if possible + pub fn as_big_int(&self) -> Result { match &self.value { - UntaggedValue::Primitive(Primitive::Filesize(fs)) => Ok(fs.clone()), + UntaggedValue::Primitive(Primitive::BigInt(n)) => Ok(n.clone()), _ => Err(ShellError::type_error("bigint", self.spanned_type_name())), } } + /// View the Value as a Filesize (u64), if possible + pub fn as_filesize(&self) -> Result { + match &self.value { + UntaggedValue::Primitive(Primitive::Filesize(fs)) => Ok(*fs), + _ => Err(ShellError::type_error("int", self.spanned_type_name())), + } + } + /// View the Value as a Duration (BigInt), if possible pub fn as_duration(&self) -> Result { match &self.value { @@ -808,8 +821,6 @@ pub trait U64Ext { fn to_untagged_value(&self) -> UntaggedValue; fn to_value(&self, tag: Tag) -> Value; fn to_value_create_tag(&self) -> Value; - fn to_filesize_untagged_value(&self) -> UntaggedValue; - fn to_filesize_value(&self, tag: Tag) -> Value; fn to_duration_untagged_value(&self) -> UntaggedValue; fn to_duration_value(&self, tag: Tag) -> Value; } @@ -817,14 +828,7 @@ pub trait U64Ext { impl U64Ext for u64 { fn to_value(&self, the_tag: Tag) -> Value { Value { - value: UntaggedValue::Primitive(Primitive::Int(BigInt::from(*self))), - tag: the_tag, - } - } - - fn to_filesize_value(&self, the_tag: Tag) -> Value { - Value { - value: UntaggedValue::Primitive(Primitive::Filesize(BigInt::from(*self))), + value: UntaggedValue::Primitive(Primitive::BigInt(BigInt::from(*self))), tag: the_tag, } } @@ -839,7 +843,7 @@ impl U64Ext for u64 { fn to_value_create_tag(&self) -> Value { let end = self.to_string().len(); Value { - value: UntaggedValue::Primitive(Primitive::Int(BigInt::from(*self))), + value: UntaggedValue::Primitive(Primitive::BigInt(BigInt::from(*self))), tag: Tag { anchor: None, span: Span::new(0, end), @@ -848,11 +852,7 @@ impl U64Ext for u64 { } fn to_untagged_value(&self) -> UntaggedValue { - UntaggedValue::int(*self) - } - - fn to_filesize_untagged_value(&self) -> UntaggedValue { - UntaggedValue::filesize(*self) + UntaggedValue::big_int(*self) } fn to_duration_untagged_value(&self) -> UntaggedValue { @@ -869,7 +869,7 @@ pub trait I64Ext { impl I64Ext for i64 { fn to_value(&self, the_tag: Tag) -> Value { Value { - value: UntaggedValue::Primitive(Primitive::Int(BigInt::from(*self))), + value: UntaggedValue::Primitive(Primitive::Int(*self)), tag: the_tag, } } @@ -877,7 +877,7 @@ impl I64Ext for i64 { fn to_value_create_tag(&self) -> Value { let end = self.to_string().len(); Value { - value: UntaggedValue::Primitive(Primitive::Int(BigInt::from(*self))), + value: UntaggedValue::Primitive(Primitive::Int(*self)), tag: Tag { anchor: None, span: Span::new(0, end), diff --git a/crates/nu-protocol/src/value/column_path.rs b/crates/nu-protocol/src/value/column_path.rs index bd2d32d00..c35a4c975 100644 --- a/crates/nu-protocol/src/value/column_path.rs +++ b/crates/nu-protocol/src/value/column_path.rs @@ -4,7 +4,6 @@ use nu_source::{ span_for_spanned_list, DbgDocBldr, DebugDocBuilder, HasFallibleSpan, PrettyDebug, Span, Spanned, SpannedItem, }; -use num_bigint::BigInt; use serde::{Deserialize, Serialize}; use crate::hir::{Expression, Literal, Member, SpannedExpression}; @@ -14,7 +13,7 @@ use nu_errors::ParseError; #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] pub enum UnspannedPathMember { String(String), - Int(BigInt), + Int(i64), } impl UnspannedPathMember { @@ -125,8 +124,8 @@ impl PathMember { } /// Create a numeric path member - pub fn int(int: impl Into, span: impl Into) -> PathMember { - UnspannedPathMember::Int(int.into()).into_path_member(span) + pub fn int(int: i64, span: impl Into) -> PathMember { + UnspannedPathMember::Int(int).into_path_member(span) } pub fn as_string(&self) -> String { @@ -160,8 +159,8 @@ fn parse(raw_column_path: &Spanned) -> (SpannedExpression, Option() { - output.push(Member::Int(BigInt::from(row_number), part_span)); + if let Ok(row_number) = current_part.parse::() { + output.push(Member::Int(row_number, part_span)); } else { let trimmed = trim_quotes(¤t_part); output.push(Member::Bare(trimmed.clone().spanned(part_span))); @@ -180,8 +179,8 @@ fn parse(raw_column_path: &Spanned) -> (SpannedExpression, Option() { - output.push(Member::Int(BigInt::from(row_number), part_span)); + if let Ok(row_number) = current_part.parse::() { + output.push(Member::Int(row_number, part_span)); } else { let current_part = trim_quotes(¤t_part); output.push(Member::Bare(current_part.spanned(part_span))); diff --git a/crates/nu-protocol/src/value/convert.rs b/crates/nu-protocol/src/value/convert.rs index bdff4fe30..c3b71871e 100644 --- a/crates/nu-protocol/src/value/convert.rs +++ b/crates/nu-protocol/src/value/convert.rs @@ -11,7 +11,8 @@ impl std::convert::TryFrom<&Value> for i64 { /// Convert to an i64 integer, if possible fn try_from(value: &Value) -> Result { match &value.value { - UntaggedValue::Primitive(Primitive::Int(int)) => { + UntaggedValue::Primitive(Primitive::Int(int)) => Ok(*int), + UntaggedValue::Primitive(Primitive::BigInt(int)) => { int.tagged(&value.tag).coerce_into("converting to i64") } _ => Err(ShellError::type_error("Integer", value.spanned_type_name())), diff --git a/crates/nu-protocol/src/value/debug.rs b/crates/nu-protocol/src/value/debug.rs index 99f273954..80376142e 100644 --- a/crates/nu-protocol/src/value/debug.rs +++ b/crates/nu-protocol/src/value/debug.rs @@ -36,6 +36,7 @@ impl PrettyType for Primitive { match self { Primitive::Nothing => ty("nothing"), Primitive::Int(_) => ty("integer"), + Primitive::BigInt(_) => ty("big-integer"), Primitive::Range(_) => ty("range"), Primitive::Decimal(_) => ty("decimal"), Primitive::Filesize(_) => ty("filesize"), @@ -59,6 +60,7 @@ impl PrettyDebug for Primitive { match self { Primitive::Nothing => DbgDocBldr::primitive("nothing"), Primitive::Int(int) => prim(format_args!("{}", int)), + Primitive::BigInt(int) => prim(format_args!("{}", int)), Primitive::Decimal(decimal) => prim(format_args!("{}", decimal)), Primitive::Range(range) => { let (left, left_inclusion) = &range.from; diff --git a/crates/nu-protocol/src/value/primitive.rs b/crates/nu-protocol/src/value/primitive.rs index c9266a164..e1b2e20c5 100644 --- a/crates/nu-protocol/src/value/primitive.rs +++ b/crates/nu-protocol/src/value/primitive.rs @@ -24,14 +24,16 @@ const NANOS_PER_SEC: u32 = 1_000_000_000; pub enum Primitive { /// An empty value Nothing, + /// A common integer + Int(i64), /// A "big int", an integer with arbitrarily large size (aka not limited to 64-bit) #[serde(with = "serde_bigint")] - Int(BigInt), + BigInt(BigInt), /// A "big decimal", an decimal number with arbitrarily large size (aka not limited to 64-bit) #[serde(with = "serde_bigdecimal")] Decimal(BigDecimal), /// A count in the number of bytes, used as a filesize - Filesize(BigInt), + Filesize(u64), /// A string value String(String), /// A path to travel to reach a value in a table @@ -351,7 +353,7 @@ impl From for Primitive { impl From for Primitive { /// Helper to convert from integers to a Primitive value fn from(int: BigInt) -> Primitive { - Primitive::Int(int) + Primitive::BigInt(int) } } @@ -373,14 +375,14 @@ macro_rules! from_native_to_primitive { }; } -from_native_to_primitive!(i8, Primitive::Int, BigInt::from_i8); -from_native_to_primitive!(i16, Primitive::Int, BigInt::from_i16); -from_native_to_primitive!(i32, Primitive::Int, BigInt::from_i32); -from_native_to_primitive!(i64, Primitive::Int, BigInt::from_i64); -from_native_to_primitive!(u8, Primitive::Int, BigInt::from_u8); -from_native_to_primitive!(u16, Primitive::Int, BigInt::from_u16); -from_native_to_primitive!(u32, Primitive::Int, BigInt::from_u32); -from_native_to_primitive!(u64, Primitive::Int, BigInt::from_u64); +from_native_to_primitive!(i8, Primitive::Int, i64::from_i8); +from_native_to_primitive!(i16, Primitive::Int, i64::from_i16); +from_native_to_primitive!(i32, Primitive::Int, i64::from_i32); +from_native_to_primitive!(i64, Primitive::Int, i64::from_i64); +from_native_to_primitive!(u8, Primitive::Int, i64::from_u8); +from_native_to_primitive!(u16, Primitive::Int, i64::from_u16); +from_native_to_primitive!(u32, Primitive::Int, i64::from_u32); +from_native_to_primitive!(u64, Primitive::BigInt, BigInt::from_u64); from_native_to_primitive!(f32, Primitive::Decimal, BigDecimal::from_f32); from_native_to_primitive!(f64, Primitive::Decimal, BigDecimal::from_f64); @@ -410,6 +412,7 @@ impl ShellTypeName for Primitive { match self { Primitive::Nothing => "nothing", Primitive::Int(_) => "integer", + Primitive::BigInt(_) => "big integer", Primitive::Range(_) => "range", Primitive::Decimal(_) => "decimal", Primitive::Filesize(_) => "filesize(in bytes)", @@ -454,6 +457,7 @@ pub fn format_primitive(primitive: &Primitive, field_name: Option<&String>) -> S } Primitive::Duration(duration) => format_duration(duration), Primitive::Int(i) => i.to_string(), + Primitive::BigInt(i) => i.to_string(), Primitive::Decimal(decimal) => { // TODO: We should really pass the precision in here instead of hard coding it let decimal_string = decimal.to_string(); diff --git a/crates/nu-protocol/src/value/range.rs b/crates/nu-protocol/src/value/range.rs index c7152fc8e..77b27b996 100644 --- a/crates/nu-protocol/src/value/range.rs +++ b/crates/nu-protocol/src/value/range.rs @@ -68,6 +68,36 @@ impl Range { } } + pub fn min_i64(&self) -> Result { + let (from, range_incl) = &self.from; + + let minval = if let Primitive::Nothing = from.item { + 0 + } else { + from.item.as_i64(from.span)? + }; + + match range_incl { + RangeInclusion::Inclusive => Ok(minval), + RangeInclusion::Exclusive => Ok(minval.saturating_add(1)), + } + } + + pub fn max_i64(&self) -> Result { + let (to, range_incl) = &self.to; + + let maxval = if let Primitive::Nothing = to.item { + i64::MAX + } else { + to.item.as_i64(to.span)? + }; + + match range_incl { + RangeInclusion::Inclusive => Ok(maxval), + RangeInclusion::Exclusive => Ok(maxval.saturating_sub(1)), + } + } + pub fn min_usize(&self) -> Result { let (from, range_incl) = &self.from; diff --git a/crates/nu-test-support/src/value.rs b/crates/nu-test-support/src/value.rs index e6f216892..faab4f193 100644 --- a/crates/nu-test-support/src/value.rs +++ b/crates/nu-test-support/src/value.rs @@ -6,7 +6,11 @@ use nu_protocol::{PathMember, Primitive, UntaggedValue, Value}; use nu_source::{Span, TaggedItem}; use num_bigint::BigInt; -pub fn int(s: impl Into) -> Value { +pub fn big_int(s: impl Into) -> Value { + UntaggedValue::big_int(s).into_untagged_value() +} + +pub fn int(s: impl Into) -> Value { UntaggedValue::int(s).into_untagged_value() } diff --git a/crates/nu-value-ext/src/lib.rs b/crates/nu-value-ext/src/lib.rs index 04f9c1852..963ba711d 100644 --- a/crates/nu-value-ext/src/lib.rs +++ b/crates/nu-value-ext/src/lib.rs @@ -648,7 +648,7 @@ pub fn as_column_path(value: &Value) -> Result, ShellError> { pub fn as_path_member(value: &Value) -> Result { match &value.value { UntaggedValue::Primitive(primitive) => match primitive { - Primitive::Int(int) => Ok(PathMember::int(int.clone(), value.tag.span)), + Primitive::Int(int) => Ok(PathMember::int(*int, value.tag.span)), Primitive::String(string) => Ok(PathMember::string(string, value.tag.span)), other => Err(ShellError::type_error( "path member", diff --git a/crates/nu_plugin_post/src/post.rs b/crates/nu_plugin_post/src/post.rs index 0911b632d..d4c155903 100644 --- a/crates/nu_plugin_post/src/post.rs +++ b/crates/nu_plugin_post/src/post.rs @@ -372,6 +372,9 @@ pub fn value_to_json_value(v: &Value) -> Result { })?, ), UntaggedValue::Primitive(Primitive::Int(i)) => { + serde_json::Value::Number(serde_json::Number::from(*i)) + } + UntaggedValue::Primitive(Primitive::BigInt(i)) => { serde_json::Value::Number(serde_json::Number::from(CoerceInto::::coerce_into( i.tagged(&v.tag), "converting to JSON number", @@ -386,12 +389,9 @@ pub fn value_to_json_value(v: &Value) -> Result { UnspannedPathMember::String(string) => { Ok(serde_json::Value::String(string.clone())) } - UnspannedPathMember::Int(int) => Ok(serde_json::Value::Number( - serde_json::Number::from(CoerceInto::::coerce_into( - int.tagged(&v.tag), - "converting to JSON number", - )?), - )), + UnspannedPathMember::Int(int) => { + Ok(serde_json::Value::Number(serde_json::Number::from(*int))) + } }) .collect::, ShellError>>()?, ), diff --git a/crates/nu_plugin_ps/src/ps.rs b/crates/nu_plugin_ps/src/ps.rs index d487de8e8..8d93ea1d6 100644 --- a/crates/nu_plugin_ps/src/ps.rs +++ b/crates/nu_plugin_ps/src/ps.rs @@ -23,7 +23,7 @@ pub async fn ps(tag: Tag, long: bool) -> Result, ShellError> { for pid in result.into_iter() { if let Some(result) = sys.get_process(pid) { let mut dict = TaggedDictBuilder::new(&tag); - dict.insert_untagged("pid", UntaggedValue::int(pid)); + dict.insert_untagged("pid", UntaggedValue::int(pid as i64)); dict.insert_untagged("name", UntaggedValue::string(result.name())); dict.insert_untagged( "status", @@ -41,7 +41,7 @@ pub async fn ps(tag: Tag, long: bool) -> Result, ShellError> { if long { if let Some(parent) = result.parent() { - dict.insert_untagged("parent", UntaggedValue::int(parent)); + dict.insert_untagged("parent", UntaggedValue::int(parent as i64)); } else { dict.insert_untagged("parent", UntaggedValue::nothing()); } diff --git a/crates/nu_plugin_sys/src/sys.rs b/crates/nu_plugin_sys/src/sys.rs index 6941edde8..d34aade76 100644 --- a/crates/nu_plugin_sys/src/sys.rs +++ b/crates/nu_plugin_sys/src/sys.rs @@ -80,7 +80,7 @@ pub fn cpu(sys: &mut System, tag: Tag) -> Option { "brand", UntaggedValue::string(trim_cstyle_null(cpu.get_brand().to_string())), ); - dict.insert_untagged("freq", UntaggedValue::int(cpu.get_frequency())); + dict.insert_untagged("freq", UntaggedValue::int(cpu.get_frequency() as i64)); output.push(dict.into_value()); } diff --git a/crates/nu_plugin_to_bson/src/to_bson.rs b/crates/nu_plugin_to_bson/src/to_bson.rs index a3dd911ff..bf6ac5cc7 100644 --- a/crates/nu_plugin_to_bson/src/to_bson.rs +++ b/crates/nu_plugin_to_bson/src/to_bson.rs @@ -41,7 +41,8 @@ pub fn value_to_bson_value(v: &Value) -> Result { ) })?) } - UntaggedValue::Primitive(Primitive::Int(i)) => { + UntaggedValue::Primitive(Primitive::Int(i)) => Bson::I64(*i), + UntaggedValue::Primitive(Primitive::BigInt(i)) => { Bson::I64(i.tagged(&v.tag).coerce_into("converting to BSON")?) } UntaggedValue::Primitive(Primitive::Nothing) => Bson::Null, @@ -50,9 +51,7 @@ pub fn value_to_bson_value(v: &Value) -> Result { path.iter() .map(|x| match &x.unspanned { UnspannedPathMember::String(string) => Ok(Bson::String(string.clone())), - UnspannedPathMember::Int(int) => Ok(Bson::I64( - int.tagged(&v.tag).coerce_into("converting to BSON")?, - )), + UnspannedPathMember::Int(int) => Ok(Bson::I64(*int)), }) .collect::, ShellError>>()?, ), @@ -183,7 +182,7 @@ fn get_binary_subtype(tagged_value: &Value) -> Result "md5" => BinarySubtype::Md5, _ => unreachable!(), }), - UntaggedValue::Primitive(Primitive::Int(i)) => Ok(BinarySubtype::UserDefined( + UntaggedValue::Primitive(Primitive::BigInt(i)) => Ok(BinarySubtype::UserDefined( i.tagged(&tagged_value.tag) .coerce_into("converting to BSON binary subtype")?, )), diff --git a/crates/nu_plugin_to_sqlite/src/to_sqlite.rs b/crates/nu_plugin_to_sqlite/src/to_sqlite.rs index 8874dc285..6066d2c28 100644 --- a/crates/nu_plugin_to_sqlite/src/to_sqlite.rs +++ b/crates/nu_plugin_to_sqlite/src/to_sqlite.rs @@ -41,6 +41,7 @@ fn nu_value_to_sqlite_string(v: Value) -> String { match &v.value { UntaggedValue::Primitive(p) => match p { Primitive::Nothing => "NULL".into(), + Primitive::BigInt(i) => format!("{}", i), Primitive::Int(i) => format!("{}", i), Primitive::Duration(i) => format!("{}", i), Primitive::Decimal(f) => format!("{}", f),