mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 01:05:01 +02:00
Parse decimals as BigDecimal (#2644)
Use implicit serde from BigDecimal crate
This commit is contained in:
@ -1099,18 +1099,8 @@ impl Expression {
|
||||
Expression::Literal(Literal::Number(Number::Int(i)))
|
||||
}
|
||||
|
||||
pub fn decimal(f: f64) -> Result<Expression, ParseError> {
|
||||
let dec = BigDecimal::from_f64(f);
|
||||
|
||||
let dec = match dec {
|
||||
Some(x) => Ok(x),
|
||||
None => Err(ParseError::internal_error(
|
||||
"Can not convert f64 to big decimal"
|
||||
.to_string()
|
||||
.spanned_unknown(),
|
||||
)),
|
||||
}?;
|
||||
Ok(Expression::Literal(Literal::Number(Number::Decimal(dec))))
|
||||
pub fn decimal(dec: BigDecimal) -> Expression {
|
||||
Expression::Literal(Literal::Number(Number::Decimal(dec)))
|
||||
}
|
||||
|
||||
pub fn string(s: String) -> Expression {
|
||||
|
@ -7,7 +7,6 @@ pub mod evaluate;
|
||||
pub mod iter;
|
||||
pub mod primitive;
|
||||
pub mod range;
|
||||
mod serde_bigdecimal;
|
||||
|
||||
use crate::hir;
|
||||
use crate::type_name::{ShellTypeName, SpannedTypeName};
|
||||
|
@ -1,7 +1,6 @@
|
||||
use crate::type_name::ShellTypeName;
|
||||
use crate::value::column_path::ColumnPath;
|
||||
use crate::value::range::{Range, RangeInclusion};
|
||||
use crate::value::serde_bigdecimal;
|
||||
use bigdecimal::BigDecimal;
|
||||
use chrono::{DateTime, Utc};
|
||||
use nu_errors::{ExpectedRange, ShellError};
|
||||
@ -27,7 +26,6 @@ pub enum Primitive {
|
||||
/// A "big int", an integer with arbitrarily large size (aka not limited to 64-bit)
|
||||
Int(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(u64),
|
||||
|
@ -1,26 +0,0 @@
|
||||
use bigdecimal::BigDecimal;
|
||||
use num_traits::cast::FromPrimitive;
|
||||
use num_traits::cast::ToPrimitive;
|
||||
|
||||
/// Enable big decimal serialization by providing a `serialize` function
|
||||
pub fn serialize<S>(big_decimal: &BigDecimal, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
serde::Serialize::serialize(
|
||||
&big_decimal
|
||||
.to_f64()
|
||||
.ok_or_else(|| serde::ser::Error::custom("expected a f64-sized bignum"))?,
|
||||
serializer,
|
||||
)
|
||||
}
|
||||
|
||||
/// Enable big decimal deserialization by providing a `deserialize` function
|
||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<BigDecimal, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let x: f64 = serde::Deserialize::deserialize(deserializer)?;
|
||||
Ok(BigDecimal::from_f64(x)
|
||||
.ok_or_else(|| serde::de::Error::custom("expected a f64-sized bigdecimal"))?)
|
||||
}
|
Reference in New Issue
Block a user