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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use crate::Serialize;
/// Different Enums to construct an abstract JSON Hierarchy which is easier to work with and to construct
#[derive(Debug, Clone, Default)]
pub enum Values {
/// Represents a JSON String
/// ```
/// use wjp::Values;
/// assert_eq!(
/// "\"Hello\"",
/// Values::String(String::from("Hello")).to_string()
/// )
/// ```
String(String),
/// Represents a JSON Number
/// ```
/// use wjp::Values;
/// assert_eq!(
/// "12.43",
/// Values::Number(12.43).to_string()
/// )
/// ```
Number(f64),
/// Represents a JSON Struct
/// ```
/// use wjp::{map, Values};
/// use wjp::Serialize;
/// assert_eq!(
/// Values::Struct(map!(("message",&Values::Null))).to_string(),
/// "{\"message\":null}"
/// )
/// ```
Struct(HashMap<String, Values>),
/// Represents a JSON Array
/// ```
/// use wjp::Values;
/// assert_eq!(
/// Values::Array(vec![Values::Null,Values::Boolean(true)]).to_string(),
/// "[null,true]"
/// )
/// ```
Array(Vec<Values>),
/// Represents the JSON Value "null"
/// ```
/// use wjp::Values;
/// assert_eq!(
/// Values::Null.to_string(),
/// "null"
/// );
/// ```
#[default]
Null,
/// Represents the JSON Value "true" or "false"
/// ```
/// use wjp::Values;
/// assert_eq!(
/// Values::Boolean(true).to_string(),
/// "true"
/// );
/// assert_eq!(
/// Values::Boolean(false).to_string(),
/// "false"
/// )
/// ```
Boolean(bool),
}
impl Serialize for Values {
fn serialize(&self) -> Values {
self.clone()
}
}
impl PartialEq<Self> for Values {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(&Values::Null, &Values::Null) => true,
(Values::String(a), Values::String(b)) => a == b,
(&Values::Number(a), &Values::String(ref b))
| (&Values::String(ref b), &Values::Number(a)) => a.to_string() == *b,
(Values::Number(a), Values::Number(b)) => a == b,
(Values::Boolean(a), Values::Boolean(b)) => a == b,
(Values::Struct(a), Values::Struct(b)) => a == b,
(Values::Array(a), Values::Array(b)) => a == b,
_ => false,
}
}
}
impl Values {
pub const STRING: &'static str = "string";
pub const STRUCT: &'static str = "struct";
pub const NUMBER: &'static str = "number";
pub const NULL: &'static str = "null";
pub const ARRAY: &'static str = "array";
pub const BOOLEAN: &'static str = "boolean";
/// if the provided value is a [`Struct`] it will return [`Some`]
/// containing the inner [`Hashmap`] otherwise returns [`None`]
///
/// [`Struct`]: Values::Struct
/// [`Hashmap`]: HashMap
pub fn get_struct(&self) -> Option<HashMap<String, Values>> {
match self {
Values::Struct(map) => Some(map.clone()),
_ => None,
}
}
/// if the provided value is a [`Boolean`] it will return [`Some`]
/// containing the inner [`bool`] otherwise returns [`None`]
///
/// [`Boolean`]: Values::Boolean
pub fn get_bool(&self) -> Option<bool> {
match self {
Values::Boolean(bool) => Some(*bool),
_ => None,
}
}
/// if the provided value is a [`String`] it will return [`Some`]
/// containing the inner [`str`] otherwise returns [`None`]
///
/// [`String`]: Values::String
/// [`str`]: String
pub fn get_string(&self) -> Option<String> {
match self {
Values::String(string) => Some(string.to_string()),
_ => None,
}
}
/// if the provided value is a [`Number`] it will return [`Some`]
/// containing the inner [`f64`] otherwise returns [`None`]
///
/// [`Number`]: Values::Number
pub fn get_number(&self) -> Option<f64> {
match self {
Values::Number(num) => Some(*num),
_ => None,
}
}
/// if the provided value is a [`Array`] it will return [`Some`]
/// containing the inner [`Vec<Values>`] otherwise returns [`None`]
///
/// [`Array`]: Values::Array
pub fn get_list_opt(&self) -> Option<Vec<Values>> {
match self {
Values::Array(arr) => Some(arr.to_vec()),
_ => None,
}
}
/// if the provided value is a [`Array`] it will return it
/// otherwise an empty Vec
///
/// [`Array`]: Values::Array
pub fn get_list(&self) -> Vec<Values> {
self.get_list_opt().unwrap_or_default()
}
/// get the Type of this [`Values`] Object as a String
/// It could be:
/// [`STRING`], [`NUMBER`], [`STRUCT`], [`NULL`], [`ARRAY`] or [`BOOLEAN`]
///
///[`STRING`]: Self::STRING
///[`NUMBER`]: Self::NUMBER
///[`STRUCT`]: Self::STRUCT
///[`NULL`]: Self::NULL
///[`ARRAY`]: Self::ARRAY
///[`BOOLEAN`]: Self::BOOLEAN
pub fn get_type_as_string(&self) -> &str {
match self {
Values::String(_) => Self::STRING,
Values::Number(_) => Self::NUMBER,
Values::Struct(_) => Self::STRUCT,
Values::Null => Self::NULL,
Values::Array(_) => Self::ARRAY,
Values::Boolean(_) => Self::BOOLEAN,
}
}
/// returns true if the provided Value is [`Boolean`]
///
/// [`Boolean`]: Values::Boolean
pub fn is_bool(&self) -> bool {
self.get_type_as_string().eq(Self::BOOLEAN)
}
/// returns true if the provided Value is [`Null`]
///
/// [`Null`]: Values::Null
pub fn is_null(&self) -> bool {
self.get_type_as_string().eq(Self::NULL)
}
/// returns true if the provided Value is [`String`]
///
/// [`String`]: Values::String
pub fn is_string(&self) -> bool {
self.get_type_as_string().eq(Self::STRING)
}
/// returns true if the provided Value is [`Number`]
///
/// [`Number`]: Values::Number
pub fn is_number(&self) -> bool {
self.get_type_as_string().eq(Self::NUMBER)
}
/// returns true if the provided Value is [`Struct`]
///
/// [`Struct`]: Values::Struct
pub fn is_struct(&self) -> bool {
self.get_type_as_string().eq(Self::STRUCT)
}
/// returns true if the provided Value is [`Array`]
///
/// [`Array`]: Values::Array
pub fn is_array(&self) -> bool {
self.get_type_as_string().eq(Self::ARRAY)
}
}
impl Display for Values {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Values::String(string) => write!(f, "\"{}\"", string.replace('"', "\\\"")),
Values::Number(number) => write!(f, "{}", number),
Values::Struct(r#struct) => {
write!(f, "{{")?;
let mut first = true;
for (key, val) in r#struct {
if first {
write!(f, "\"{}\":{}", key, val)?;
first = false;
} else {
write!(f, ",\"{}\":{}", key, val)?
}
}
write!(f, "}}")
}
Values::Array(arr) => {
write!(f, "[")?;
let mut first = true;
for item in arr {
if first {
write!(f, "{}", item)?;
first = false;
} else {
write!(f, ",{}", item)?;
}
}
write!(f, "]")
}
Values::Null => write!(f, "{}", Self::NULL),
Values::Boolean(bool) => write!(f, "{}", bool),
}
}
}
#[cfg(test)]
mod tests {
use crate::serializer::Serialize;
use crate::values::Values;
use crate::{map, FALSE, NULL, TRUE};
#[test]
pub fn display_on_bool_true() {
let bool = TRUE;
assert_eq!(bool.to_string(), "true")
}
#[test]
pub fn display_on_bool_false() {
let bool = FALSE;
assert_eq!(bool.to_string(), "false")
}
#[test]
pub fn display_on_null() {
let null = NULL;
assert_eq!(null.to_string(), "null")
}
#[test]
pub fn display_on_arr() {
let arr = vec![Values::Null, Values::Null];
let null = Values::Array(arr);
assert_eq!(null.to_string(), "[null,null]")
}
#[test]
pub fn display_on_num() {
let num = Values::Number(1.56);
assert_eq!(num.to_string(), "1.56")
}
#[test]
pub fn display_on_string() {
let num = Values::String(String::from("TEST TEST TEST"));
assert_eq!(num.to_string(), "\"TEST TEST TEST\"")
}
#[test]
pub fn display_on_struct() {
struct Hello {
hello: String,
}
impl Serialize for Hello {
fn serialize(&self) -> Values {
Values::Struct(map!(("hello", &self.hello)))
}
}
let struc = Hello {
hello: String::from("Moin"),
}
.serialize();
assert_eq!(struc.to_string(), "{\"hello\":\"Moin\"}");
}
#[test]
pub fn display_on_arr_with_struct() {
struct Hello {
hello: String,
}
impl Serialize for Hello {
fn serialize(&self) -> Values {
Values::Struct(map!(("hello", &self.hello)))
}
}
let arr = vec![
Hello {
hello: String::from("Moin"),
},
Hello {
hello: String::from("IDK"),
},
Hello {
hello: String::from("Hel\"lo"),
},
];
assert_eq!(
arr.serialize().to_string(),
"[{\"hello\":\"Moin\"},{\"hello\":\"IDK\"},{\"hello\":\"Hel\\\"lo\"}]"
);
}
}