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
|
#[macro_use]
extern crate serde_derive;
extern crate serde_qs as qs;
use std::collections::HashMap;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct Address {
city: String,
postcode: String,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct QueryParams {
id: u8,
name: String,
address: Address,
phone: u32,
user_ids: Vec<u8>,
}
// Compares a map generated by hash_to_map with the map returned by
// qs::from_str. All types are inferred by the compiler.
macro_rules! map_test {
($string:expr, $($mapvars:tt)*) => {
let expected_map = hash_to_map!(New $($mapvars)*);
let testmap: HashMap<_, _> = qs::from_str($string).unwrap();
assert_eq!(expected_map, testmap);
}
}
// Macro used to quickly generate a nested HashMap from a string.
macro_rules! hash_to_map {
// Base case: a map with no inputs, do nothing.
($map:expr, ) => ();
//{}
// This parses a single map entry, with a value explicitly an expression.
($map:expr, $k:tt[e $v:expr] $($rest:tt)*) => {{
$map.insert($k.to_owned(), $v.to_owned());
hash_to_map!($map, $($rest)*);
}};
// This parses a single map entry, plus the rest of the values.
($map:expr, $k:tt[$v:tt] $($rest:tt)*) => {{
$map.insert($k.to_owned(), $v.to_owned());
hash_to_map!($map, $($rest)*);
}};
// This parses the first entry as a nested entry, and tail calls the
// remaining in rest.
($map:expr, $k:tt[$($inner:tt)*] $($rest:tt)*) => {{
let mut inner_map = HashMap::new();
hash_to_map!(inner_map, $($inner)*);
$map.insert($k.to_owned(), inner_map);
hash_to_map!($map, $($rest)*);
}};
// Constructs the map and then runs the macro. This infers the types for the
// hashmap.
(New $($rest:tt)*) => {{
let mut map = HashMap::new();
hash_to_map!(map, $($rest)*);
map
}}
}
#[test]
fn deserialize_struct() {
let params = QueryParams {
id: 42,
name: "Acme".to_string(),
phone: 12345,
address: Address {
city: "Carrot City".to_string(),
postcode: "12345".to_string(),
},
user_ids: vec![1, 2, 3, 4],
};
// standard parameters
let rec_params: QueryParams = qs::from_str("\
name=Acme&id=42&phone=12345&address[postcode]=12345&\
address[city]=Carrot+City&user_ids[0]=1&user_ids[1]=2&\
user_ids[2]=3&user_ids[3]=4")
.unwrap();
assert_eq!(rec_params, params);
// unindexed arrays
let rec_params: QueryParams = qs::from_str("\
name=Acme&id=42&phone=12345&address[postcode]=12345&\
address[city]=Carrot+City&user_ids[]=1&user_ids[]=2&\
user_ids[]=3&user_ids[]=4")
.unwrap();
assert_eq!(rec_params, params);
// ordering doesn't matter
let rec_params: QueryParams = qs::from_str("\
address[city]=Carrot+City&user_ids[]=1&user_ids[]=2&\
name=Acme&id=42&phone=12345&address[postcode]=12345&\
user_ids[]=3&user_ids[]=4")
.unwrap();
assert_eq!(rec_params, params);
}
#[test]
fn qs_test_simple() {
// test('parse()', function (t) {
// t.test('parses a simple string', function (st) {
// st.deepEqual(qs.parse('0=foo'), { 0: 'foo' });
map_test!("0=foo", 0["foo"]);
// st.deepEqual(qs.parse('foo=c++'), { foo: 'c ' });
map_test!("foo=c++", "foo"["c "]);
// st.deepEqual(qs.parse('a[>=]=23'), { a: { '>=': '23' } });
map_test!("a[>=]=23", "a"[">="[23]]);
// st.deepEqual(qs.parse('a[<=>]==23'), { a: { '<=>': '=23' } });
map_test!("a[<=>]==23", "a"["<=>"["=23"]]);
// st.deepEqual(qs.parse('a[==]=23'), { a: { '==': '23' } });
map_test!("a[==]=23", "a"["=="[23]]);
// st.deepEqual(qs.parse('foo', { strictNullHandling: true }),
// { foo: null });
let none: Option<String> = Option::None;
map_test!("foo", "foo"[none]);
// st.deepEqual(qs.parse('foo'), { foo: '' });
map_test!("foo", "foo"[""]);
// st.deepEqual(qs.parse('foo='), { foo: '' });
map_test!("foo=", "foo"[""]);
// st.deepEqual(qs.parse('foo=bar'), { foo: 'bar' });
map_test!("foo=bar", "foo"["bar"]);
// st.deepEqual(qs.parse(' foo = bar = baz '), { ' foo ': ' bar = baz ' });
map_test!(" foo = bar = baz ", " foo "[" bar = baz "]);
// st.deepEqual(qs.parse('foo=bar=baz'), { foo: 'bar=baz' });
map_test!("foo=bar=baz", "foo"["bar=baz"]);
// st.deepEqual(qs.parse('foo=bar&bar=baz'), { foo: 'bar', bar: 'baz' });
map_test!("foo=bar&bar=baz", "foo"["bar"] "bar"["baz"]);
// st.deepEqual(qs.parse('foo2=bar2&baz2='), { foo2: 'bar2', baz2: '' });
map_test!("foo2=bar2&baz2=", "foo2"["bar2"] "baz2"[""]);
// st.deepEqual(qs.parse('foo=bar&baz', { strictNullHandling: true }), {
// foo: 'bar', baz: null });
map_test!("foo=bar&baz", "foo"[e Some("bar".to_string())] "baz"[e None]);
// st.deepEqual(qs.parse('foo=bar&baz'), { foo: 'bar', baz: '' });
map_test!("foo=bar&baz", "foo"["bar"] "baz"[""]);
// st.deepEqual(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'),
// {
// cht: 'p3',
// chd: 't:60,40',
// chs: '250x100',
// chl: 'Hello|World'
// });
map_test!("cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World",
"cht"["p3"]
"chd"["t:60,40"]
"chs"["250x100"]
"chl"["Hello|World"]
);
// st.end();
// });
}
#[test]
fn qs_nesting() {
// t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single
// nested string');
map_test!("a[b]=c", "a"["b"["c"]]);
// t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a
// double nested string');
map_test!("a[b][c]=d", "a"["b"["c"["d"]]]);
// t.deepEqual(
// qs.parse('a[b][c][d][e][f][g][h]=i'),
// { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } },
// 'defaults to a depth of 5'
// );
map_test!("a[b][c][d][e][f][g][h]=i",
"a"["b"["c"["d"["e"["f"["[g][h]"["i"]]]]]]]);
}
|