reqrio_json/
number.rs

1use crate::JsonResult;
2
3#[derive(Clone)]
4pub enum Number {
5    U64(u64),
6    I64(i64),
7    F64(f64),
8}
9
10impl Number {
11    pub fn as_i64(&self) -> JsonResult<i64> {
12        match self {
13            Number::U64(u) => { Ok(u.clone() as i64) }
14            Number::I64(i) => { Ok(i.clone()) }
15            Number::F64(f) => { Ok(f.clone() as i64) }
16        }
17    }
18
19    pub fn as_f32(&self) -> JsonResult<f32> {
20        match self {
21            Number::U64(u) => { Ok(u.clone() as f32) }
22            Number::I64(i) => { Ok(i.clone() as f32) }
23            Number::F64(f) => { Ok(f.clone() as f32) }
24        }
25    }
26
27    pub fn as_u64(&self) -> JsonResult<u64> {
28        match self {
29            Number::U64(u) => { Ok(u.clone()) }
30            Number::I64(i) => { Ok(i.clone() as u64) }
31            Number::F64(f) => { Ok(f.clone() as u64) }
32        }
33    }
34
35    pub fn as_u32(&self) -> JsonResult<u32> {
36        match self {
37            Number::U64(u) => { Ok(u.clone() as u32) }
38            Number::I64(i) => { Ok(i.clone() as u32) }
39            Number::F64(f) => { Ok(f.clone() as u32) }
40        }
41    }
42
43    pub fn as_u16(&self) -> JsonResult<u16> {
44        match self {
45            Number::U64(u) => { Ok(u.clone() as u16) }
46            Number::I64(i) => { Ok(i.clone() as u16) }
47            Number::F64(f) => { Ok(f.clone() as u16) }
48        }
49    }
50
51    pub fn as_u8(&self) -> JsonResult<u8> {
52        match self {
53            Number::U64(u) => { Ok(u.clone() as u8) }
54            Number::I64(i) => { Ok(i.clone() as u8) }
55            Number::F64(f) => { Ok(f.clone() as u8) }
56        }
57    }
58
59    pub fn as_usize(&self) -> JsonResult<usize> {
60        match self {
61            Number::U64(u) => { Ok(u.clone() as usize) }
62            Number::I64(i) => { Ok(i.clone() as usize) }
63            Number::F64(f) => { Ok(f.clone() as usize) }
64        }
65    }
66
67    pub fn as_i32(&self) -> JsonResult<i32> {
68        match self {
69            Number::U64(u) => { Ok(u.clone() as i32) }
70            Number::I64(i) => { Ok(i.clone() as i32) }
71            Number::F64(f) => { Ok(f.clone() as i32) }
72        }
73    }
74
75    pub fn as_i16(&self) -> JsonResult<i16> {
76        match self {
77            Number::U64(u) => { Ok(u.clone() as i16) }
78            Number::I64(i) => { Ok(i.clone() as i16) }
79            Number::F64(f) => { Ok(f.clone() as i16) }
80        }
81    }
82
83    pub fn as_i8(&self) -> JsonResult<i8> {
84        match self {
85            Number::U64(u) => { Ok(u.clone() as i8) }
86            Number::I64(i) => { Ok(i.clone() as i8) }
87            Number::F64(f) => { Ok(f.clone() as i8) }
88        }
89    }
90
91    pub fn as_isize(&self) -> JsonResult<isize> {
92        match self {
93            Number::U64(u) => { Ok(u.clone() as isize) }
94            Number::I64(i) => { Ok(i.clone() as isize) }
95            Number::F64(f) => { Ok(f.clone() as isize) }
96        }
97    }
98
99    pub fn as_f64(&self) -> JsonResult<f64> {
100        match self {
101            Number::U64(u) => { Ok(u.clone() as f64) }
102            Number::I64(i) => { Ok(i.clone() as f64) }
103            Number::F64(f) => { Ok(f.clone()) }
104        }
105    }
106}