@@ -188,6 +188,7 @@ fn inner_structs_with_options() {
188
188
assert_eq ! ( encode!( v) ,
189
189
bdoc! {
190
190
"a" => {
191
+ "a" => ( Bson :: Null ) ,
191
192
"b" => {
192
193
"a" => "foo" ,
193
194
"b" => ( 4.5 )
@@ -201,6 +202,43 @@ fn inner_structs_with_options() {
201
202
assert_eq ! ( v, decode!( encode!( v) ) ) ;
202
203
}
203
204
205
+ #[ test]
206
+ fn inner_structs_with_skippable_options ( ) {
207
+ #[ derive( Serialize , Deserialize , PartialEq , Debug ) ]
208
+ struct Foo {
209
+ #[ serde( skip_serializing_if="Option::is_none" ) ]
210
+ a : Option < Box < Foo > > ,
211
+ b : Bar ,
212
+ }
213
+ #[ derive( Serialize , Deserialize , PartialEq , Debug ) ]
214
+ struct Bar {
215
+ a : String ,
216
+ b : f64 ,
217
+ }
218
+
219
+ let v = Foo {
220
+ a : Some ( Box :: new ( Foo {
221
+ a : None ,
222
+ b : Bar { a : "foo" . to_string ( ) , b : 4.5 } ,
223
+ } ) ) ,
224
+ b : Bar { a : "bar" . to_string ( ) , b : 1.0 } ,
225
+ } ;
226
+ assert_eq ! ( encode!( v) ,
227
+ bdoc! {
228
+ "a" => {
229
+ "b" => {
230
+ "a" => "foo" ,
231
+ "b" => ( 4.5 )
232
+ }
233
+ } ,
234
+ "b" => {
235
+ "a" => "bar" ,
236
+ "b" => ( 1.0 )
237
+ }
238
+ } ) ;
239
+ assert_eq ! ( v, decode!( encode!( v) ) ) ;
240
+ }
241
+
204
242
#[ test]
205
243
fn hashmap ( ) {
206
244
#[ derive( Serialize , Deserialize , PartialEq , Debug ) ]
@@ -295,7 +333,7 @@ fn missing_errors() {
295
333
296
334
let mut d = Decoder :: new ( bdoc ! { } ) ;
297
335
let a: Result < Foo , DecoderError > = Deserialize :: deserialize ( & mut d) ;
298
-
336
+
299
337
assert ! ( a. is_err( ) ) ;
300
338
}
301
339
@@ -305,18 +343,28 @@ fn parse_enum() {
305
343
struct Foo { a : E }
306
344
#[ derive( Serialize , Deserialize , PartialEq , Debug ) ]
307
345
enum E {
346
+ Empty ,
308
347
Bar ( i32 ) ,
309
348
Baz ( f64 ) ,
349
+ Pair ( i32 , i32 ) ,
310
350
Last ( Foo2 ) ,
351
+ Vector ( Vec < i32 > ) ,
352
+ Named { a : i32 } ,
353
+ MultiNamed { a : i32 , b : i32 } ,
311
354
}
312
355
#[ derive( Serialize , Deserialize , PartialEq , Debug ) ]
313
356
struct Foo2 {
314
357
test : String ,
315
358
}
316
359
360
+ let v = Foo { a : E :: Empty } ;
361
+ assert_eq ! (
362
+ encode!( v) ,
363
+ bdoc! { "a" => "Empty" }
364
+ ) ;
365
+ assert_eq ! ( v, decode!( encode!( v) ) ) ;
366
+
317
367
let v = Foo { a : E :: Bar ( 10 ) } ;
318
- // technically serde is correct here. a single element tuple still is a
319
- // tuple and therefor a sequence
320
368
assert_eq ! (
321
369
encode!( v) ,
322
370
bdoc! { "a" => { "Bar" => 10 } }
@@ -326,7 +374,14 @@ fn parse_enum() {
326
374
let v = Foo { a : E :: Baz ( 10.2 ) } ;
327
375
assert_eq ! (
328
376
encode!( v) ,
329
- bdoc! { "a" => { "Baz" => ( 10.2 ) } }
377
+ bdoc! { "a" => { "Baz" => 10.2 } }
378
+ ) ;
379
+ assert_eq ! ( v, decode!( encode!( v) ) ) ;
380
+
381
+ let v = Foo { a : E :: Pair ( 12 , 42 ) } ;
382
+ assert_eq ! (
383
+ encode!( v) ,
384
+ bdoc! { "a" => { "Pair" => [ 12 , 42 ] } }
330
385
) ;
331
386
assert_eq ! ( v, decode!( encode!( v) ) ) ;
332
387
@@ -336,6 +391,26 @@ fn parse_enum() {
336
391
bdoc! { "a" => { "Last" => { "test" => "test" } } }
337
392
) ;
338
393
assert_eq ! ( v, decode!( encode!( v) ) ) ;
394
+
395
+ let v = Foo { a : E :: Vector ( vec ! ( 12 , 42 ) ) } ;
396
+ assert_eq ! (
397
+ encode!( v) ,
398
+ bdoc! { "a" => { "Vector" => [ 12 , 42 ] } }
399
+ ) ;
400
+ assert_eq ! ( v, decode!( encode!( v) ) ) ;
401
+
402
+ let v = Foo { a : E :: Named { a : 12 } } ;
403
+ assert_eq ! (
404
+ encode!( v) ,
405
+ bdoc! { "a" => { "Named" => { "a" => 12 } } }
406
+ ) ;
407
+ assert_eq ! ( v, decode!( encode!( v) ) ) ;
408
+ let v = Foo { a : E :: MultiNamed { a : 12 , b : 42 } } ;
409
+ assert_eq ! (
410
+ encode!( v) ,
411
+ bdoc! { "a" => { "MultiNamed" => { "a" => 12 , "b" => 42 } } }
412
+ ) ;
413
+ assert_eq ! ( v, decode!( encode!( v) ) ) ;
339
414
}
340
415
341
416
#[ test]
0 commit comments