use core::hash::{Hash, Hasher};
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Number {
pub(crate) n: N,
}
impl From<N> for Number {
fn from(n: N) -> Self {
Self { n }
}
}
#[derive(Copy, Clone)]
pub(crate) enum N {
PosInt(u64),
NegInt(i64),
Float(f64),
}
impl Number {
pub fn as_u64(&self) -> Option<u64> {
match self.n {
N::PosInt(v) => Some(v),
_ => None,
}
}
pub 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(v) => Some(v),
_ => None,
}
}
pub 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),
}
}
pub fn is_f64(&self) -> bool {
matches!(self.n, N::Float(_))
}
pub fn is_u64(&self) -> bool {
matches!(self.n, N::PosInt(_))
}
pub fn is_i64(&self) -> bool {
match self.n {
N::PosInt(v) => v <= i64::MAX as u64,
N::NegInt(_) => true,
N::Float(_) => false,
}
}
}
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);
}
}
}
}
}
impl std::fmt::Display for Number {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.n {
N::PosInt(n) => write!(f, "{}", n),
N::NegInt(n) => write!(f, "{}", n),
N::Float(n) => write!(f, "{}", n),
}
}
}
impl From<u64> for Number {
fn from(val: u64) -> Self {
Self { n: N::PosInt(val) }
}
}
impl From<i64> for Number {
fn from(val: i64) -> Self {
Self { n: N::NegInt(val) }
}
}
impl From<f64> for Number {
fn from(val: f64) -> Self {
Self { n: N::Float(val) }
}
}
impl From<Number> for serde_json::value::Number {
fn from(num: Number) -> Self {
match num.n {
N::PosInt(n) => n.into(),
N::NegInt(n) => n.into(),
N::Float(n) => serde_json::value::Number::from_f64(n).unwrap(),
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_number_display() {
assert_eq!(Number::from(42u64).to_string(), "42");
assert_eq!(Number::from(-42i64).to_string(), "-42");
assert_eq!(Number::from(3.66).to_string(), "3.66");
}
}