mirror of
https://github.com/nushell/nushell.git
synced 2025-08-17 22:59:52 +02:00
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:
@@ -4,7 +4,6 @@ use nu_source::{
|
||||
span_for_spanned_list, DbgDocBldr, DebugDocBuilder, HasFallibleSpan, PrettyDebug, Span,
|
||||
Spanned, SpannedItem,
|
||||
};
|
||||
use num_bigint::BigInt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::hir::{Expression, Literal, Member, SpannedExpression};
|
||||
@@ -14,7 +13,7 @@ use nu_errors::ParseError;
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
|
||||
pub enum UnspannedPathMember {
|
||||
String(String),
|
||||
Int(BigInt),
|
||||
Int(i64),
|
||||
}
|
||||
|
||||
impl UnspannedPathMember {
|
||||
@@ -125,8 +124,8 @@ impl PathMember {
|
||||
}
|
||||
|
||||
/// Create a numeric path member
|
||||
pub fn int(int: impl Into<BigInt>, span: impl Into<Span>) -> PathMember {
|
||||
UnspannedPathMember::Int(int.into()).into_path_member(span)
|
||||
pub fn int(int: i64, span: impl Into<Span>) -> PathMember {
|
||||
UnspannedPathMember::Int(int).into_path_member(span)
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
if let Ok(row_number) = current_part.parse::<u64>() {
|
||||
output.push(Member::Int(BigInt::from(row_number), part_span));
|
||||
if let Ok(row_number) = current_part.parse::<i64>() {
|
||||
output.push(Member::Int(row_number, part_span));
|
||||
} else {
|
||||
let trimmed = trim_quotes(¤t_part);
|
||||
output.push(Member::Bare(trimmed.clone().spanned(part_span)));
|
||||
@@ -180,8 +179,8 @@ fn parse(raw_column_path: &Spanned<String>) -> (SpannedExpression, Option<ParseE
|
||||
raw_column_path.span.start() + start_index,
|
||||
raw_column_path.span.start() + last_index + 1,
|
||||
);
|
||||
if let Ok(row_number) = current_part.parse::<u64>() {
|
||||
output.push(Member::Int(BigInt::from(row_number), part_span));
|
||||
if let Ok(row_number) = current_part.parse::<i64>() {
|
||||
output.push(Member::Int(row_number, part_span));
|
||||
} else {
|
||||
let current_part = trim_quotes(¤t_part);
|
||||
output.push(Member::Bare(current_part.spanned(part_span)));
|
||||
|
@@ -11,7 +11,8 @@ impl std::convert::TryFrom<&Value> for i64 {
|
||||
/// Convert to an i64 integer, if possible
|
||||
fn try_from(value: &Value) -> Result<i64, ShellError> {
|
||||
match &value.value {
|
||||
UntaggedValue::Primitive(Primitive::Int(int)) => {
|
||||
UntaggedValue::Primitive(Primitive::Int(int)) => Ok(*int),
|
||||
UntaggedValue::Primitive(Primitive::BigInt(int)) => {
|
||||
int.tagged(&value.tag).coerce_into("converting to i64")
|
||||
}
|
||||
_ => Err(ShellError::type_error("Integer", value.spanned_type_name())),
|
||||
|
@@ -36,6 +36,7 @@ impl PrettyType for Primitive {
|
||||
match self {
|
||||
Primitive::Nothing => ty("nothing"),
|
||||
Primitive::Int(_) => ty("integer"),
|
||||
Primitive::BigInt(_) => ty("big-integer"),
|
||||
Primitive::Range(_) => ty("range"),
|
||||
Primitive::Decimal(_) => ty("decimal"),
|
||||
Primitive::Filesize(_) => ty("filesize"),
|
||||
@@ -59,6 +60,7 @@ impl PrettyDebug for Primitive {
|
||||
match self {
|
||||
Primitive::Nothing => DbgDocBldr::primitive("nothing"),
|
||||
Primitive::Int(int) => prim(format_args!("{}", int)),
|
||||
Primitive::BigInt(int) => prim(format_args!("{}", int)),
|
||||
Primitive::Decimal(decimal) => prim(format_args!("{}", decimal)),
|
||||
Primitive::Range(range) => {
|
||||
let (left, left_inclusion) = &range.from;
|
||||
|
@@ -24,14 +24,16 @@ const NANOS_PER_SEC: u32 = 1_000_000_000;
|
||||
pub enum Primitive {
|
||||
/// An empty value
|
||||
Nothing,
|
||||
/// A common integer
|
||||
Int(i64),
|
||||
/// A "big int", an integer with arbitrarily large size (aka not limited to 64-bit)
|
||||
#[serde(with = "serde_bigint")]
|
||||
Int(BigInt),
|
||||
BigInt(BigInt),
|
||||
/// A "big decimal", an decimal number with arbitrarily large size (aka not limited to 64-bit)
|
||||
#[serde(with = "serde_bigdecimal")]
|
||||
Decimal(BigDecimal),
|
||||
/// A count in the number of bytes, used as a filesize
|
||||
Filesize(BigInt),
|
||||
Filesize(u64),
|
||||
/// A string value
|
||||
String(String),
|
||||
/// A path to travel to reach a value in a table
|
||||
@@ -351,7 +353,7 @@ impl From<BigDecimal> for Primitive {
|
||||
impl From<BigInt> for Primitive {
|
||||
/// Helper to convert from integers to a Primitive value
|
||||
fn from(int: BigInt) -> Primitive {
|
||||
Primitive::Int(int)
|
||||
Primitive::BigInt(int)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -373,14 +375,14 @@ macro_rules! from_native_to_primitive {
|
||||
};
|
||||
}
|
||||
|
||||
from_native_to_primitive!(i8, Primitive::Int, BigInt::from_i8);
|
||||
from_native_to_primitive!(i16, Primitive::Int, BigInt::from_i16);
|
||||
from_native_to_primitive!(i32, Primitive::Int, BigInt::from_i32);
|
||||
from_native_to_primitive!(i64, Primitive::Int, BigInt::from_i64);
|
||||
from_native_to_primitive!(u8, Primitive::Int, BigInt::from_u8);
|
||||
from_native_to_primitive!(u16, Primitive::Int, BigInt::from_u16);
|
||||
from_native_to_primitive!(u32, Primitive::Int, BigInt::from_u32);
|
||||
from_native_to_primitive!(u64, Primitive::Int, BigInt::from_u64);
|
||||
from_native_to_primitive!(i8, Primitive::Int, i64::from_i8);
|
||||
from_native_to_primitive!(i16, Primitive::Int, i64::from_i16);
|
||||
from_native_to_primitive!(i32, Primitive::Int, i64::from_i32);
|
||||
from_native_to_primitive!(i64, Primitive::Int, i64::from_i64);
|
||||
from_native_to_primitive!(u8, Primitive::Int, i64::from_u8);
|
||||
from_native_to_primitive!(u16, Primitive::Int, i64::from_u16);
|
||||
from_native_to_primitive!(u32, Primitive::Int, i64::from_u32);
|
||||
from_native_to_primitive!(u64, Primitive::BigInt, BigInt::from_u64);
|
||||
from_native_to_primitive!(f32, Primitive::Decimal, BigDecimal::from_f32);
|
||||
from_native_to_primitive!(f64, Primitive::Decimal, BigDecimal::from_f64);
|
||||
|
||||
@@ -410,6 +412,7 @@ impl ShellTypeName for Primitive {
|
||||
match self {
|
||||
Primitive::Nothing => "nothing",
|
||||
Primitive::Int(_) => "integer",
|
||||
Primitive::BigInt(_) => "big integer",
|
||||
Primitive::Range(_) => "range",
|
||||
Primitive::Decimal(_) => "decimal",
|
||||
Primitive::Filesize(_) => "filesize(in bytes)",
|
||||
@@ -454,6 +457,7 @@ pub fn format_primitive(primitive: &Primitive, field_name: Option<&String>) -> S
|
||||
}
|
||||
Primitive::Duration(duration) => format_duration(duration),
|
||||
Primitive::Int(i) => i.to_string(),
|
||||
Primitive::BigInt(i) => i.to_string(),
|
||||
Primitive::Decimal(decimal) => {
|
||||
// TODO: We should really pass the precision in here instead of hard coding it
|
||||
let decimal_string = decimal.to_string();
|
||||
|
@@ -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> {
|
||||
let (from, range_incl) = &self.from;
|
||||
|
||||
|
Reference in New Issue
Block a user