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
use crate::de::{Deserialize, Visitor};
use crate::error::Result;
use crate::ser::{Fragment, Serialize};
use crate::Place;
use core::fmt::{self, Display};
#[derive(Clone, Debug)]
pub enum Number {
U64(u64),
I64(i64),
F64(f64),
}
impl Display for Number {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self {
Number::U64(n) => formatter.write_str(itoa::Buffer::new().format(*n)),
Number::I64(n) => formatter.write_str(itoa::Buffer::new().format(*n)),
Number::F64(n) => formatter.write_str(ryu::Buffer::new().format(*n)),
}
}
}
impl Serialize for Number {
fn begin(&self) -> Fragment {
match self {
Number::U64(n) => Fragment::U64(*n),
Number::I64(n) => Fragment::I64(*n),
Number::F64(n) => Fragment::F64(*n),
}
}
}
impl Deserialize for Number {
fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
impl Visitor for Place<Number> {
fn negative(&mut self, n: i64) -> Result<()> {
self.out = Some(Number::I64(n));
Ok(())
}
fn nonnegative(&mut self, n: u64) -> Result<()> {
self.out = Some(Number::U64(n));
Ok(())
}
fn float(&mut self, n: f64) -> Result<()> {
self.out = Some(Number::F64(n));
Ok(())
}
}
Place::new(out)
}
}