Make the default int an i64 (#3428)

* Make the default int an i64

* fmt

* Fix random integer

* Treat pids as i64 for now
This commit is contained in:
JT 2021-05-14 20:35:09 +12:00 committed by GitHub
parent 440a589f9e
commit d79a3130b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
59 changed files with 693 additions and 284 deletions

View File

@ -273,6 +273,7 @@ fn get_shape_of_expr(expr: &SpannedExpression) -> Option<SyntaxShape> {
Expression::Literal(literal) => { Expression::Literal(literal) => {
match literal { match literal {
nu_protocol::hir::Literal::Number(number) => match number { 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::Int(_) => Some(SyntaxShape::Int),
nu_protocol::hir::Number::Decimal(_) => Some(SyntaxShape::Number), nu_protocol::hir::Number::Decimal(_) => Some(SyntaxShape::Number),
}, },

View File

@ -106,6 +106,12 @@ pub fn autoview(args: CommandArgs) -> Result<OutputStream, ShellError> {
} => { } => {
out!("{}", n); out!("{}", n);
} }
Value {
value: UntaggedValue::Primitive(Primitive::BigInt(n)),
..
} => {
out!("{}", n);
}
Value { Value {
value: UntaggedValue::Primitive(Primitive::Decimal(n)), value: UntaggedValue::Primitive(Primitive::Decimal(n)),
.. ..

View File

@ -102,7 +102,7 @@ pub fn process_row(
pub(crate) fn make_indexed_item(index: usize, item: Value) -> Value { pub(crate) fn make_indexed_item(index: usize, item: Value) -> Value {
let mut dict = TaggedDictBuilder::new(item.tag()); 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.insert_value("item", item);
dict.into_value() dict.into_value()

View File

@ -1,5 +1,4 @@
use crate::prelude::*; use crate::prelude::*;
use bigdecimal::Zero;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::hir::Operator; use nu_protocol::hir::Operator;
@ -81,7 +80,7 @@ impl RangeIterator {
}; };
let end = match range.to.0.item { let end = match range.to.0.item {
Primitive::Nothing => Primitive::Int(u64::MAX.into()), Primitive::Nothing => Primitive::Int(i64::MAX),
x => x, x => x,
}; };
@ -121,11 +120,11 @@ impl Iterator for RangeIterator {
( (
UntaggedValue::Primitive(Primitive::Decimal(x)), UntaggedValue::Primitive(Primitive::Decimal(x)),
UntaggedValue::Primitive(Primitive::Int(y)), UntaggedValue::Primitive(Primitive::Int(y)),
) => x.cmp(&(BigDecimal::zero() + y)), ) => x.cmp(&(BigDecimal::from(*y))),
( (
UntaggedValue::Primitive(Primitive::Int(x)), UntaggedValue::Primitive(Primitive::Int(x)),
UntaggedValue::Primitive(Primitive::Decimal(y)), UntaggedValue::Primitive(Primitive::Decimal(y)),
) => (BigDecimal::zero() + x).cmp(y), ) => (BigDecimal::from(*x)).cmp(y),
_ => { _ => {
self.done = true; self.done = true;
return Some( return Some(

View File

@ -64,7 +64,7 @@ fn process_row(
.. ..
} = s } = 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); let byte_value = Value::from(byte_format.1);
Ok(input Ok(input
.replace_data_at_column_path(&field, byte_value) .replace_data_at_column_path(&field, byte_value)

View File

@ -35,7 +35,7 @@ fn convert_json_value_to_nu_value(v: &nu_json::Value, tag: impl Into<Tag>) -> Va
nu_json::Value::Null => UntaggedValue::Primitive(Primitive::Nothing).into_value(&tag), 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::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::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::I64(n) => UntaggedValue::int(*n).into_value(&tag),
nu_json::Value::String(s) => { nu_json::Value::String(s) => {
UntaggedValue::Primitive(Primitive::String(String::from(s))).into_value(&tag) UntaggedValue::Primitive(Primitive::String(String::from(s))).into_value(&tag)

View File

@ -90,14 +90,14 @@ impl WholeStreamCommand for SubCommand {
description: "convert a number to a nushell binary primitive", description: "convert a number to a nushell binary primitive",
example: "echo 1 | into binary", example: "echo 1 | into binary",
result: Some(vec![ 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 { Example {
description: "convert a boolean to a nushell binary primitive", description: "convert a boolean to a nushell binary primitive",
example: "echo $true | into binary", example: "echo $true | into binary",
result: Some(vec![ 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 { Example {
@ -152,7 +152,17 @@ fn into_binary(args: CommandArgs) -> Result<ActionStream, ShellError> {
.to_action_stream()) .to_action_stream())
} }
pub fn bigint_to_endian(n: &BigInt) -> Vec<u8> { fn int_to_endian(n: i64) -> Vec<u8> {
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<u8> {
if cfg!(target_endian = "little") { if cfg!(target_endian = "little") {
// eprintln!("Little Endian"); // eprintln!("Little Endian");
n.to_bytes_le().1 n.to_bytes_le().1
@ -192,7 +202,8 @@ pub fn action(
.collect() .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() { Primitive::Decimal(dec) => match dec.to_bigint() {
Some(n) => bigint_to_endian(&n), Some(n) => bigint_to_endian(&n),
None => { None => {
@ -229,8 +240,8 @@ pub fn action(
} }
} }
Primitive::Boolean(a_bool) => match a_bool { Primitive::Boolean(a_bool) => match a_bool {
false => bigint_to_endian(&BigInt::from(0)), false => int_to_endian(0),
true => bigint_to_endian(&BigInt::from(1)), true => int_to_endian(1),
}, },
Primitive::Date(a_date) => a_date.format("%c").to_string().as_bytes().to_vec(), Primitive::Date(a_date) => a_date.format("%c").to_string().as_bytes().to_vec(),
Primitive::FilePath(a_filepath) => a_filepath Primitive::FilePath(a_filepath) => a_filepath

View File

@ -4,7 +4,7 @@ use nu_errors::ShellError;
use nu_protocol::{ use nu_protocol::{
ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
}; };
use num_bigint::{BigInt, ToBigInt}; use num_bigint::ToBigInt;
pub struct SubCommand; pub struct SubCommand;
@ -118,20 +118,34 @@ pub fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {
} }
}, },
Primitive::Decimal(dec) => match dec.to_bigint() { 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 => { None => {
return Err(ShellError::unimplemented( return Err(ShellError::unimplemented(
"failed to convert decimal to int", "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 { Primitive::Boolean(a_bool) => match a_bool {
false => BigInt::from(0), false => 0,
true => BigInt::from(1), true => 1,
}, },
Primitive::Filesize(a_filesize) => match a_filesize.to_bigint() { 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 => { None => {
return Err(ShellError::unimplemented( return Err(ShellError::unimplemented(
"failed to convert filesize to bigint", "failed to convert filesize to bigint",
@ -154,13 +168,17 @@ pub fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {
} }
} }
fn int_from_string(a_string: &str, tag: &Tag) -> Result<BigInt, ShellError> { fn int_from_string(a_string: &str, tag: &Tag) -> Result<i64, ShellError> {
match a_string.parse::<BigInt>() { match a_string.parse::<i64>() {
Ok(n) => Ok(n), Ok(n) => Ok(n),
Err(_) => match a_string.parse::<f64>() { Err(_) => match a_string.parse::<f64>() {
Ok(res_float) => match res_float.to_bigint() { Ok(f) => match f.to_i64() {
Some(n) => Ok(n), Some(i) => Ok(i),
None => Err(ShellError::unimplemented("failed to convert f64 to int")), None => Err(ShellError::labeled_error(
"Could not convert string value to int",
"original value",
tag.clone(),
)),
}, },
Err(_) => Err(ShellError::labeled_error( Err(_) => Err(ShellError::labeled_error(
"Could not convert string value to int", "Could not convert string value to int",

View File

@ -129,6 +129,13 @@ pub fn action(
match &input.value { match &input.value {
UntaggedValue::Primitive(prim) => Ok(UntaggedValue::string(match prim { UntaggedValue::Primitive(prim) => Ok(UntaggedValue::string(match prim {
Primitive::Int(int) => { Primitive::Int(int) => {
if group_digits {
format_int(*int) // int.to_formatted_string(*locale)
} else {
int.to_string()
}
}
Primitive::BigInt(int) => {
if group_digits { if group_digits {
format_bigint(int) // int.to_formatted_string(*locale) format_bigint(int) // int.to_formatted_string(*locale)
} else { } else {
@ -141,7 +148,7 @@ pub fn action(
Primitive::Date(a_date) => a_date.format("%c").to_string(), Primitive::Date(a_date) => a_date.format("%c").to_string(),
Primitive::FilePath(a_filepath) => a_filepath.as_path().display().to_string(), Primitive::FilePath(a_filepath) => a_filepath.as_path().display().to_string(),
Primitive::Filesize(a_filesize) => { 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 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 { fn format_bigint(int: &BigInt) -> String {
format!("{}", int) format!("{}", int)

View File

@ -1,7 +1,7 @@
use crate::prelude::*; use crate::prelude::*;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
pub struct Last; pub struct Last;
@ -31,7 +31,7 @@ impl WholeStreamCommand for Last {
Example { Example {
description: "Get the last row", description: "Get the last row",
example: "echo [1 2 3] | last", example: "echo [1 2 3] | last",
result: Some(vec![Value::from(UntaggedValue::from(BigInt::from(3)))]), result: Some(vec![UntaggedValue::int(3).into()]),
}, },
Example { Example {
description: "Get the last three rows", description: "Get the last three rows",

View File

@ -94,7 +94,7 @@ impl Iterator for CountIterator {
input.count() input.count()
}; };
Some(UntaggedValue::int(length).into_value(self.tag.clone())) Some(UntaggedValue::int(length as i64).into_value(self.tag.clone()))
} }
} }

View File

@ -20,8 +20,9 @@ impl WholeStreamCommand for SubCommand {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let mapped = args.input.map(move |val| match val.value { let mapped = args.input.map(move |val| match val.value {
UntaggedValue::Primitive(Primitive::Int(val)) => { UntaggedValue::Primitive(Primitive::Int(val)) => UntaggedValue::int(val.abs()).into(),
UntaggedValue::int(val.magnitude().clone()).into() UntaggedValue::Primitive(Primitive::BigInt(val)) => {
UntaggedValue::big_int(val.magnitude().clone()).into()
} }
UntaggedValue::Primitive(Primitive::Decimal(val)) => { UntaggedValue::Primitive(Primitive::Decimal(val)) => {
UntaggedValue::decimal(val.abs()).into() UntaggedValue::decimal(val.abs()).into()

View File

@ -5,10 +5,7 @@ use crate::commands::math::utils::run_with_function;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ use nu_protocol::{hir::Operator, Primitive, Signature, UntaggedValue, Value};
hir::{convert_number_to_u64, Number, Operator},
Primitive, Signature, UntaggedValue, Value,
};
use bigdecimal::FromPrimitive; use bigdecimal::FromPrimitive;
@ -47,7 +44,7 @@ impl WholeStreamCommand for SubCommand {
fn to_byte(value: &Value) -> Option<Value> { fn to_byte(value: &Value) -> Option<Value> {
match &value.value { match &value.value {
UntaggedValue::Primitive(Primitive::Int(num)) => { 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, _ => None,
} }
@ -79,7 +76,7 @@ pub fn average(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
Value { Value {
value: UntaggedValue::Primitive(Primitive::Filesize(num)), value: UntaggedValue::Primitive(Primitive::Filesize(num)),
.. ..
} => UntaggedValue::int(num.clone()).into_untagged_value(), } => UntaggedValue::int(*num as i64).into_untagged_value(),
other => other.clone(), other => other.clone(),
}) })
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
@ -100,15 +97,18 @@ pub fn average(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
value: UntaggedValue::Primitive(Primitive::Filesize(num)), 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); let result = nu_data::value::compute_values(Operator::Divide, &left, &total_rows);
match result { match result {
Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) => { Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) => match result.to_u64() {
let number = Number::Decimal(result); Some(number) => Ok(UntaggedValue::filesize(number).into_value(name)),
let number = convert_number_to_u64(&number); None => Err(ShellError::labeled_error(
Ok(UntaggedValue::filesize(number).into_value(name)) "could not calculate average of non-integer or unrelated types",
} "source",
name,
)),
},
Ok(_) => Err(ShellError::labeled_error( Ok(_) => Err(ShellError::labeled_error(
"could not calculate average of non-integer or unrelated types", "could not calculate average of non-integer or unrelated types",
"source", "source",

View File

@ -23,7 +23,13 @@ impl WholeStreamCommand for SubCommand {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let input = args.input; 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<Example> { fn examples(&self) -> Vec<Example> {
@ -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() UntaggedValue::int(val).into()
} }
fn ceil_big_int(val: BigInt) -> Value {
UntaggedValue::big_int(val).into()
}
fn ceil_big_decimal(val: BigDecimal) -> Value { fn ceil_big_decimal(val: BigDecimal) -> Value {
let mut maybe_ceiled = val.round(0); let mut maybe_ceiled = val.round(0);
if maybe_ceiled < val { if maybe_ceiled < val {
maybe_ceiled += BigDecimal::one(); maybe_ceiled += BigDecimal::one();
} }
let (ceiled, _) = maybe_ceiled.into_bigint_and_exponent(); let ceiling = maybe_ceiled.to_i64();
UntaggedValue::int(ceiled).into()
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 { fn ceil_default(_: UntaggedValue) -> Value {

View File

@ -25,6 +25,7 @@ impl WholeStreamCommand for SubCommand {
run_with_numerical_functions_on_stream( run_with_numerical_functions_on_stream(
input, input,
floor_int,
floor_big_int, floor_big_int,
floor_big_decimal, floor_big_decimal,
floor_default, floor_default,
@ -36,25 +37,29 @@ impl WholeStreamCommand for SubCommand {
description: "Apply the floor function to a list of numbers", description: "Apply the floor function to a list of numbers",
example: "echo [1.5 2.3 -3.1] | math floor", example: "echo [1.5 2.3 -3.1] | math floor",
result: Some(vec![ result: Some(vec![
UntaggedValue::int(1).into(), UntaggedValue::big_int(1).into(),
UntaggedValue::int(2).into(), UntaggedValue::big_int(2).into(),
UntaggedValue::int(-4).into(), UntaggedValue::big_int(-4).into(),
]), ]),
}] }]
} }
} }
fn floor_big_int(val: BigInt) -> Value { fn floor_int(val: i64) -> Value {
UntaggedValue::int(val).into() UntaggedValue::int(val).into()
} }
fn floor_big_int(val: BigInt) -> Value {
UntaggedValue::big_int(val).into()
}
fn floor_big_decimal(val: BigDecimal) -> Value { fn floor_big_decimal(val: BigDecimal) -> Value {
let mut maybe_floored = val.round(0); let mut maybe_floored = val.round(0);
if maybe_floored > val { if maybe_floored > val {
maybe_floored -= BigDecimal::one(); maybe_floored -= BigDecimal::one();
} }
let (floored, _) = maybe_floored.into_bigint_and_exponent(); let (floored, _) = maybe_floored.into_bigint_and_exponent();
UntaggedValue::int(floored).into() UntaggedValue::big_int(floored).into()
} }
fn floor_default(_: UntaggedValue) -> Value { fn floor_default(_: UntaggedValue) -> Value {

View File

@ -4,10 +4,7 @@ use crate::prelude::*;
use bigdecimal::FromPrimitive; use bigdecimal::FromPrimitive;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ use nu_protocol::{hir::Operator, Primitive, Signature, UntaggedValue, Value};
hir::{convert_number_to_u64, Number, Operator},
Primitive, Signature, UntaggedValue, Value,
};
pub struct SubCommand; pub struct SubCommand;
@ -124,14 +121,21 @@ fn compute_average(values: &[Value], name: impl Into<Tag>) -> Result<Value, Shel
value: UntaggedValue::Primitive(Primitive::Filesize(num)), 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); let result = nu_data::value::compute_values(Operator::Divide, &left, &total_rows);
match result { match result {
Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) => { Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) => {
let number = Number::Decimal(result); let (bi, _) = result.as_bigint_and_exponent();
let number = convert_number_to_u64(&number); let number = bi.to_u64();
Ok(UntaggedValue::filesize(number).into_value(name)) 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( Ok(_) => Err(ShellError::labeled_error(
"could not calculate median of non-numeric or unrelated types", "could not calculate median of non-numeric or unrelated types",

View File

@ -36,7 +36,7 @@ impl WholeStreamCommand for SubCommand {
fn to_byte(value: &Value) -> Option<Value> { fn to_byte(value: &Value) -> Option<Value> {
match &value.value { match &value.value {
UntaggedValue::Primitive(Primitive::Int(num)) => { 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, _ => None,
} }
@ -59,7 +59,7 @@ pub fn product(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
Value { Value {
value: UntaggedValue::Primitive(Primitive::Filesize(num)), value: UntaggedValue::Primitive(Primitive::Filesize(num)),
.. ..
} => UntaggedValue::int(num.clone()).into_untagged_value(), } => UntaggedValue::int(*num as i64).into_untagged_value(),
other => other.clone(), other => other.clone(),
}) })
.collect::<Vec<_>>(), .collect::<Vec<_>>(),

View File

@ -62,7 +62,7 @@ fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
0 0
}; };
let mapped = input.map(move |val| match val.value { 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)) => { UntaggedValue::Primitive(Primitive::Decimal(val)) => {
round_big_decimal(val, precision.into()) round_big_decimal(val, precision.into())
} }
@ -72,18 +72,22 @@ fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
} }
fn round_big_int(val: BigInt) -> Value { 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 { fn round_big_decimal(val: BigDecimal, precision: i64) -> Value {
if precision > 0 { if precision > 0 {
UntaggedValue::decimal(val.with_scale(precision + 1).round(precision)).into() UntaggedValue::decimal(val.with_scale(precision + 1).round(precision)).into()
} else { } else {
let (rounded, _) = val let rounded = val.with_scale(precision + 1).round(precision).to_i64();
.with_scale(precision + 1)
.round(precision) match rounded {
.as_bigint_and_exponent(); Some(x) => UntaggedValue::int(x).into(),
UntaggedValue::int(rounded).into() None => UntaggedValue::Error(ShellError::untagged_runtime_error(
"Number too larger to round to 64-bit int",
))
.into(),
}
} }
} }

View File

@ -46,10 +46,18 @@ fn operate(args: CommandArgs) -> OutputStream {
fn sqrt_big_decimal(val: BigDecimal) -> Value { fn sqrt_big_decimal(val: BigDecimal) -> Value {
let squared = val.sqrt(); let squared = val.sqrt();
match squared { match squared {
None => UntaggedValue::Error(ShellError::unexpected("Cant square root a negative number")) None => UntaggedValue::Error(ShellError::untagged_runtime_error(
.into(), "Can't square root a negative number",
))
.into(),
Some(val) if !val.is_integer() => UntaggedValue::decimal(val.normalized()).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(),
},
} }
} }

View File

@ -44,7 +44,7 @@ impl WholeStreamCommand for SubCommand {
fn to_byte(value: &Value) -> Option<Value> { fn to_byte(value: &Value) -> Option<Value> {
match &value.value { match &value.value {
UntaggedValue::Primitive(Primitive::Int(num)) => { 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, _ => None,
} }
@ -71,7 +71,7 @@ pub fn summation(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
Value { Value {
value: UntaggedValue::Primitive(Primitive::Filesize(num)), value: UntaggedValue::Primitive(Primitive::Filesize(num)),
.. ..
} => UntaggedValue::int(num.clone()).into_untagged_value(), } => UntaggedValue::int(*num as i64).into_untagged_value(),
other => other.clone(), other => other.clone(),
}) })
.collect::<Vec<_>>(), .collect::<Vec<_>>(),

View File

@ -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; 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( pub fn run_with_numerical_functions_on_stream(
input: InputStream, input: InputStream,
int_function: IntFunction, int_function: IntFunction,
big_int_function: BigIntFunction,
decimal_function: DecimalFunction, decimal_function: DecimalFunction,
default_function: DefaultFunction, default_function: DefaultFunction,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let mapped = input.map(move |val| match val.value { let mapped = input.map(move |val| match val.value {
UntaggedValue::Primitive(Primitive::Int(val)) => int_function(val), 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), UntaggedValue::Primitive(Primitive::Decimal(val)) => decimal_function(val),
other => default_function(other), other => default_function(other),
}); });

View File

@ -120,7 +120,7 @@ fn sum_of_squares(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
value: UntaggedValue::Primitive(Primitive::Filesize(num)), value: UntaggedValue::Primitive(Primitive::Filesize(num)),
.. ..
} => { } => {
UntaggedValue::from(Primitive::Int(num.clone())) UntaggedValue::from(Primitive::Int(*num as i64))
}, },
Value { Value {
value: UntaggedValue::Primitive(num), value: UntaggedValue::Primitive(num),

View File

@ -62,9 +62,9 @@ pub fn integer(args: CommandArgs) -> Result<OutputStream, ShellError> {
}; };
let (min, max) = if let Some(range) = &cmd_args.range { let (min, max) = if let Some(range) = &cmd_args.range {
(range.min_u64()?, range.max_u64()?) (range.min_i64()?, range.max_i64()?)
} else { } else {
(0, u64::MAX) (0, i64::MAX)
}; };
match min.cmp(&max) { match min.cmp(&max) {
@ -83,7 +83,7 @@ pub fn integer(args: CommandArgs) -> Result<OutputStream, ShellError> {
let mut thread_rng = thread_rng(); let mut thread_rng = thread_rng();
// add 1 to max, because gen_range is right-exclusive // add 1 to max, because gen_range is right-exclusive
let max = max.saturating_add(1); 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( Ok(OutputStream::one(
UntaggedValue::int(result).into_value(Tag::unknown()), UntaggedValue::int(result).into_value(Tag::unknown()),

View File

@ -109,6 +109,14 @@ pub fn action(
match &input.value { match &input.value {
UntaggedValue::Primitive(prim) => Ok(UntaggedValue::string(match prim { UntaggedValue::Primitive(prim) => Ok(UntaggedValue::string(match prim {
Primitive::Int(int) => { Primitive::Int(int) => {
if group_digits {
format_int(*int) // int.to_formatted_string(*locale)
} else {
int.to_string()
}
}
Primitive::BigInt(int) => {
if group_digits { if group_digits {
format_bigint(int) // int.to_formatted_string(*locale) format_bigint(int) // int.to_formatted_string(*locale)
} else { } else {
@ -121,7 +129,7 @@ pub fn action(
Primitive::Date(a_date) => a_date.format("%c").to_string(), Primitive::Date(a_date) => a_date.format("%c").to_string(),
Primitive::FilePath(a_filepath) => a_filepath.as_path().display().to_string(), Primitive::FilePath(a_filepath) => a_filepath.as_path().display().to_string(),
Primitive::Filesize(a_filesize) => { 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 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 { fn format_bigint(int: &BigInt) -> String {
format!("{}", int) format!("{}", int)

View File

@ -147,12 +147,12 @@ fn action(
if *end { if *end {
if let Some(result) = s[start_index..end_index].rfind(&**pattern) { 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 { } else {
Ok(UntaggedValue::int(-1).into_value(tag)) Ok(UntaggedValue::int(-1).into_value(tag))
} }
} else if let Some(result) = s[start_index..end_index].find(&**pattern) { } 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 { } else {
Ok(UntaggedValue::int(-1).into_value(tag)) Ok(UntaggedValue::int(-1).into_value(tag))
} }

View File

@ -81,7 +81,7 @@ fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> {
fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> { fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {
match &input.value { match &input.value {
UntaggedValue::Primitive(Primitive::String(s)) => { UntaggedValue::Primitive(Primitive::String(s)) => {
Ok(UntaggedValue::int(s.len()).into_value(tag)) Ok(UntaggedValue::int(s.len() as i64).into_value(tag))
} }
other => { other => {
let got = format!("got {}", other.type_name()); let got = format!("got {}", other.type_name());

View File

@ -9,9 +9,6 @@ use nu_protocol::{
use nu_source::{Tag, Tagged}; use nu_source::{Tag, Tagged};
use nu_value_ext::ValueExt; use nu_value_ext::ValueExt;
use num_bigint::BigInt;
use num_traits::Num;
struct Arguments { struct Arguments {
radix: Option<Tagged<u32>>, radix: Option<Tagged<u32>>,
column_paths: Vec<ColumnPath>, column_paths: Vec<ColumnPath>,
@ -106,7 +103,7 @@ fn action(input: &Value, tag: impl Into<Tag>, radix: u32) -> Result<Value, Shell
let out = match trimmed { let out = match trimmed {
b if b.starts_with("0b") => { b if b.starts_with("0b") => {
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, Ok(n) => n,
Err(reason) => { Err(reason) => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
@ -119,7 +116,7 @@ fn action(input: &Value, tag: impl Into<Tag>, radix: u32) -> Result<Value, Shell
UntaggedValue::int(num) UntaggedValue::int(num)
} }
h if h.starts_with("0x") => { h if h.starts_with("0x") => {
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, Ok(n) => n,
Err(reason) => { Err(reason) => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
@ -132,7 +129,7 @@ fn action(input: &Value, tag: impl Into<Tag>, radix: u32) -> Result<Value, Shell
UntaggedValue::int(num) UntaggedValue::int(num)
} }
_ => { _ => {
let num = match BigInt::from_str_radix(trimmed, radix) { let num = match i64::from_str_radix(trimmed, radix) {
Ok(n) => n, Ok(n) => n,
Err(reason) => { Err(reason) => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(

View File

@ -35,13 +35,23 @@ impl WholeStreamCommand for TermSize {
match size { match size {
Some((w, h)) => { Some((w, h)) => {
if wide && !tall { 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 { } 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 { } else {
let mut indexmap = IndexMap::with_capacity(2); let mut indexmap = IndexMap::with_capacity(2);
indexmap.insert("width".to_string(), UntaggedValue::int(w).into_value(&tag)); indexmap.insert(
indexmap.insert("height".to_string(), UntaggedValue::int(h).into_value(&tag)); "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); let value = UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag);
Ok(ActionStream::one(value)) Ok(ActionStream::one(value))
} }

View File

@ -109,14 +109,12 @@ pub fn clone_tagged_value(v: &Value) -> Value {
UntaggedValue::Primitive(Primitive::Decimal(f)) => { UntaggedValue::Primitive(Primitive::Decimal(f)) => {
UntaggedValue::Primitive(Primitive::Decimal(f.clone())) UntaggedValue::Primitive(Primitive::Decimal(f.clone()))
} }
UntaggedValue::Primitive(Primitive::Int(i)) => { UntaggedValue::Primitive(Primitive::Int(i)) => UntaggedValue::Primitive(Primitive::Int(*i)),
UntaggedValue::Primitive(Primitive::Int(i.clone()))
}
UntaggedValue::Primitive(Primitive::FilePath(x)) => { UntaggedValue::Primitive(Primitive::FilePath(x)) => {
UntaggedValue::Primitive(Primitive::FilePath(x.clone())) UntaggedValue::Primitive(Primitive::FilePath(x.clone()))
} }
UntaggedValue::Primitive(Primitive::Filesize(b)) => { 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)) => {
UntaggedValue::Primitive(Primitive::Date(*d)) UntaggedValue::Primitive(Primitive::Date(*d))

View File

@ -82,6 +82,9 @@ pub fn value_to_json_value(v: &Value) -> Result<serde_json::Value, ShellError> {
} }
UntaggedValue::Primitive(Primitive::Int(i)) => { 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::<i64>::coerce_into( serde_json::Value::Number(serde_json::Number::from(CoerceInto::<i64>::coerce_into(
i.tagged(&v.tag), i.tagged(&v.tag),
"converting to JSON number", "converting to JSON number",
@ -96,12 +99,9 @@ pub fn value_to_json_value(v: &Value) -> Result<serde_json::Value, ShellError> {
UnspannedPathMember::String(string) => { UnspannedPathMember::String(string) => {
Ok(serde_json::Value::String(string.clone())) Ok(serde_json::Value::String(string.clone()))
} }
UnspannedPathMember::Int(int) => Ok(serde_json::Value::Number( UnspannedPathMember::Int(int) => {
serde_json::Number::from(CoerceInto::<i64>::coerce_into( Ok(serde_json::Value::Number(serde_json::Number::from(*int)))
int.tagged(&v.tag), }
"converting to JSON number",
)?),
)),
}) })
.collect::<Result<Vec<serde_json::Value>, ShellError>>()?, .collect::<Result<Vec<serde_json::Value>, ShellError>>()?,
), ),

View File

@ -50,7 +50,8 @@ fn helper(v: &Value) -> Result<toml::Value, ShellError> {
UntaggedValue::Primitive(Primitive::Decimal(f)) => { UntaggedValue::Primitive(Primitive::Decimal(f)) => {
toml::Value::Float(f.tagged(&v.tag).coerce_into("converting to TOML float")?) 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")?) toml::Value::Integer(i.tagged(&v.tag).coerce_into("converting to TOML integer")?)
} }
UntaggedValue::Primitive(Primitive::Nothing) => { UntaggedValue::Primitive(Primitive::Nothing) => {
@ -65,10 +66,7 @@ fn helper(v: &Value) -> Result<toml::Value, ShellError> {
path.iter() path.iter()
.map(|x| match &x.unspanned { .map(|x| match &x.unspanned {
UnspannedPathMember::String(string) => Ok(toml::Value::String(string.clone())), UnspannedPathMember::String(string) => Ok(toml::Value::String(string.clone())),
UnspannedPathMember::Int(int) => Ok(toml::Value::Integer( UnspannedPathMember::Int(int) => Ok(toml::Value::Integer(*int)),
int.tagged(&v.tag)
.coerce_into("converting to TOML integer")?,
)),
}) })
.collect::<Result<Vec<toml::Value>, ShellError>>()?, .collect::<Result<Vec<toml::Value>, ShellError>>()?,
), ),

View File

@ -51,6 +51,9 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
})?)) })?))
} }
UntaggedValue::Primitive(Primitive::Int(i)) => { 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::<i64>::coerce_into( serde_yaml::Value::Number(serde_yaml::Number::from(CoerceInto::<i64>::coerce_into(
i.tagged(&v.tag), i.tagged(&v.tag),
"converting to YAML number", "converting to YAML number",
@ -67,12 +70,9 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
UnspannedPathMember::String(string) => { UnspannedPathMember::String(string) => {
out.push(serde_yaml::Value::String(string.clone())) out.push(serde_yaml::Value::String(string.clone()))
} }
UnspannedPathMember::Int(int) => out.push(serde_yaml::Value::Number( UnspannedPathMember::Int(int) => {
serde_yaml::Number::from(CoerceInto::<i64>::coerce_into( out.push(serde_yaml::Value::Number(serde_yaml::Number::from(*int)))
int.tagged(&member.span), }
"converting to YAML number",
)?),
)),
} }
} }

View File

@ -126,7 +126,7 @@ fn uniq(args: CommandArgs) -> Result<ActionStream, ShellError> {
UntaggedValue::Row(mut row) => { UntaggedValue::Row(mut row) => {
row.entries.insert( row.entries.insert(
"count".to_string(), "count".to_string(),
UntaggedValue::int(item.1).into_untagged_value(), UntaggedValue::int(item.1 as i64).into_untagged_value(),
); );
Value { Value {
value: UntaggedValue::Row(row), value: UntaggedValue::Row(row),
@ -141,7 +141,7 @@ fn uniq(args: CommandArgs) -> Result<ActionStream, ShellError> {
); );
map.insert( map.insert(
"count".to_string(), "count".to_string(),
UntaggedValue::int(item.1).into_untagged_value(), UntaggedValue::int(item.1 as i64).into_untagged_value(),
); );
Value { Value {
value: UntaggedValue::row(map), value: UntaggedValue::row(map),

View File

@ -10,7 +10,6 @@ use nu_protocol::{
use nu_source::{Span, Tag}; use nu_source::{Span, Tag};
use nu_value_ext::ValueExt; use nu_value_ext::ValueExt;
use num_bigint::BigInt; use num_bigint::BigInt;
use num_traits::Zero;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, new, 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<Tag>) -> Val
} }
pub enum CompareValues { pub enum CompareValues {
Ints(BigInt, BigInt), Ints(i64, i64),
Filesizes(u64, u64),
BigInts(BigInt, BigInt),
Decimals(BigDecimal, BigDecimal), Decimals(BigDecimal, BigDecimal),
String(String, String), String(String, String),
Date(DateTime<FixedOffset>, DateTime<FixedOffset>), Date(DateTime<FixedOffset>, DateTime<FixedOffset>),
@ -83,7 +84,9 @@ pub enum CompareValues {
impl CompareValues { impl CompareValues {
pub fn compare(&self) -> std::cmp::Ordering { pub fn compare(&self) -> std::cmp::Ordering {
match self { match self {
CompareValues::BigInts(left, right) => left.cmp(right),
CompareValues::Ints(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::Decimals(left, right) => left.cmp(right),
CompareValues::String(left, right) => left.cmp(right), CompareValues::String(left, right) => left.cmp(right),
CompareValues::Date(left, right) => left.cmp(right), CompareValues::Date(left, right) => left.cmp(right),
@ -125,22 +128,34 @@ pub fn coerce_compare_primitive(
use Primitive::*; use Primitive::*;
Ok(match (left, right) { Ok(match (left, right) {
(Int(left), Int(right)) => CompareValues::Ints(left.clone(), right.clone()), (Int(left), Int(right)) => CompareValues::Ints(*left, *right),
(Int(left), Decimal(right)) => { (Int(left), BigInt(right)) => {
CompareValues::Decimals(BigDecimal::zero() + left, right.clone()) 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), Decimal(right)) => CompareValues::Decimals(left.clone(), right.clone()),
(Decimal(left), Int(right)) => { (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())) CompareValues::Decimals(left.clone(), BigDecimal::from(right.clone()))
} }
(Filesize(left), Filesize(right)) => CompareValues::Ints(left.clone(), right.clone()), (Decimal(left), Filesize(right)) => {
(Filesize(left), Int(right)) => CompareValues::Ints(left.clone(), right.clone()), CompareValues::Decimals(left.clone(), BigDecimal::from(*right))
}
(Filesize(left), Filesize(right)) => CompareValues::Filesizes(*left, *right),
(Filesize(left), Decimal(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), (Nothing, Nothing) => CompareValues::Booleans(true, true),
(String(left), String(right)) => CompareValues::String(left.clone(), right.clone()), (String(left), String(right)) => CompareValues::String(left.clone(), right.clone()),

View File

@ -21,10 +21,11 @@ pub struct InlineRange {
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)] #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
pub enum InlineShape { pub enum InlineShape {
Nothing, Nothing,
Int(BigInt), Int(i64),
BigInt(BigInt),
Decimal(BigDecimal), Decimal(BigDecimal),
Range(Box<InlineRange>), Range(Box<InlineRange>),
Bytesize(BigInt), Bytesize(u64),
String(String), String(String),
Line(String), Line(String),
ColumnPath(ColumnPath), ColumnPath(ColumnPath),
@ -74,7 +75,8 @@ impl InlineShape {
pub fn from_primitive(primitive: &Primitive) -> InlineShape { pub fn from_primitive(primitive: &Primitive) -> InlineShape {
match primitive { match primitive {
Primitive::Nothing => InlineShape::Nothing, 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) => { Primitive::Range(range) => {
let (left, left_inclusion) = &range.from; let (left, left_inclusion) = &range.from;
let (right, right_inclusion) = &range.to; let (right, right_inclusion) = &range.to;
@ -85,7 +87,7 @@ impl InlineShape {
})) }))
} }
Primitive::Decimal(decimal) => InlineShape::Decimal(decimal.clone()), 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::String(string) => InlineShape::String(string.clone()),
Primitive::ColumnPath(path) => InlineShape::ColumnPath(path.clone()), Primitive::ColumnPath(path) => InlineShape::ColumnPath(path.clone()),
Primitive::GlobPattern(pattern) => InlineShape::GlobPattern(pattern.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; use bigdecimal::ToPrimitive;
// get the config value, if it doesn't exist make it 'auto' so it works how it originally did // 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 { match &self.shape {
InlineShape::Nothing => DbgDocBldr::blank(), InlineShape::Nothing => DbgDocBldr::blank(),
InlineShape::Int(int) => DbgDocBldr::primitive(format!("{}", int)), InlineShape::Int(int) => DbgDocBldr::primitive(format!("{}", int)),
InlineShape::BigInt(int) => DbgDocBldr::primitive(format!("{}", int)),
InlineShape::Decimal(decimal) => DbgDocBldr::description(format_primitive( InlineShape::Decimal(decimal) => DbgDocBldr::description(format_primitive(
&Primitive::Decimal(decimal.clone()), &Primitive::Decimal(decimal.clone()),
None, None,
@ -258,7 +261,7 @@ impl PrettyDebug for FormatInlineShape {
+ right.clone().format().pretty() + right.clone().format().pretty()
} }
InlineShape::Bytesize(bytesize) => { InlineShape::Bytesize(bytesize) => {
let bytes = InlineShape::format_bytes(bytesize, None); let bytes = InlineShape::format_bytes(*bytesize, None);
bytes.0 bytes.0
} }
InlineShape::String(string) => DbgDocBldr::primitive(string), InlineShape::String(string) => DbgDocBldr::primitive(string),

View File

@ -93,7 +93,8 @@ fn helper(v: &Value) -> Result<toml::Value, ShellError> {
UntaggedValue::Primitive(Primitive::Decimal(f)) => { UntaggedValue::Primitive(Primitive::Decimal(f)) => {
toml::Value::Float(f.tagged(&v.tag).coerce_into("converting to TOML float")?) 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")?) toml::Value::Integer(i.tagged(&v.tag).coerce_into("converting to TOML integer")?)
} }
UntaggedValue::Primitive(Primitive::Nothing) => { UntaggedValue::Primitive(Primitive::Nothing) => {
@ -108,10 +109,7 @@ fn helper(v: &Value) -> Result<toml::Value, ShellError> {
path.iter() path.iter()
.map(|x| match &x.unspanned { .map(|x| match &x.unspanned {
UnspannedPathMember::String(string) => Ok(toml::Value::String(string.clone())), UnspannedPathMember::String(string) => Ok(toml::Value::String(string.clone())),
UnspannedPathMember::Int(int) => Ok(toml::Value::Integer( UnspannedPathMember::Int(int) => Ok(toml::Value::Integer(*int)),
int.tagged(&v.tag)
.coerce_into("converting to TOML integer")?,
)),
}) })
.collect::<Result<Vec<toml::Value>, ShellError>>()?, .collect::<Result<Vec<toml::Value>, ShellError>>()?,
), ),

View File

@ -8,6 +8,7 @@ pub fn number(number: impl Into<Number>) -> Primitive {
let number = number.into(); let number = number.into();
match number { match number {
Number::BigInt(int) => Primitive::BigInt(int),
Number::Int(int) => Primitive::Int(int), Number::Int(int) => Primitive::Int(int),
Number::Decimal(decimal) => Primitive::Decimal(decimal), Number::Decimal(decimal) => Primitive::Decimal(decimal),
} }

View File

@ -52,6 +52,10 @@ impl ExtractType for i64 {
&Value { &Value {
value: UntaggedValue::Primitive(Primitive::Int(int)), 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")?), } => Ok(int.tagged(&value.tag).coerce_into("converting to i64")?),
other => Err(ShellError::type_error("Integer", other.spanned_type_name())), other => Err(ShellError::type_error("Integer", other.spanned_type_name())),
} }
@ -64,10 +68,13 @@ impl ExtractType for u64 {
match &value { match &value {
&Value { &Value {
value: UntaggedValue::Primitive(Primitive::Int(int)), value: UntaggedValue::Primitive(Primitive::BigInt(int)),
.. ..
} => Ok(int.tagged(&value.tag).coerce_into("converting to u64")?), } => 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(),
)),
} }
} }
} }

View File

@ -28,7 +28,7 @@ impl Labels {
} }
pub fn grouping_total(&self) -> Value { 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<Item = &String> { pub fn splits(&self) -> impl Iterator<Item = &String> {
@ -36,7 +36,7 @@ impl Labels {
} }
pub fn splits_total(&self) -> Value { pub fn splits_total(&self) -> Value {
UntaggedValue::int(self.y.len()).into_untagged_value() UntaggedValue::big_int(self.y.len()).into_untagged_value()
} }
} }

View File

@ -8,6 +8,7 @@ use nu_protocol::ShellTypeName;
use nu_protocol::{Primitive, Type, UntaggedValue}; use nu_protocol::{Primitive, Type, UntaggedValue};
use nu_source::{DebugDocBuilder, PrettyDebug, Span, Tagged}; use nu_source::{DebugDocBuilder, PrettyDebug, Span, Tagged};
use nu_table::TextStyle; use nu_table::TextStyle;
use num_bigint::BigInt;
use num_traits::{ToPrimitive, Zero}; use num_traits::{ToPrimitive, Zero};
use std::collections::HashMap; use std::collections::HashMap;
@ -81,12 +82,18 @@ pub fn unsafe_compute_values(
match (left, right) { match (left, right) {
(UntaggedValue::Primitive(lhs), UntaggedValue::Primitive(rhs)) => match (lhs, rhs) { (UntaggedValue::Primitive(lhs), UntaggedValue::Primitive(rhs)) => match (lhs, rhs) {
(Primitive::Filesize(x), Primitive::Int(y)) => match operator { (Primitive::Filesize(x), Primitive::Int(y)) => match operator {
Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::Filesize(x * y))), Operator::Multiply => {
Operator::Divide => Ok(UntaggedValue::Primitive(Primitive::Filesize(x / y))), 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())), _ => Err((left.type_name(), right.type_name())),
}, },
(Primitive::Int(x), Primitive::Filesize(y)) => match operator { (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())),
}, },
_ => 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))) Ok(UntaggedValue::Primitive(Primitive::Filesize(result)))
} }
(Primitive::Filesize(x), Primitive::Int(y)) => match operator { (Primitive::Filesize(x), Primitive::Int(y)) => match operator {
Operator::Multiply => Ok(UntaggedValue::Primitive(Primitive::Filesize(x * y))), Operator::Multiply => {
Operator::Divide => Ok(UntaggedValue::Primitive(Primitive::Filesize(x / y))), 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())), _ => Err((left.type_name(), right.type_name())),
}, },
(Primitive::Int(x), Primitive::Filesize(y)) => match operator { (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())),
}, },
(Primitive::Int(x), Primitive::Int(y)) => match operator { (Primitive::Int(x), Primitive::Int(y)) => match operator {
@ -126,12 +139,11 @@ pub fn compute_values(
Operator::Divide => { Operator::Divide => {
if y.is_zero() { if y.is_zero() {
Ok(zero_division_error()) 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))) Ok(UntaggedValue::Primitive(Primitive::Int(x / y)))
} else { } else {
Ok(UntaggedValue::Primitive(Primitive::Decimal( Ok(UntaggedValue::Primitive(Primitive::Decimal(
bigdecimal::BigDecimal::from(x.clone()) bigdecimal::BigDecimal::from(*x) / bigdecimal::BigDecimal::from(*y),
/ bigdecimal::BigDecimal::from(y.clone()),
))) )))
} }
} }
@ -151,7 +163,176 @@ pub fn compute_values(
} }
_ => Err((left.type_name(), right.type_name())), _ => 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)) => { (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 { let result = match operator {
Operator::Plus => Ok(x + bigdecimal::BigDecimal::from(y.clone())), Operator::Plus => Ok(x + bigdecimal::BigDecimal::from(y.clone())),
Operator::Minus => 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))) Ok(UntaggedValue::Primitive(Primitive::Decimal(result)))
} }
(Primitive::Int(x), Primitive::Decimal(y)) => { (Primitive::BigInt(x), Primitive::Decimal(y)) => {
let result = match operator { let result = match operator {
Operator::Plus => Ok(bigdecimal::BigDecimal::from(x.clone()) + y), Operator::Plus => Ok(bigdecimal::BigDecimal::from(x.clone()) + y),
Operator::Minus => Ok(bigdecimal::BigDecimal::from(x.clone()) - y), Operator::Minus => Ok(bigdecimal::BigDecimal::from(x.clone()) - y),

View File

@ -439,6 +439,10 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut ConfigDeserializer<'de> {
Value { Value {
value: UntaggedValue::Primitive(Primitive::Int(int)), value: UntaggedValue::Primitive(Primitive::Int(int)),
.. ..
} => visit::<Tagged<i64>, _>(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")?; let i: i64 = int.tagged(value.val.tag).coerce_into("converting to i64")?;
visit::<Tagged<i64>, _>(i.tagged(tag), name, fields, visitor) visit::<Tagged<i64>, _>(i.tagged(tag), name, fields, visitor)

View File

@ -212,7 +212,10 @@ fn evaluate_literal(literal: &hir::Literal, span: Span) -> Value {
.into_value(span) .into_value(span)
} }
hir::Literal::Number(int) => match int { 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) => { nu_protocol::hir::Number::Decimal(d) => {
UntaggedValue::decimal(d.clone()).into_value(span) UntaggedValue::decimal(d.clone()).into_value(span)
} }

View File

@ -7,6 +7,7 @@ use nu_protocol::{
hir::CapturedBlock, ColumnPath, Dictionary, Primitive, Range, UntaggedValue, Value, hir::CapturedBlock, ColumnPath, Dictionary, Primitive, Range, UntaggedValue, Value,
}; };
use nu_source::{Tagged, TaggedItem}; use nu_source::{Tagged, TaggedItem};
use num_bigint::BigInt;
pub trait FromValue: Sized { pub trait FromValue: Sized {
fn from_value(v: &Value) -> Result<Self, ShellError>; fn from_value(v: &Value) -> Result<Self, ShellError>;
@ -24,12 +25,12 @@ impl FromValue for num_bigint::BigInt {
Value { Value {
value: UntaggedValue::Primitive(Primitive::Int(i)), value: UntaggedValue::Primitive(Primitive::Int(i)),
.. ..
} } => Ok(BigInt::from(*i)),
| Value { Value {
value: UntaggedValue::Primitive(Primitive::Filesize(i)), value: UntaggedValue::Primitive(Primitive::Filesize(i)),
.. ..
} } => Ok(BigInt::from(*i)),
| Value { Value {
value: UntaggedValue::Primitive(Primitive::Duration(i)), value: UntaggedValue::Primitive(Primitive::Duration(i)),
.. ..
} => Ok(i.clone()), } => Ok(i.clone()),

View File

@ -57,8 +57,8 @@ pub fn parse_simple_column_path(
lite_arg.span.start() + idx, lite_arg.span.start() + idx,
); );
if let Ok(row_number) = current_part.parse::<u64>() { if let Ok(row_number) = current_part.parse::<i64>() {
output.push(Member::Int(BigInt::from(row_number), part_span)); output.push(Member::Int(row_number, part_span));
} else { } else {
let trimmed = trim_quotes(&current_part); let trimmed = trim_quotes(&current_part);
output.push(Member::Bare(trimmed.clone().spanned(part_span))); 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() + start_index,
lite_arg.span.start() + last_index + 1, lite_arg.span.start() + last_index + 1,
); );
if let Ok(row_number) = current_part.parse::<u64>() { if let Ok(row_number) = current_part.parse::<i64>() {
output.push(Member::Int(BigInt::from(row_number), part_span)); output.push(Member::Int(row_number, part_span));
} else { } else {
let current_part = trim_quotes(&current_part); let current_part = trim_quotes(&current_part);
output.push(Member::Bare(current_part.spanned(part_span))); 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('$') { } else if head.is_none() && current_part.starts_with('$') {
// We have the variable head // We have the variable head
head = Some(Expression::variable(current_part.clone(), part_span)) head = Some(Expression::variable(current_part.clone(), part_span))
} else if let Ok(row_number) = current_part.parse::<u64>() { } else if let Ok(row_number) = current_part.parse::<i64>() {
output.push( output.push(UnspannedPathMember::Int(row_number).into_path_member(part_span));
UnspannedPathMember::Int(BigInt::from(row_number)).into_path_member(part_span),
);
} else { } else {
let current_part = trim_quotes(&current_part); let current_part = trim_quotes(&current_part);
output.push( output.push(
@ -165,18 +163,14 @@ pub fn parse_full_column_path(
head = Some(invoc.expr); head = Some(invoc.expr);
} else if current_part.starts_with('$') { } else if current_part.starts_with('$') {
head = Some(Expression::variable(current_part, lite_arg.span)); head = Some(Expression::variable(current_part, lite_arg.span));
} else if let Ok(row_number) = current_part.parse::<u64>() { } else if let Ok(row_number) = current_part.parse::<i64>() {
output.push( output.push(UnspannedPathMember::Int(row_number).into_path_member(part_span));
UnspannedPathMember::Int(BigInt::from(row_number)).into_path_member(part_span),
);
} else { } else {
let current_part = trim_quotes(&current_part); let current_part = trim_quotes(&current_part);
output.push(UnspannedPathMember::String(current_part).into_path_member(part_span)); output.push(UnspannedPathMember::String(current_part).into_path_member(part_span));
} }
} else if let Ok(row_number) = current_part.parse::<u64>() { } else if let Ok(row_number) = current_part.parse::<i64>() {
output.push( output.push(UnspannedPathMember::Int(row_number).into_path_member(part_span));
UnspannedPathMember::Int(BigInt::from(row_number)).into_path_member(part_span),
);
} else { } else {
let current_part = trim_quotes(&current_part); let current_part = trim_quotes(&current_part);
output.push(UnspannedPathMember::String(current_part).into_path_member(part_span)); output.push(UnspannedPathMember::String(current_part).into_path_member(part_span));
@ -771,11 +765,16 @@ fn parse_arg(
match expected_type { match expected_type {
SyntaxShape::Number => { SyntaxShape::Number => {
if let Ok(x) = lite_arg.item.parse::<BigInt>() { if let Ok(x) = lite_arg.item.parse::<i64>() {
( (
SpannedExpression::new(Expression::integer(x), lite_arg.span), SpannedExpression::new(Expression::integer(x), lite_arg.span),
None, None,
) )
} else if let Ok(x) = lite_arg.item.parse::<BigInt>() {
(
SpannedExpression::new(Expression::big_integer(x), lite_arg.span),
None,
)
} else if let Ok(x) = lite_arg.item.parse::<BigDecimal>() { } else if let Ok(x) = lite_arg.item.parse::<BigDecimal>() {
( (
SpannedExpression::new(Expression::decimal(x), lite_arg.span), SpannedExpression::new(Expression::decimal(x), lite_arg.span),
@ -789,11 +788,16 @@ fn parse_arg(
} }
} }
SyntaxShape::Int => { SyntaxShape::Int => {
if let Ok(x) = lite_arg.item.parse::<BigInt>() { if let Ok(x) = lite_arg.item.parse::<i64>() {
( (
SpannedExpression::new(Expression::integer(x), lite_arg.span), SpannedExpression::new(Expression::integer(x), lite_arg.span),
None, None,
) )
} else if let Ok(x) = lite_arg.item.parse::<BigInt>() {
(
SpannedExpression::new(Expression::big_integer(x), lite_arg.span),
None,
)
} else { } else {
( (
garbage(lite_arg.span), garbage(lite_arg.span),

View File

@ -10,18 +10,18 @@ use crate::{hir, Dictionary, PositionalType, Primitive, SyntaxShape, UntaggedVal
use crate::{PathMember, ShellTypeName}; use crate::{PathMember, ShellTypeName};
use derive_new::new; use derive_new::new;
use nu_errors::ParseError; use nu_errors::{ParseError, ShellError};
use nu_source::{ use nu_source::{
DbgDocBldr, DebugDocBuilder, HasSpan, PrettyDebug, PrettyDebugRefineKind, PrettyDebugWithSource, DbgDocBldr, DebugDocBuilder, HasSpan, PrettyDebug, PrettyDebugRefineKind, PrettyDebugWithSource,
}; };
use nu_source::{IntoSpanned, Span, Spanned, SpannedItem, Tag}; use nu_source::{IntoSpanned, Span, Spanned, SpannedItem, Tag};
use bigdecimal::BigDecimal; use bigdecimal::{BigDecimal, ToPrimitive};
use indexmap::IndexMap; use indexmap::IndexMap;
use log::trace; use log::trace;
use num_bigint::{BigInt, ToBigInt}; use num_bigint::{BigInt, ToBigInt};
use num_traits::identities::Zero; use num_traits::identities::Zero;
use num_traits::{FromPrimitive, ToPrimitive}; use num_traits::FromPrimitive;
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
pub struct InternalCommand { pub struct InternalCommand {
@ -363,7 +363,7 @@ pub enum Unit {
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Deserialize, Serialize)] #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Deserialize, Serialize)]
pub enum Member { pub enum Member {
String(/* outer */ Span, /* inner */ Span), String(/* outer */ Span, /* inner */ Span),
Int(BigInt, Span), Int(i64, Span),
Bare(Spanned<String>), Bare(Spanned<String>),
} }
@ -371,7 +371,7 @@ impl Member {
pub fn to_path_member(&self) -> PathMember { pub fn to_path_member(&self) -> PathMember {
match self { match self {
//Member::String(outer, inner) => PathMember::string(inner.slice(source), *outer), //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) => { Member::Bare(spanned_string) => {
PathMember::string(spanned_string.item.clone(), spanned_string.span) 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)] #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Deserialize, Serialize)]
pub enum Number { pub enum Number {
Int(BigInt), BigInt(BigInt),
Int(i64),
Decimal(BigDecimal), Decimal(BigDecimal),
} }
impl Number {
pub fn to_i64(&self) -> Result<i64, ShellError> {
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 { impl PrettyDebug for Number {
fn pretty(&self) -> DebugDocBuilder { fn pretty(&self) -> DebugDocBuilder {
match self { match self {
Number::BigInt(int) => DbgDocBldr::primitive(int),
Number::Int(int) => DbgDocBldr::primitive(int), Number::Int(int) => DbgDocBldr::primitive(int),
Number::Decimal(decimal) => DbgDocBldr::primitive(decimal), Number::Decimal(decimal) => DbgDocBldr::primitive(decimal),
} }
@ -420,13 +439,13 @@ macro_rules! primitive_int {
$( $(
impl From<$ty> for Number { impl From<$ty> for Number {
fn from(int: $ty) -> Number { fn from(int: $ty) -> Number {
Number::Int(BigInt::zero() + int) Number::BigInt(BigInt::zero() + int)
} }
} }
impl From<&$ty> for Number { impl From<&$ty> for Number {
fn from(int: &$ty) -> 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 { fn mul(self, other: Number) -> Number {
match (self, other) { match (self, other) {
(Number::Int(a), Number::Int(b)) => Number::Int(a * b), (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::Int(a), Number::Decimal(b)) => Number::Decimal(BigDecimal::from(a) * b),
(Number::Decimal(a), Number::Int(b)) => Number::Decimal(a * BigDecimal::from(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), (Number::Decimal(a), Number::Decimal(b)) => Number::Decimal(a * b),
} }
} }
@ -482,6 +506,7 @@ impl std::ops::Mul<u32> for Number {
fn mul(self, other: u32) -> Number { fn mul(self, other: u32) -> Number {
match self { match self {
Number::BigInt(left) => Number::BigInt(left * (other as i64)),
Number::Int(left) => Number::Int(left * (other as i64)), Number::Int(left) => Number::Int(left * (other as i64)),
Number::Decimal(left) => Number::Decimal(left * BigDecimal::from(other)), Number::Decimal(left) => Number::Decimal(left * BigDecimal::from(other)),
} }
@ -491,7 +516,8 @@ impl std::ops::Mul<u32> for Number {
impl ToBigInt for Number { impl ToBigInt for Number {
fn to_bigint(&self) -> Option<BigInt> { fn to_bigint(&self) -> Option<BigInt> {
match self { 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(). // The BigDecimal to BigInt conversion always return Some().
// FIXME: This conversion might not be want we want, it just remove the scale. // FIXME: This conversion might not be want we want, it just remove the scale.
Number::Decimal(decimal) => decimal.to_bigint(), 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 { impl Unit {
pub fn as_str(self) -> &'static str { pub fn as_str(self) -> &'static str {
match self { match self {
@ -553,22 +560,18 @@ impl Unit {
let size = size.clone(); let size = size.clone();
match self { match self {
Unit::Byte => filesize(convert_number_to_u64(&size)), Unit::Byte => filesize(size),
Unit::Kilobyte => filesize(convert_number_to_u64(&size) * 1000), Unit::Kilobyte => filesize(size * 1000),
Unit::Megabyte => filesize(convert_number_to_u64(&size) * 1000 * 1000), Unit::Megabyte => filesize(size * 1000 * 1000),
Unit::Gigabyte => filesize(convert_number_to_u64(&size) * 1000 * 1000 * 1000), Unit::Gigabyte => filesize(size * 1000 * 1000 * 1000),
Unit::Terabyte => filesize(convert_number_to_u64(&size) * 1000 * 1000 * 1000 * 1000), Unit::Terabyte => filesize(size * 1000 * 1000 * 1000 * 1000),
Unit::Petabyte => { Unit::Petabyte => filesize(size * 1000 * 1000 * 1000 * 1000 * 1000),
filesize(convert_number_to_u64(&size) * 1000 * 1000 * 1000 * 1000 * 1000)
}
Unit::Kibibyte => filesize(convert_number_to_u64(&size) * 1024), Unit::Kibibyte => filesize(size * 1024),
Unit::Mebibyte => filesize(convert_number_to_u64(&size) * 1024 * 1024), Unit::Mebibyte => filesize(size * 1024 * 1024),
Unit::Gibibyte => filesize(convert_number_to_u64(&size) * 1024 * 1024 * 1024), Unit::Gibibyte => filesize(size * 1024 * 1024 * 1024),
Unit::Tebibyte => filesize(convert_number_to_u64(&size) * 1024 * 1024 * 1024 * 1024), Unit::Tebibyte => filesize(size * 1024 * 1024 * 1024 * 1024),
Unit::Pebibyte => { Unit::Pebibyte => filesize(size * 1024 * 1024 * 1024 * 1024 * 1024),
filesize(convert_number_to_u64(&size) * 1024 * 1024 * 1024 * 1024 * 1024)
}
Unit::Nanosecond => duration(size.to_bigint().expect("Conversion should never fail.")), Unit::Nanosecond => duration(size.to_bigint().expect("Conversion should never fail.")),
Unit::Microsecond => { Unit::Microsecond => {
@ -614,8 +617,19 @@ impl Unit {
} }
} }
pub fn filesize(size_in_bytes: impl Into<BigInt>) -> UntaggedValue { pub fn filesize(size_in_bytes: Number) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Filesize(size_in_bytes.into())) 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 { pub fn duration(nanos: BigInt) -> UntaggedValue {
@ -1069,10 +1083,14 @@ impl IntoSpanned for Expression {
} }
impl Expression { impl Expression {
pub fn integer(i: BigInt) -> Expression { pub fn integer(i: i64) -> Expression {
Expression::Literal(Literal::Number(Number::Int(i))) 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 { pub fn decimal(dec: BigDecimal) -> Expression {
Expression::Literal(Literal::Number(Number::Decimal(dec))) Expression::Literal(Literal::Number(Number::Decimal(dec)))
} }
@ -1116,7 +1134,7 @@ impl Expression {
pub fn unit(i: Spanned<i64>, unit: Spanned<Unit>) -> Expression { pub fn unit(i: Spanned<i64>, unit: Spanned<Unit>) -> Expression {
Expression::Literal(Literal::Size( Expression::Literal(Literal::Size(
Number::Int(BigInt::from(i.item)).spanned(i.span), Number::BigInt(BigInt::from(i.item)).spanned(i.span),
unit, unit,
)) ))
} }

View File

@ -30,6 +30,8 @@ pub enum Type {
Nothing, Nothing,
/// An integer-based value /// An integer-based value
Int, Int,
/// An big integer-based value
BigInt,
/// A range between two values /// A range between two values
Range(Box<RangeType>), Range(Box<RangeType>),
/// A decimal (floating point) value /// A decimal (floating point) value
@ -131,6 +133,7 @@ impl Type {
match primitive { match primitive {
Primitive::Nothing => Type::Nothing, Primitive::Nothing => Type::Nothing,
Primitive::Int(_) => Type::Int, Primitive::Int(_) => Type::Int,
Primitive::BigInt(_) => Type::BigInt,
Primitive::Range(range) => { Primitive::Range(range) => {
let (left_value, left_inclusion) = &range.from; let (left_value, left_inclusion) = &range.from;
let (right_value, right_inclusion) = &range.to; let (right_value, right_inclusion) = &range.to;
@ -199,6 +202,7 @@ impl PrettyDebug for Type {
match self { match self {
Type::Nothing => ty("nothing"), Type::Nothing => ty("nothing"),
Type::Int => ty("integer"), Type::Int => ty("integer"),
Type::BigInt => ty("big integer"),
Type::Range(range) => { Type::Range(range) => {
let (left, left_inclusion) = &range.from; let (left, left_inclusion) = &range.from;
let (right, right_inclusion) = &range.to; let (right, right_inclusion) = &range.to;

View File

@ -178,10 +178,15 @@ impl UntaggedValue {
} }
/// Helper for creating integer values /// Helper for creating integer values
pub fn int(i: impl Into<BigInt>) -> UntaggedValue { pub fn int(i: impl Into<i64>) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Int(i.into())) UntaggedValue::Primitive(Primitive::Int(i.into()))
} }
/// Helper for creating big integer values
pub fn big_int(i: impl Into<BigInt>) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::BigInt(i.into()))
}
/// Helper for creating glob pattern values /// Helper for creating glob pattern values
pub fn glob_pattern(s: impl Into<String>) -> UntaggedValue { pub fn glob_pattern(s: impl Into<String>) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::String(s.into())) UntaggedValue::Primitive(Primitive::String(s.into()))
@ -193,8 +198,8 @@ impl UntaggedValue {
} }
/// Helper for creating filesize values /// Helper for creating filesize values
pub fn filesize(s: impl Into<BigInt>) -> UntaggedValue { pub fn filesize(s: u64) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Filesize(s.into())) UntaggedValue::Primitive(Primitive::Filesize(s))
} }
/// Helper for creating decimal values /// Helper for creating decimal values
@ -339,22 +344,30 @@ impl Value {
} }
} }
/// View the Value as a Int (BigInt), if possible /// View the Value as a Int, if possible
pub fn as_int(&self) -> Result<BigInt, ShellError> { pub fn as_int(&self) -> Result<i64, ShellError> {
match &self.value { 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())), _ => Err(ShellError::type_error("bigint", self.spanned_type_name())),
} }
} }
/// View the Value as a Filesize (BigInt), if possible /// View the Value as a Int, if possible
pub fn as_filesize(&self) -> Result<BigInt, ShellError> { pub fn as_big_int(&self) -> Result<BigInt, ShellError> {
match &self.value { 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())), _ => Err(ShellError::type_error("bigint", self.spanned_type_name())),
} }
} }
/// View the Value as a Filesize (u64), if possible
pub fn as_filesize(&self) -> Result<u64, ShellError> {
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 /// View the Value as a Duration (BigInt), if possible
pub fn as_duration(&self) -> Result<BigInt, ShellError> { pub fn as_duration(&self) -> Result<BigInt, ShellError> {
match &self.value { match &self.value {
@ -808,8 +821,6 @@ pub trait U64Ext {
fn to_untagged_value(&self) -> UntaggedValue; fn to_untagged_value(&self) -> UntaggedValue;
fn to_value(&self, tag: Tag) -> Value; fn to_value(&self, tag: Tag) -> Value;
fn to_value_create_tag(&self) -> 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_untagged_value(&self) -> UntaggedValue;
fn to_duration_value(&self, tag: Tag) -> Value; fn to_duration_value(&self, tag: Tag) -> Value;
} }
@ -817,14 +828,7 @@ pub trait U64Ext {
impl U64Ext for u64 { impl U64Ext for u64 {
fn to_value(&self, the_tag: Tag) -> Value { fn to_value(&self, the_tag: Tag) -> Value {
Value { Value {
value: UntaggedValue::Primitive(Primitive::Int(BigInt::from(*self))), value: UntaggedValue::Primitive(Primitive::BigInt(BigInt::from(*self))),
tag: the_tag,
}
}
fn to_filesize_value(&self, the_tag: Tag) -> Value {
Value {
value: UntaggedValue::Primitive(Primitive::Filesize(BigInt::from(*self))),
tag: the_tag, tag: the_tag,
} }
} }
@ -839,7 +843,7 @@ impl U64Ext for u64 {
fn to_value_create_tag(&self) -> Value { fn to_value_create_tag(&self) -> Value {
let end = self.to_string().len(); let end = self.to_string().len();
Value { Value {
value: UntaggedValue::Primitive(Primitive::Int(BigInt::from(*self))), value: UntaggedValue::Primitive(Primitive::BigInt(BigInt::from(*self))),
tag: Tag { tag: Tag {
anchor: None, anchor: None,
span: Span::new(0, end), span: Span::new(0, end),
@ -848,11 +852,7 @@ impl U64Ext for u64 {
} }
fn to_untagged_value(&self) -> UntaggedValue { fn to_untagged_value(&self) -> UntaggedValue {
UntaggedValue::int(*self) UntaggedValue::big_int(*self)
}
fn to_filesize_untagged_value(&self) -> UntaggedValue {
UntaggedValue::filesize(*self)
} }
fn to_duration_untagged_value(&self) -> UntaggedValue { fn to_duration_untagged_value(&self) -> UntaggedValue {
@ -869,7 +869,7 @@ pub trait I64Ext {
impl I64Ext for i64 { impl I64Ext for i64 {
fn to_value(&self, the_tag: Tag) -> Value { fn to_value(&self, the_tag: Tag) -> Value {
Value { Value {
value: UntaggedValue::Primitive(Primitive::Int(BigInt::from(*self))), value: UntaggedValue::Primitive(Primitive::Int(*self)),
tag: the_tag, tag: the_tag,
} }
} }
@ -877,7 +877,7 @@ impl I64Ext for i64 {
fn to_value_create_tag(&self) -> Value { fn to_value_create_tag(&self) -> Value {
let end = self.to_string().len(); let end = self.to_string().len();
Value { Value {
value: UntaggedValue::Primitive(Primitive::Int(BigInt::from(*self))), value: UntaggedValue::Primitive(Primitive::Int(*self)),
tag: Tag { tag: Tag {
anchor: None, anchor: None,
span: Span::new(0, end), span: Span::new(0, end),

View File

@ -4,7 +4,6 @@ use nu_source::{
span_for_spanned_list, DbgDocBldr, DebugDocBuilder, HasFallibleSpan, PrettyDebug, Span, span_for_spanned_list, DbgDocBldr, DebugDocBuilder, HasFallibleSpan, PrettyDebug, Span,
Spanned, SpannedItem, Spanned, SpannedItem,
}; };
use num_bigint::BigInt;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::hir::{Expression, Literal, Member, SpannedExpression}; 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)] #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum UnspannedPathMember { pub enum UnspannedPathMember {
String(String), String(String),
Int(BigInt), Int(i64),
} }
impl UnspannedPathMember { impl UnspannedPathMember {
@ -125,8 +124,8 @@ impl PathMember {
} }
/// Create a numeric path member /// Create a numeric path member
pub fn int(int: impl Into<BigInt>, span: impl Into<Span>) -> PathMember { pub fn int(int: i64, span: impl Into<Span>) -> PathMember {
UnspannedPathMember::Int(int.into()).into_path_member(span) UnspannedPathMember::Int(int).into_path_member(span)
} }
pub fn as_string(&self) -> String { pub fn as_string(&self) -> String {
@ -160,8 +159,8 @@ fn parse(raw_column_path: &Spanned<String>) -> (SpannedExpression, Option<ParseE
raw_column_path.span.start() + idx, raw_column_path.span.start() + idx,
); );
if let Ok(row_number) = current_part.parse::<u64>() { if let Ok(row_number) = current_part.parse::<i64>() {
output.push(Member::Int(BigInt::from(row_number), part_span)); output.push(Member::Int(row_number, part_span));
} else { } else {
let trimmed = trim_quotes(&current_part); let trimmed = trim_quotes(&current_part);
output.push(Member::Bare(trimmed.clone().spanned(part_span))); output.push(Member::Bare(trimmed.clone().spanned(part_span)));
@ -180,8 +179,8 @@ fn parse(raw_column_path: &Spanned<String>) -> (SpannedExpression, Option<ParseE
raw_column_path.span.start() + start_index, raw_column_path.span.start() + start_index,
raw_column_path.span.start() + last_index + 1, raw_column_path.span.start() + last_index + 1,
); );
if let Ok(row_number) = current_part.parse::<u64>() { if let Ok(row_number) = current_part.parse::<i64>() {
output.push(Member::Int(BigInt::from(row_number), part_span)); output.push(Member::Int(row_number, part_span));
} else { } else {
let current_part = trim_quotes(&current_part); let current_part = trim_quotes(&current_part);
output.push(Member::Bare(current_part.spanned(part_span))); output.push(Member::Bare(current_part.spanned(part_span)));

View File

@ -11,7 +11,8 @@ impl std::convert::TryFrom<&Value> for i64 {
/// Convert to an i64 integer, if possible /// Convert to an i64 integer, if possible
fn try_from(value: &Value) -> Result<i64, ShellError> { fn try_from(value: &Value) -> Result<i64, ShellError> {
match &value.value { 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") int.tagged(&value.tag).coerce_into("converting to i64")
} }
_ => Err(ShellError::type_error("Integer", value.spanned_type_name())), _ => Err(ShellError::type_error("Integer", value.spanned_type_name())),

View File

@ -36,6 +36,7 @@ impl PrettyType for Primitive {
match self { match self {
Primitive::Nothing => ty("nothing"), Primitive::Nothing => ty("nothing"),
Primitive::Int(_) => ty("integer"), Primitive::Int(_) => ty("integer"),
Primitive::BigInt(_) => ty("big-integer"),
Primitive::Range(_) => ty("range"), Primitive::Range(_) => ty("range"),
Primitive::Decimal(_) => ty("decimal"), Primitive::Decimal(_) => ty("decimal"),
Primitive::Filesize(_) => ty("filesize"), Primitive::Filesize(_) => ty("filesize"),
@ -59,6 +60,7 @@ impl PrettyDebug for Primitive {
match self { match self {
Primitive::Nothing => DbgDocBldr::primitive("nothing"), Primitive::Nothing => DbgDocBldr::primitive("nothing"),
Primitive::Int(int) => prim(format_args!("{}", int)), Primitive::Int(int) => prim(format_args!("{}", int)),
Primitive::BigInt(int) => prim(format_args!("{}", int)),
Primitive::Decimal(decimal) => prim(format_args!("{}", decimal)), Primitive::Decimal(decimal) => prim(format_args!("{}", decimal)),
Primitive::Range(range) => { Primitive::Range(range) => {
let (left, left_inclusion) = &range.from; let (left, left_inclusion) = &range.from;

View File

@ -24,14 +24,16 @@ const NANOS_PER_SEC: u32 = 1_000_000_000;
pub enum Primitive { pub enum Primitive {
/// An empty value /// An empty value
Nothing, Nothing,
/// A common integer
Int(i64),
/// A "big int", an integer with arbitrarily large size (aka not limited to 64-bit) /// A "big int", an integer with arbitrarily large size (aka not limited to 64-bit)
#[serde(with = "serde_bigint")] #[serde(with = "serde_bigint")]
Int(BigInt), BigInt(BigInt),
/// A "big decimal", an decimal number with arbitrarily large size (aka not limited to 64-bit) /// A "big decimal", an decimal number with arbitrarily large size (aka not limited to 64-bit)
#[serde(with = "serde_bigdecimal")] #[serde(with = "serde_bigdecimal")]
Decimal(BigDecimal), Decimal(BigDecimal),
/// A count in the number of bytes, used as a filesize /// A count in the number of bytes, used as a filesize
Filesize(BigInt), Filesize(u64),
/// A string value /// A string value
String(String), String(String),
/// A path to travel to reach a value in a table /// A path to travel to reach a value in a table
@ -351,7 +353,7 @@ impl From<BigDecimal> for Primitive {
impl From<BigInt> for Primitive { impl From<BigInt> for Primitive {
/// Helper to convert from integers to a Primitive value /// Helper to convert from integers to a Primitive value
fn from(int: BigInt) -> Primitive { 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!(i8, Primitive::Int, i64::from_i8);
from_native_to_primitive!(i16, Primitive::Int, BigInt::from_i16); from_native_to_primitive!(i16, Primitive::Int, i64::from_i16);
from_native_to_primitive!(i32, Primitive::Int, BigInt::from_i32); from_native_to_primitive!(i32, Primitive::Int, i64::from_i32);
from_native_to_primitive!(i64, Primitive::Int, BigInt::from_i64); from_native_to_primitive!(i64, Primitive::Int, i64::from_i64);
from_native_to_primitive!(u8, Primitive::Int, BigInt::from_u8); from_native_to_primitive!(u8, Primitive::Int, i64::from_u8);
from_native_to_primitive!(u16, Primitive::Int, BigInt::from_u16); from_native_to_primitive!(u16, Primitive::Int, i64::from_u16);
from_native_to_primitive!(u32, Primitive::Int, BigInt::from_u32); from_native_to_primitive!(u32, Primitive::Int, i64::from_u32);
from_native_to_primitive!(u64, Primitive::Int, BigInt::from_u64); from_native_to_primitive!(u64, Primitive::BigInt, BigInt::from_u64);
from_native_to_primitive!(f32, Primitive::Decimal, BigDecimal::from_f32); from_native_to_primitive!(f32, Primitive::Decimal, BigDecimal::from_f32);
from_native_to_primitive!(f64, Primitive::Decimal, BigDecimal::from_f64); from_native_to_primitive!(f64, Primitive::Decimal, BigDecimal::from_f64);
@ -410,6 +412,7 @@ impl ShellTypeName for Primitive {
match self { match self {
Primitive::Nothing => "nothing", Primitive::Nothing => "nothing",
Primitive::Int(_) => "integer", Primitive::Int(_) => "integer",
Primitive::BigInt(_) => "big integer",
Primitive::Range(_) => "range", Primitive::Range(_) => "range",
Primitive::Decimal(_) => "decimal", Primitive::Decimal(_) => "decimal",
Primitive::Filesize(_) => "filesize(in bytes)", 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::Duration(duration) => format_duration(duration),
Primitive::Int(i) => i.to_string(), Primitive::Int(i) => i.to_string(),
Primitive::BigInt(i) => i.to_string(),
Primitive::Decimal(decimal) => { Primitive::Decimal(decimal) => {
// TODO: We should really pass the precision in here instead of hard coding it // TODO: We should really pass the precision in here instead of hard coding it
let decimal_string = decimal.to_string(); let decimal_string = decimal.to_string();

View File

@ -68,6 +68,36 @@ impl Range {
} }
} }
pub fn min_i64(&self) -> Result<i64, ShellError> {
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<i64, ShellError> {
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<usize, ShellError> { pub fn min_usize(&self) -> Result<usize, ShellError> {
let (from, range_incl) = &self.from; let (from, range_incl) = &self.from;

View File

@ -6,7 +6,11 @@ use nu_protocol::{PathMember, Primitive, UntaggedValue, Value};
use nu_source::{Span, TaggedItem}; use nu_source::{Span, TaggedItem};
use num_bigint::BigInt; use num_bigint::BigInt;
pub fn int(s: impl Into<BigInt>) -> Value { pub fn big_int(s: impl Into<BigInt>) -> Value {
UntaggedValue::big_int(s).into_untagged_value()
}
pub fn int(s: impl Into<i64>) -> Value {
UntaggedValue::int(s).into_untagged_value() UntaggedValue::int(s).into_untagged_value()
} }

View File

@ -648,7 +648,7 @@ pub fn as_column_path(value: &Value) -> Result<Tagged<ColumnPath>, ShellError> {
pub fn as_path_member(value: &Value) -> Result<PathMember, ShellError> { pub fn as_path_member(value: &Value) -> Result<PathMember, ShellError> {
match &value.value { match &value.value {
UntaggedValue::Primitive(primitive) => match primitive { 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)), Primitive::String(string) => Ok(PathMember::string(string, value.tag.span)),
other => Err(ShellError::type_error( other => Err(ShellError::type_error(
"path member", "path member",

View File

@ -372,6 +372,9 @@ pub fn value_to_json_value(v: &Value) -> Result<serde_json::Value, ShellError> {
})?, })?,
), ),
UntaggedValue::Primitive(Primitive::Int(i)) => { 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::<i64>::coerce_into( serde_json::Value::Number(serde_json::Number::from(CoerceInto::<i64>::coerce_into(
i.tagged(&v.tag), i.tagged(&v.tag),
"converting to JSON number", "converting to JSON number",
@ -386,12 +389,9 @@ pub fn value_to_json_value(v: &Value) -> Result<serde_json::Value, ShellError> {
UnspannedPathMember::String(string) => { UnspannedPathMember::String(string) => {
Ok(serde_json::Value::String(string.clone())) Ok(serde_json::Value::String(string.clone()))
} }
UnspannedPathMember::Int(int) => Ok(serde_json::Value::Number( UnspannedPathMember::Int(int) => {
serde_json::Number::from(CoerceInto::<i64>::coerce_into( Ok(serde_json::Value::Number(serde_json::Number::from(*int)))
int.tagged(&v.tag), }
"converting to JSON number",
)?),
)),
}) })
.collect::<Result<Vec<serde_json::Value>, ShellError>>()?, .collect::<Result<Vec<serde_json::Value>, ShellError>>()?,
), ),

View File

@ -23,7 +23,7 @@ pub async fn ps(tag: Tag, long: bool) -> Result<Vec<Value>, ShellError> {
for pid in result.into_iter() { for pid in result.into_iter() {
if let Some(result) = sys.get_process(pid) { if let Some(result) = sys.get_process(pid) {
let mut dict = TaggedDictBuilder::new(&tag); 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("name", UntaggedValue::string(result.name()));
dict.insert_untagged( dict.insert_untagged(
"status", "status",
@ -41,7 +41,7 @@ pub async fn ps(tag: Tag, long: bool) -> Result<Vec<Value>, ShellError> {
if long { if long {
if let Some(parent) = result.parent() { if let Some(parent) = result.parent() {
dict.insert_untagged("parent", UntaggedValue::int(parent)); dict.insert_untagged("parent", UntaggedValue::int(parent as i64));
} else { } else {
dict.insert_untagged("parent", UntaggedValue::nothing()); dict.insert_untagged("parent", UntaggedValue::nothing());
} }

View File

@ -80,7 +80,7 @@ pub fn cpu(sys: &mut System, tag: Tag) -> Option<UntaggedValue> {
"brand", "brand",
UntaggedValue::string(trim_cstyle_null(cpu.get_brand().to_string())), 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()); output.push(dict.into_value());
} }

View File

@ -41,7 +41,8 @@ pub fn value_to_bson_value(v: &Value) -> Result<Bson, ShellError> {
) )
})?) })?)
} }
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")?) Bson::I64(i.tagged(&v.tag).coerce_into("converting to BSON")?)
} }
UntaggedValue::Primitive(Primitive::Nothing) => Bson::Null, UntaggedValue::Primitive(Primitive::Nothing) => Bson::Null,
@ -50,9 +51,7 @@ pub fn value_to_bson_value(v: &Value) -> Result<Bson, ShellError> {
path.iter() path.iter()
.map(|x| match &x.unspanned { .map(|x| match &x.unspanned {
UnspannedPathMember::String(string) => Ok(Bson::String(string.clone())), UnspannedPathMember::String(string) => Ok(Bson::String(string.clone())),
UnspannedPathMember::Int(int) => Ok(Bson::I64( UnspannedPathMember::Int(int) => Ok(Bson::I64(*int)),
int.tagged(&v.tag).coerce_into("converting to BSON")?,
)),
}) })
.collect::<Result<Vec<Bson>, ShellError>>()?, .collect::<Result<Vec<Bson>, ShellError>>()?,
), ),
@ -183,7 +182,7 @@ fn get_binary_subtype(tagged_value: &Value) -> Result<BinarySubtype, ShellError>
"md5" => BinarySubtype::Md5, "md5" => BinarySubtype::Md5,
_ => unreachable!(), _ => unreachable!(),
}), }),
UntaggedValue::Primitive(Primitive::Int(i)) => Ok(BinarySubtype::UserDefined( UntaggedValue::Primitive(Primitive::BigInt(i)) => Ok(BinarySubtype::UserDefined(
i.tagged(&tagged_value.tag) i.tagged(&tagged_value.tag)
.coerce_into("converting to BSON binary subtype")?, .coerce_into("converting to BSON binary subtype")?,
)), )),

View File

@ -41,6 +41,7 @@ fn nu_value_to_sqlite_string(v: Value) -> String {
match &v.value { match &v.value {
UntaggedValue::Primitive(p) => match p { UntaggedValue::Primitive(p) => match p {
Primitive::Nothing => "NULL".into(), Primitive::Nothing => "NULL".into(),
Primitive::BigInt(i) => format!("{}", i),
Primitive::Int(i) => format!("{}", i), Primitive::Int(i) => format!("{}", i),
Primitive::Duration(i) => format!("{}", i), Primitive::Duration(i) => format!("{}", i),
Primitive::Decimal(f) => format!("{}", f), Primitive::Decimal(f) => format!("{}", f),