1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// License: see LICENSE file at root directory of `master` branch

//! # Number

mod impls;
mod number_writer;
mod tests;

use {
    core::{
        convert::TryFrom,
        fmt::{self, Display, Formatter},
    },
    crate::Error,
};

#[cfg(feature="std")]
pub use self::number_writer::*;

/// # Number
///
/// You rarely use this directly, but [`Value`][::Value].
///
/// ## Notes
///
/// - [`PartialEq`][core::cmp/PartialEq] and [`PartialOrd`][core::cmp/PartialOrd] are **not** implemented. Since different number types cannot
///   be compared directly.
/// - [`Hash`][core::hash/Hash] is not implemented. Because [`f32`][f32] and [`f64`][f64] don't implement it.
///
/// [::Value]: enum.Value.html
///
/// [f32]: https://doc.rust-lang.org/std/primitive.f32.html
/// [f64]: https://doc.rust-lang.org/std/primitive.f64.html
/// [core::cmp/PartialEq]: https://doc.rust-lang.org/core/cmp/trait.PartialEq.html
/// [core::cmp/PartialOrd]: https://doc.rust-lang.org/core/cmp/trait.PartialOrd.html
/// [core::hash/Hash]: https://doc.rust-lang.org/core/hash/trait.Hash.html
#[derive(Debug, Clone)]
pub struct Number {
    inner: Inner,
}

/// # Inner value of `Number`
///
/// This helps prevent misuses, because JSON numbers have no type.
///
/// If we expose this enum, the user may incorrectly use `match` or `if let` on a **parsed** `Value::Number(...)`. For example, they may expect
/// some ID field to be a `u64`, while the parser could have parsed it as a `u8`.
#[derive(Debug, Clone)]
enum Inner {
    I8(i8), I16(i16), I32(i32), I64(i64), I128(i128), ISize(isize),
    U8(u8), U16(u16), U32(u32), U64(u64), U128(u128), USize(usize),
    F32(f32), F64(f64),
}

impl Display for Number {

    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        match self.inner {
            Inner::I8(i) => i.fmt(f),
            Inner::U8(u) => u.fmt(f),

            Inner::I16(i) => i.fmt(f),
            Inner::U16(u) => u.fmt(f),

            Inner::I32(i) => i.fmt(f),
            Inner::U32(u) => u.fmt(f),

            Inner::I64(i) => i.fmt(f),
            Inner::U64(u) => u.fmt(f),

            Inner::I128(i) => i.fmt(f),
            Inner::U128(u) => u.fmt(f),

            Inner::ISize(i) => i.fmt(f),
            Inner::USize(u) => u.fmt(f),

            Inner::F32(x) => x.fmt(f),
            Inner::F64(x) => x.fmt(f),
        }
    }

}

macro_rules! impl_from_primitives_for_number { ($($ty: ty, $code: tt,)+) => {
    $(
        impl From<$ty> for Number {

            fn from(n: $ty) -> Self {
                Self {
                    inner: Inner::$code(n),
                }
            }

        }
    )+
}}

impl_from_primitives_for_number! {
    i8, I8, i16, I16, i32, I32, i64, I64, i128, I128, isize, ISize,
    u8, U8, u16, U16, u32, U32, u64, U64, u128, U128, usize, USize,
    f32, F32, f64, F64,
}

macro_rules! impl_try_from_number_for_integers { ($($ty: ty,)+) => {
    $(
        impl TryFrom<&Number> for $ty {

            type Error = Error;

            fn try_from(n: &Number) -> Result<Self, Self::Error> {
                match n.inner {
                    Inner::I8(i) => Self::try_from(i).map_err(|e| Error::from(__!("{}", e))),
                    Inner::I16(i) => Self::try_from(i).map_err(|e| Error::from(__!("{}", e))),
                    Inner::I32(i) => Self::try_from(i).map_err(|e| Error::from(__!("{}", e))),
                    Inner::I64(i) => Self::try_from(i).map_err(|e| Error::from(__!("{}", e))),
                    Inner::I128(i) => Self::try_from(i).map_err(|e| Error::from(__!("{}", e))),
                    Inner::ISize(i) => Self::try_from(i).map_err(|e| Error::from(__!("{}", e))),

                    Inner::U8(u) => Self::try_from(u).map_err(|e| Error::from(__!("{}", e))),
                    Inner::U16(u) => Self::try_from(u).map_err(|e| Error::from(__!("{}", e))),
                    Inner::U32(u) => Self::try_from(u).map_err(|e| Error::from(__!("{}", e))),
                    Inner::U64(u) => Self::try_from(u).map_err(|e| Error::from(__!("{}", e))),
                    Inner::U128(u) => Self::try_from(u).map_err(|e| Error::from(__!("{}", e))),
                    Inner::USize(u) => Self::try_from(u).map_err(|e| Error::from(__!("{}", e))),

                    _ => Err(Error::from(__!("Floating numbers are not supported"))),
                }
            }

        }

        impl TryFrom<Number> for $ty {

            type Error = Error;

            fn try_from(n: Number) -> Result<Self, Self::Error> {
                Self::try_from(&n)
            }

        }
    )+
}}

impl_try_from_number_for_integers! {
    i8, i16, i32, i64, i128, isize,
    u8, u16, u32, u64, u128, usize,
}

impl TryFrom<&Number> for f32 {

    type Error = Error;

    fn try_from(n: &Number) -> Result<Self, Self::Error> {
        match n.inner {
            Inner::I8(i) => Ok(Self::from(i)),
            Inner::I16(i) => Ok(Self::from(i)),

            Inner::U8(u) => Ok(Self::from(u)),
            Inner::U16(u) => Ok(Self::from(u)),

            Inner::F32(f) => Ok(f),

            _ => Err(Error::from(__!("Not supported"))),
        }
    }

}

impl TryFrom<Number> for f32 {

    type Error = Error;

    fn try_from(n: Number) -> Result<Self, Self::Error> {
        Self::try_from(&n)
    }

}

impl TryFrom<&Number> for f64 {

    type Error = Error;

    fn try_from(n: &Number) -> Result<Self, Self::Error> {
        match n.inner {
            Inner::I8(i) => Ok(Self::from(i)),
            Inner::I16(i) => Ok(Self::from(i)),
            Inner::I32(i) => Ok(Self::from(i)),

            Inner::U8(u) => Ok(Self::from(u)),
            Inner::U16(u) => Ok(Self::from(u)),
            Inner::U32(u) => Ok(Self::from(u)),

            Inner::F32(f) => Ok(Self::from(f)),
            Inner::F64(f) => Ok(f),

            _ => Err(Error::from(__!("Not supported"))),
        }
    }

}

impl TryFrom<Number> for f64 {

    type Error = Error;

    fn try_from(n: Number) -> Result<Self, Self::Error> {
        Self::try_from(&n)
    }

}