Debian Codegraph @

tests/115_minimal_flattening.rs

raw symbols SCIP
1 use std::collections::HashMap;
2
3 use ron::error::Span;
4 use serde::{Deserialize, Serialize};
5
6 #[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
7 struct Main {
8 #[serde(flatten)]
9 required: Required,
10 #[serde(flatten)]
11 optional: Optional,
12
13 some_other_field: u32,
14 }
15
16 #[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
17 struct Required {
18 first: u32,
19 second: u32,
20 }
21
22 #[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
23 struct Optional {
24 third: Option<u32>,
25 }
26
27 #[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
28 struct MyType {
29 first: u32,
30 second: u32,
31 #[serde(flatten)]
32 everything_else: HashMap<String, ron::Value>,
33 }
34
35 #[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
36 struct AllOptional {
37 #[serde(flatten)]
38 everything_else: HashMap<String, ron::Value>,
39 }
40
41 #[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
42 enum Newtype {
43 Main(Main),
44 MyType(MyType),
45 AllOptional(AllOptional),
46 }
47
48 #[test]
49 fn test_flatten_struct_into_struct() {
50 let val = Main {
51 required: Required {
52 first: 1,
53 second: 2,
54 },
55 optional: Optional { third: Some(3) },
56 some_other_field: 1337,
57 };
58
59 let ron = ron::ser::to_string_pretty(&val, ron::ser::PrettyConfig::default()).unwrap();
60
61 assert_eq!(
62 ron,
63 "{
64 \"first\": 1,
65 \"second\": 2,
66 \"third\": Some(3),
67 \"some_other_field\": 1337,
68 }"
69 );
70
71 let de: Main = ron::from_str(&ron).unwrap();
72
73 assert_eq!(de, val);
74
75 let val = Newtype::Main(Main {
76 required: Required {
77 first: 1,
78 second: 2,
79 },
80 optional: Optional { third: Some(3) },
81 some_other_field: 1337,
82 });
83
84 let ron = ron::ser::to_string_pretty(
85 &val,
86 ron::ser::PrettyConfig::default()
87 .extensions(ron::extensions::Extensions::UNWRAP_VARIANT_NEWTYPES),
88 )
89 .unwrap();
90
91 assert_eq!(
92 ron,
93 "#![enable(unwrap_variant_newtypes)]
94 Main({
95 \"first\": 1,
96 \"second\": 2,
97 \"third\": Some(3),
98 \"some_other_field\": 1337,
99 })"
100 );
101
102 let ron = ron::ser::to_string_pretty(&val, ron::ser::PrettyConfig::default()).unwrap();
103
104 let de: Newtype = ron::from_str(&ron).unwrap();
105
106 assert_eq!(de, val);
107
108 assert_eq!(
109 ron::from_str::<Main>(
110 "{
111 first\": 1,
112 \"second\": 2,
113 \"third\": Some(3),
114 \"some_other_field\": 1337,
115 }"
116 ),
117 Err(ron::error::SpannedError {
118 code: ron::error::Error::ExpectedString,
119 span: Span {
120 start: ron::error::Position { line: 1, col: 2 },
121 end: ron::error::Position { line: 2, col: 9 },
122 }
123 })
124 );
125
126 assert_eq!(
127 ron::from_str::<Main>(
128 "{
129 \"first\": 1,
130 \"second: 2,
131 \"third\": Some(3),
132 \"some_other_field\": 1337,
133 }"
134 ),
135 Err(ron::error::SpannedError {
136 code: ron::error::Error::ExpectedMapColon,
137 span: Span {
138 start: ron::error::Position { line: 4, col: 10 },
139 end: ron::error::Position { line: 4, col: 10 },
140 }
141 })
142 );
143
144 assert_eq!(
145 ron::from_str::<Main>(
146 "{
147 \"first\": 1,
148 \"second\": 2,
149 third\": Some(3),
150 \"some_other_field\": 1337,
151 }"
152 ),
153 Err(ron::error::SpannedError {
154 code: ron::error::Error::ExpectedString,
155 span: Span {
156 start: ron::error::Position { line: 4, col: 9 },
157 end: ron::error::Position { line: 4, col: 9 },
158 }
159 })
160 );
161
162 assert_eq!(
163 ron::from_str::<Main>(
164 "{
165 \"first\": 1,
166 \"second\": 2,
167 \"third\": Some(3),
168 \"some_other_field: 1337,
169 }"
170 ),
171 Err(ron::error::SpannedError {
172 code: ron::error::Error::ExpectedStringEnd,
173 span: Span {
174 start: ron::error::Position { line: 5, col: 9 },
175 end: ron::error::Position { line: 5, col: 10 },
176 }
177 })
178 );
179 }
180
181 #[test]
182 fn test_flatten_rest() {
183 let val = MyType {
184 first: 1,
185 second: 2,
186 everything_else: {
187 let mut map = HashMap::new();
188 map.insert(
189 String::from("third"),
190 ron::Value::Number(ron::value::Number::U8(3)),
191 );
192 map
193 },
194 };
195
196 let ron = ron::ser::to_string_pretty(&val, ron::ser::PrettyConfig::default()).unwrap();
197
198 assert_eq!(
199 ron,
200 "{
201 \"first\": 1,
202 \"second\": 2,
203 \"third\": 3,
204 }"
205 );
206
207 let de: MyType = ron::from_str(&ron).unwrap();
208
209 assert_eq!(de, val);
210
211 let val = Newtype::MyType(MyType {
212 first: 1,
213 second: 2,
214 everything_else: {
215 let mut map = HashMap::new();
216 map.insert(
217 String::from("third"),
218 ron::Value::Number(ron::value::Number::U8(3)),
219 );
220 map
221 },
222 });
223
224 let ron = ron::ser::to_string_pretty(
225 &val,
226 ron::ser::PrettyConfig::default()
227 .extensions(ron::extensions::Extensions::UNWRAP_VARIANT_NEWTYPES),
228 )
229 .unwrap();
230
231 assert_eq!(
232 ron,
233 "#![enable(unwrap_variant_newtypes)]
234 MyType({
235 \"first\": 1,
236 \"second\": 2,
237 \"third\": 3,
238 })"
239 );
240
241 let de: Newtype = ron::from_str(&ron).unwrap();
242
243 assert_eq!(de, val);
244
245 assert_eq!(
246 ron::from_str::<MyType>(
247 "{
248 first\": 1,
249 \"second\": 2,
250 \"third\": 3,
251 }"
252 ),
253 Err(ron::error::SpannedError {
254 code: ron::error::Error::ExpectedString,
255 span: Span {
256 start: ron::error::Position { line: 1, col: 2 },
257 end: ron::error::Position { line: 2, col: 9 },
258 }
259 })
260 );
261
262 assert_eq!(
263 ron::from_str::<MyType>(
264 "{
265 \"first\": 1,
266 \"second: 2,
267 \"third\": 3,
268 }"
269 ),
270 Err(ron::error::SpannedError {
271 code: ron::error::Error::ExpectedMapColon,
272 span: Span {
273 start: ron::error::Position { line: 4, col: 10 },
274 end: ron::error::Position { line: 4, col: 10 },
275 }
276 })
277 );
278
279 assert_eq!(
280 ron::from_str::<MyType>(
281 "{
282 \"first\": 1,
283 \"second\": 2,
284 third\": 3,
285 }"
286 ),
287 Err(ron::error::SpannedError {
288 code: ron::error::Error::ExpectedString,
289 span: Span {
290 start: ron::error::Position { line: 4, col: 9 },
291 end: ron::error::Position { line: 4, col: 9 },
292 }
293 })
294 );
295
296 assert_eq!(
297 ron::from_str::<MyType>(
298 "{
299 \"first\": 1,
300 \"second\": 2,
301 \"third: 3,
302 }"
303 ),
304 Err(ron::error::SpannedError {
305 code: ron::error::Error::ExpectedStringEnd,
306 span: Span {
307 start: ron::error::Position { line: 4, col: 9 },
308 end: ron::error::Position { line: 4, col: 10 },
309 }
310 })
311 );
312 }
313
314 #[test]
315 fn test_flatten_only_rest() {
316 let val = AllOptional {
317 everything_else: HashMap::new(),
318 };
319
320 let ron = ron::ser::to_string(&val).unwrap();
321
322 assert_eq!(ron, "{}");
323
324 let de: AllOptional = ron::from_str(&ron).unwrap();
325
326 assert_eq!(de, val);
327
328 let val = Newtype::AllOptional(AllOptional {
329 everything_else: HashMap::new(),
330 });
331
332 let ron = ron::ser::to_string_pretty(
333 &val,
334 ron::ser::PrettyConfig::default()
335 .extensions(ron::extensions::Extensions::UNWRAP_VARIANT_NEWTYPES),
336 )
337 .unwrap();
338
339 assert_eq!(
340 ron,
341 "#![enable(unwrap_variant_newtypes)]
342 AllOptional({
343 })"
344 );
345
346 let de: Newtype = ron::from_str(&ron).unwrap();
347
348 assert_eq!(de, val);
349 }
350
351 #[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq, Serialize)]
352 #[serde(deny_unknown_fields)]
353 pub struct AvailableCards {
354 pub left: u8,
355 pub right: u8,
356 }
357
358 #[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
359 struct MapProperties {
360 #[serde(flatten)]
361 cards: AvailableCards,
362 }
363
364 #[test]
365 fn test_issue_456() {
366 let map_properties = MapProperties {
367 cards: AvailableCards {
368 ..Default::default()
369 },
370 };
371 let ron = ron::to_string(&map_properties).unwrap();
372
373 let de: MapProperties = ron::from_str(&ron).unwrap();
374
375 assert_eq!(map_properties, de);
376 }