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
use crate::{Error, Map, Result};
mod de;
mod ser;
pub use de::from_object;
pub use ser::{to_object, Serializer};
#[derive(Debug, PartialEq, Clone)]
pub enum Object {
Null,
Boolean(bool),
Integer(i64),
Float(f64),
String(std::string::String),
Raw(Vec<u8>),
List(Vec<Object>),
Map(Map),
}
impl Object {
pub fn as_null(&self) -> Result<()> {
match self {
Object::Null => Ok(()),
_ => Err(Error::WrongType),
}
}
pub fn as_boolean(&self) -> Result<bool> {
match self {
Object::Boolean(b) => Ok(*b),
_ => Err(Error::WrongType),
}
}
pub fn as_integer(&self) -> Result<i64> {
match self {
Object::Integer(i) => Ok(*i),
_ => Err(Error::WrongType),
}
}
pub fn as_float(&self) -> Result<f64> {
match self {
Object::Float(f) => Ok(*f),
_ => Err(Error::WrongType),
}
}
pub fn as_string(&self) -> Result<&str> {
match self {
Object::String(s) => Ok(s),
_ => Err(Error::WrongType),
}
}
pub fn as_raw(&self) -> Result<&[u8]> {
match self {
Object::Raw(r) => Ok(r),
_ => Err(Error::WrongType),
}
}
pub fn as_list(&self) -> Result<&[Object]> {
match self {
Object::List(l) => Ok(l),
_ => Err(Error::WrongType),
}
}
pub fn as_map(&self) -> Result<&Map> {
match self {
Object::Map(d) => Ok(d),
_ => Err(Error::WrongType),
}
}
pub fn to_null(self) -> Result<()> {
self.as_null()
}
pub fn to_boolean(self) -> Result<bool> {
self.as_boolean()
}
pub fn to_integer(self) -> Result<i64> {
self.as_integer()
}
pub fn to_float(self) -> Result<f64> {
self.as_float()
}
pub fn to_string(self) -> Result<String> {
match self {
Object::String(s) => Ok(s),
_ => Err(Error::WrongType),
}
}
pub fn to_raw(self) -> Result<Vec<u8>> {
match self {
Object::Raw(r) => Ok(r),
_ => Err(Error::WrongType),
}
}
pub fn to_list(self) -> Result<Vec<Object>> {
match self {
Object::List(l) => Ok(l),
_ => Err(Error::WrongType),
}
}
pub fn to_map(self) -> Result<Map> {
match self {
Object::Map(d) => Ok(d),
_ => Err(Error::WrongType),
}
}
}