use sonic_number::ParserNumber;
use crate::{error::make_error, util::private::Sealed};
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Number {
pub(crate) n: N,
}
#[derive(Debug, Copy, Clone)]
pub(crate) enum N {
PosInt(u64),
NegInt(i64),
Float(f64),
}
use core::{
fmt::{self, Debug, Display},
hash::{Hash, Hasher},
};
use serde::{
de::{self, Visitor},
forward_to_deserialize_any, Deserialize, Deserializer, Serialize, Serializer,
};
use crate::error::Error;
impl PartialEq for N {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(N::PosInt(a), N::PosInt(b)) => a == b,
(N::NegInt(a), N::NegInt(b)) => a == b,
(N::Float(a), N::Float(b)) => a == b,
_ => false,
}
}
}
impl Eq for N {}
impl Hash for N {
fn hash<H: Hasher>(&self, h: &mut H) {
match *self {
N::PosInt(i) => i.hash(h),
N::NegInt(i) => i.hash(h),
N::Float(f) => {
if f == 0.0f64 {
0.0f64.to_bits().hash(h);
} else {
f.to_bits().hash(h);
}
}
}
}
}
pub trait JsonNumberTrait: Sealed {
fn is_i64(&self) -> bool;
fn is_u64(&self) -> bool;
fn is_f64(&self) -> bool;
fn as_i64(&self) -> Option<i64>;
fn as_u64(&self) -> Option<u64>;
fn as_f64(&self) -> Option<f64>;
}
impl Sealed for Number {}
impl JsonNumberTrait for Number {
#[inline]
fn is_i64(&self) -> bool {
match self.n {
N::PosInt(v) => v <= i64::MAX as u64,
N::NegInt(_) => true,
N::Float(_) => false,
}
}
#[inline]
fn is_u64(&self) -> bool {
match self.n {
N::PosInt(_) => true,
N::NegInt(_) | N::Float(_) => false,
}
}
#[inline]
fn is_f64(&self) -> bool {
match self.n {
N::Float(_) => true,
N::PosInt(_) | N::NegInt(_) => false,
}
}
#[inline]
fn as_i64(&self) -> Option<i64> {
match self.n {
N::PosInt(n) => {
if n <= i64::MAX as u64 {
Some(n as i64)
} else {
None
}
}
N::NegInt(n) => Some(n),
N::Float(_) => None,
}
}
#[inline]
fn as_u64(&self) -> Option<u64> {
match self.n {
N::PosInt(n) => Some(n),
N::NegInt(_) | N::Float(_) => None,
}
}
#[inline]
fn as_f64(&self) -> Option<f64> {
match self.n {
N::PosInt(n) => Some(n as f64),
N::NegInt(n) => Some(n as f64),
N::Float(n) => Some(n),
}
}
}
impl Number {
#[inline]
pub fn from_f64(f: f64) -> Option<Number> {
if f.is_finite() {
let n = { N::Float(f) };
Some(Number { n })
} else {
None
}
}
}
impl Display for Number {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self.n {
N::PosInt(u) => formatter.write_str(itoa::Buffer::new().format(u)),
N::NegInt(i) => formatter.write_str(itoa::Buffer::new().format(i)),
N::Float(f) => formatter.write_str(ryu::Buffer::new().format_finite(f)),
}
}
}
impl Debug for Number {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "Number({})", self)
}
}
impl Serialize for Number {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self.n {
N::PosInt(u) => serializer.serialize_u64(u),
N::NegInt(i) => serializer.serialize_i64(i),
N::Float(f) => serializer.serialize_f64(f),
}
}
}
impl<'de> Deserialize<'de> for Number {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Number, D::Error>
where
D: Deserializer<'de>,
{
struct NumberVisitor;
impl<'de> Visitor<'de> for NumberVisitor {
type Value = Number;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a JSON number")
}
#[inline]
fn visit_i64<E>(self, value: i64) -> Result<Number, E> {
Ok(value.into())
}
#[inline]
fn visit_u64<E>(self, value: u64) -> Result<Number, E> {
Ok(value.into())
}
#[inline]
fn visit_f64<E>(self, value: f64) -> Result<Number, E>
where
E: de::Error,
{
Number::from_f64(value).ok_or_else(|| de::Error::custom("not a JSON number"))
}
}
deserializer.deserialize_any(NumberVisitor)
}
}
macro_rules! deserialize_any {
(@expand [$($num_string:tt)*]) => {
#[inline]
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.n {
N::PosInt(u) => visitor.visit_u64(u),
N::NegInt(i) => visitor.visit_i64(i),
N::Float(f) => visitor.visit_f64(f),
}
}
};
(owned) => {
deserialize_any!(@expand [n]);
};
(ref) => {
deserialize_any!(@expand [n.clone()]);
};
}
macro_rules! deserialize_number {
($deserialize:ident) => {
fn $deserialize<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
self.deserialize_any(visitor)
}
};
}
impl<'de> Deserializer<'de> for Number {
type Error = Error;
deserialize_any!(owned);
deserialize_number!(deserialize_i8);
deserialize_number!(deserialize_i16);
deserialize_number!(deserialize_i32);
deserialize_number!(deserialize_i64);
deserialize_number!(deserialize_i128);
deserialize_number!(deserialize_u8);
deserialize_number!(deserialize_u16);
deserialize_number!(deserialize_u32);
deserialize_number!(deserialize_u64);
deserialize_number!(deserialize_u128);
deserialize_number!(deserialize_f32);
deserialize_number!(deserialize_f64);
forward_to_deserialize_any! {
bool char str string bytes byte_buf option unit unit_struct
newtype_struct seq tuple tuple_struct map struct enum identifier
ignored_any
}
}
impl<'de, 'a> Deserializer<'de> for &'a Number {
type Error = Error;
deserialize_any!(ref);
deserialize_number!(deserialize_i8);
deserialize_number!(deserialize_i16);
deserialize_number!(deserialize_i32);
deserialize_number!(deserialize_i64);
deserialize_number!(deserialize_i128);
deserialize_number!(deserialize_u8);
deserialize_number!(deserialize_u16);
deserialize_number!(deserialize_u32);
deserialize_number!(deserialize_u64);
deserialize_number!(deserialize_u128);
deserialize_number!(deserialize_f32);
deserialize_number!(deserialize_f64);
forward_to_deserialize_any! {
bool char str string bytes byte_buf option unit unit_struct
newtype_struct seq tuple tuple_struct map struct enum identifier
ignored_any
}
}
impl From<ParserNumber> for Number {
fn from(value: ParserNumber) -> Self {
let n = match value {
ParserNumber::Float(f) => N::Float(f),
ParserNumber::Unsigned(u) => N::PosInt(u),
ParserNumber::Signed(i) => N::NegInt(i),
};
Number { n }
}
}
macro_rules! impl_from_unsigned {
(
$($ty:ty),*
) => {
$(
impl From<$ty> for Number {
#[inline]
fn from(u: $ty) -> Self {
let n = {
{ N::PosInt(u as u64) }
};
Number { n }
}
}
)*
};
}
macro_rules! impl_from_signed {
(
$($ty:ty),*
) => {
$(
impl From<$ty> for Number {
#[inline]
fn from(i: $ty) -> Self {
let n = {
if i < 0 {
N::NegInt(i as i64)
} else {
N::PosInt(i as u64)
}
};
Number { n }
}
}
)*
};
}
impl_from_unsigned!(u8, u16, u32, u64, usize);
impl_from_signed!(i8, i16, i32, i64, isize);
impl TryFrom<f32> for Number {
type Error = crate::Error;
#[inline]
fn try_from(f: f32) -> Result<Self, Self::Error> {
Number::try_from(f as f64)
}
}
impl TryFrom<f64> for Number {
type Error = crate::Error;
fn try_from(value: f64) -> Result<Self, Self::Error> {
Number::from_f64(value)
.ok_or_else(|| make_error("NaN or Infinity is not a valid JSON value".to_string()))
}
}