-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathmacros.rs
More file actions
144 lines (131 loc) · 4.36 KB
/
macros.rs
File metadata and controls
144 lines (131 loc) · 4.36 KB
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
// macros.rs
pub use std::collections::HashMap;
pub use std::boxed::Box;
pub use std::string::ToString;
#[macro_export]
macro_rules! json {
(null) => {
$crate::Json::Null
};
([ $( $element:tt ),* ]) => {
$crate::Json::Array(vec![ $( json!($element) ),* ])
};
({ $( $key:tt : $value:tt ),* }) => {
{
let mut fields = $crate::macros::Box::new(
$crate::macros::HashMap::new());
$(
fields.insert($crate::macros::ToString::to_string($key), json!($value));
)*
$crate::Json::Object(fields)
}
};
($other:tt) => {
$crate::Json::from($other)
};
}
#[cfg(test)]
mod tests {
use crate::Json;
#[test]
fn json_with_rust_expressions() {
const HELLO: &'static str = "hello";
let macro_generated_value =
json!({
"math_works": (4 - 2 == 2),
"en": HELLO,
HELLO: "bonjour!"
})
;
let hand_coded_value = Json::Object(Box::new(vec![
("math_works".to_string(), Json::Boolean(true)),
("en".to_string(), Json::String("hello".to_string())),
("hello".to_string(), Json::String("bonjour!".to_string())),
].into_iter().collect()));
assert_eq!(macro_generated_value, hand_coded_value);
}
// Tests from earlier in the chapter should actually pass with this macro.
#[test]
fn original_example() {
let hand_coded_value = {
let students = Json::Array(vec![
Json::Object(Box::new(vec![
("name".to_string(), Json::String("Jim Blandy".to_string())),
("class_of".to_string(), Json::Number(1926.0)),
("major".to_string(), Json::String("Tibetan throat singing".to_string()))
].into_iter().collect())),
Json::Object(Box::new(vec![
("name".to_string(), Json::String("Jason Orendorff".to_string())),
("class_of".to_string(), Json::Number(1702.0)),
("major".to_string(), Json::String("Knots".to_string()))
].into_iter().collect()))
]);
students
};
let macro_generated_value = {
let students = json!([
{
"name": "Jim Blandy",
"class_of": 1926,
"major": "Tibetan throat singing"
},
{
"name": "Jason Orendorff",
"class_of": 1702,
"major": "Knots"
}
]);
students
};
assert_eq!(macro_generated_value, hand_coded_value);
}
#[test]
fn json_array_with_json_element() {
let macro_generated_value = json!(
[
// valid JSON that doesn't match `$element:expr`
{
"pitch": 440.0
}
]
);
let hand_coded_value =
Json::Array(vec![
Json::Object(Box::new(vec![
("pitch".to_string(), Json::Number(440.0))
].into_iter().collect()))
]);
assert_eq!(macro_generated_value, hand_coded_value);
}
#[test]
fn json_monolith() {
let width = 4.0;
let desc =
json!({
"width": width,
"height": (width * 9.0 / 4.0)
});
let hand_coded_value =
Json::Object(Box::new(vec![
("width".to_string(), Json::Number(width)),
("height".to_string(), Json::Number(width * 9.0 / 4.0))
].into_iter().collect()));
assert_eq!(desc, hand_coded_value);
}
#[test]
fn hygiene() {
// The surprise is that *the macro works as-is*.
// Rust renames the variable for you!
let fields = "Fields, W.C.";
let role = json!({
"name": "Larson E. Whipsnade",
"actor": fields
});
let hand_coded_value =
Json::Object(Box::new(vec![
("name".to_string(), Json::String("Larson E. Whipsnade".to_string())),
("actor".to_string(), Json::String("Fields, W.C.".to_string()))
].into_iter().collect()));
assert_eq!(role, hand_coded_value);
}
}