mirror of
https://github.com/nushell/nushell
synced 2025-01-14 14:14:13 +00:00
Fix reading/writing bigint and bigdecimal (#2893)
This commit is contained in:
parent
5356cb9fbd
commit
99117ff2ef
3 changed files with 33 additions and 22 deletions
|
@ -1,18 +1,11 @@
|
||||||
use bigdecimal::BigDecimal;
|
use bigdecimal::BigDecimal;
|
||||||
use num_traits::cast::FromPrimitive;
|
|
||||||
use num_traits::cast::ToPrimitive;
|
|
||||||
|
|
||||||
/// Enable big decimal serialization by providing a `serialize` function
|
/// Enable big decimal serialization by providing a `serialize` function
|
||||||
pub fn serialize<S>(big_decimal: &BigDecimal, serializer: S) -> Result<S::Ok, S::Error>
|
pub fn serialize<S>(big_decimal: &BigDecimal, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
S: serde::Serializer,
|
S: serde::Serializer,
|
||||||
{
|
{
|
||||||
serde::Serialize::serialize(
|
serde::Serialize::serialize(&big_decimal.to_string(), serializer)
|
||||||
&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
|
/// Enable big decimal deserialization by providing a `deserialize` function
|
||||||
|
@ -20,7 +13,7 @@ pub fn deserialize<'de, D>(deserializer: D) -> Result<BigDecimal, D::Error>
|
||||||
where
|
where
|
||||||
D: serde::Deserializer<'de>,
|
D: serde::Deserializer<'de>,
|
||||||
{
|
{
|
||||||
let x: f64 = serde::Deserialize::deserialize(deserializer)?;
|
let x: String = serde::Deserialize::deserialize(deserializer)?;
|
||||||
Ok(BigDecimal::from_f64(x)
|
Ok(BigDecimal::parse_bytes(x.as_bytes(), 10)
|
||||||
.ok_or_else(|| serde::de::Error::custom("expected a f64-sized bigdecimal"))?)
|
.ok_or_else(|| serde::de::Error::custom("expected a bigdecimal"))?)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,11 @@
|
||||||
use num_bigint::BigInt;
|
use num_bigint::BigInt;
|
||||||
use num_traits::cast::FromPrimitive;
|
|
||||||
use num_traits::cast::ToPrimitive;
|
|
||||||
|
|
||||||
/// Enable big int serialization by providing a `serialize` function
|
/// Enable big int serialization by providing a `serialize` function
|
||||||
pub fn serialize<S>(big_int: &BigInt, serializer: S) -> Result<S::Ok, S::Error>
|
pub fn serialize<S>(big_int: &BigInt, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
S: serde::Serializer,
|
S: serde::Serializer,
|
||||||
{
|
{
|
||||||
serde::Serialize::serialize(
|
serde::Serialize::serialize(&big_int.to_string(), serializer)
|
||||||
&big_int
|
|
||||||
.to_i64()
|
|
||||||
.ok_or_else(|| serde::ser::Error::custom("expected a i64-sized bignum"))?,
|
|
||||||
serializer,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enable big int deserialization by providing a `deserialize` function
|
/// Enable big int deserialization by providing a `deserialize` function
|
||||||
|
@ -20,7 +13,8 @@ pub fn deserialize<'de, D>(deserializer: D) -> Result<BigInt, D::Error>
|
||||||
where
|
where
|
||||||
D: serde::Deserializer<'de>,
|
D: serde::Deserializer<'de>,
|
||||||
{
|
{
|
||||||
let x: i64 = serde::Deserialize::deserialize(deserializer)?;
|
let x: String = serde::Deserialize::deserialize(deserializer)?;
|
||||||
Ok(BigInt::from_i64(x)
|
|
||||||
.ok_or_else(|| serde::de::Error::custom("expected a i64-sized bignum"))?)
|
Ok(BigInt::parse_bytes(x.as_bytes(), 10)
|
||||||
|
.ok_or_else(|| serde::de::Error::custom("expected a bignum"))?)
|
||||||
}
|
}
|
||||||
|
|
|
@ -332,6 +332,30 @@ fn string_interpolation_with_it_column_path() {
|
||||||
assert_eq!(actual.out, "sammie");
|
assert_eq!(actual.out, "sammie");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bignum_large_integer() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".",
|
||||||
|
r#"
|
||||||
|
echo 91231720741731287123917
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "91231720741731287123917");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bignum_large_decimal() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".",
|
||||||
|
r#"
|
||||||
|
echo 91231720741731287123917.1
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "91231720741731287123917.1");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn run_custom_command() {
|
fn run_custom_command() {
|
||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
|
|
Loading…
Reference in a new issue