Always return numbers instead of bigint
This commit is contained in:
parent
b7b22a63cd
commit
2bb647cdac
5 changed files with 93 additions and 76 deletions
|
|
@ -5,6 +5,10 @@ use serde::{Deserialize, Serialize};
|
|||
#[cfg(feature = "wasm")]
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[cfg(feature = "wasm")]
|
||||
#[allow(clippy::cast_precision_loss)]
|
||||
const INTEGRAL_LIMIT: f64 = (1u64 << 53) as f64;
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "serde", serde(untagged))]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
|
|
@ -22,6 +26,17 @@ impl TryFrom<JsValue> for NumberOrString {
|
|||
return Ok(NumberOrString::Number(num));
|
||||
}
|
||||
|
||||
if let Some(num) = value.clone().as_f64() {
|
||||
if num.abs() > INTEGRAL_LIMIT {
|
||||
return Err(DeserialisationError::new(
|
||||
"Floating-point number exceeds safe integer limit, use BigInt instead",
|
||||
));
|
||||
}
|
||||
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
return Ok(NumberOrString::Number(num.round() as i64));
|
||||
}
|
||||
|
||||
if let Ok(text) = value.try_into() {
|
||||
return Ok(NumberOrString::Text(text));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue