use {
core::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8},
crate::{Error, Json, Number},
};
#[cfg(any(target_pointer_width = "16", target_pointer_width = "32", target_pointer_width = "64"))]
use core::num::{NonZeroIsize, NonZeroUsize};
impl From<&Number> for Json {
fn from(n: &Number) -> Self {
Json::Number(*n)
}
}
impl From<Number> for Json {
fn from(n: Number) -> Self {
Json::Number(n)
}
}
impl From<bool> for Json {
fn from(b: bool) -> Self {
Json::Boolean(b)
}
}
impl PartialEq<bool> for Json {
fn eq(&self, other: &bool) -> bool {
match self {
Self::Boolean(b) => b == other,
_ => false,
}
}
}
impl PartialEq<Json> for bool {
fn eq(&self, other: &Json) -> bool {
match other {
Json::Boolean(b) => self == b,
_ => false,
}
}
}
impl TryFrom<&Json> for bool {
type Error = Error;
fn try_from(value: &Json) -> Result<Self, Self::Error> {
match value {
Json::Boolean(b) => Ok(*b),
_ => Err(e!("Json is not a Boolean")),
}
}
}
impl TryFrom<Json> for bool {
type Error = Error;
fn try_from(value: Json) -> Result<Self, Self::Error> {
Self::try_from(&value)
}
}
macro_rules! impl_from_primitives_for_json { ($($ty: ty),+$(,)?) => {
$(
impl From<$ty> for Json {
fn from(n: $ty) -> Self {
Self::Number(Number::from(n))
}
}
impl From<&$ty> for Json {
fn from(n: &$ty) -> Self {
Self::from(*n)
}
}
)+
}}
impl_from_primitives_for_json! {
i8, i16, i32, i64, i128,
u8, u16, u32, u64, u128,
f32, f64,
NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128,
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128,
}
#[cfg(any(target_pointer_width = "16", target_pointer_width = "32", target_pointer_width = "64"))]
impl_from_primitives_for_json!(usize, isize, NonZeroUsize, NonZeroIsize);
macro_rules! impl_try_from_json_for_primitives { ($($ty: ty),+$(,)?) => {
$(
impl TryFrom<&Json> for $ty {
type Error = Error;
fn try_from(value: &Json) -> Result<Self, Self::Error> {
match value {
Json::Number(n) => Self::try_from(n),
_ => Err(e!("Json is not a Number")),
}
}
}
impl TryFrom<Json> for $ty {
type Error = Error;
fn try_from(value: Json) -> Result<Self, Self::Error> {
Self::try_from(&value)
}
}
)+
}}
impl_try_from_json_for_primitives! {
i8, i16, i32, i64, i128,
u8, u16, u32, u64, u128,
f32, f64,
NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128,
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128,
}
#[cfg(any(target_pointer_width = "16", target_pointer_width = "32", target_pointer_width = "64"))]
impl_try_from_json_for_primitives! {isize, usize, NonZeroIsize, NonZeroUsize}
macro_rules! impl_partial_eq_for_json_and_primitive_numbers { ($($ty: ty),+$(,)?) => {
$(
impl PartialEq<$ty> for Json {
fn eq(&self, other: &$ty) -> bool {
match &self {
Self::Number(n) => n == other,
_ => false,
}
}
}
impl PartialEq<Json> for $ty {
fn eq(&self, other: &Json) -> bool {
match &other {
Json::Number(n) => self == n,
_ => false,
}
}
}
)+
}}
impl_partial_eq_for_json_and_primitive_numbers!(
u8, u16, u32, u64, u128,
i8, i16, i32, i64, i128,
NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128,
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128,
f32, f64,
);
#[cfg(any(target_pointer_width = "16", target_pointer_width = "32", target_pointer_width = "64"))]
impl_partial_eq_for_json_and_primitive_numbers!(isize, usize, NonZeroUsize, NonZeroIsize);