@@ -188,6 +188,7 @@ fn inner_structs_with_options() {
188188 assert_eq ! ( encode!( v) ,
189189 bdoc! {
190190 "a" => {
191+ "a" => ( Bson :: Null ) ,
191192 "b" => {
192193 "a" => "foo" ,
193194 "b" => ( 4.5 )
@@ -201,6 +202,43 @@ fn inner_structs_with_options() {
201202 assert_eq ! ( v, decode!( encode!( v) ) ) ;
202203}
203204
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+
204242#[ test]
205243fn hashmap ( ) {
206244 #[ derive( Serialize , Deserialize , PartialEq , Debug ) ]
@@ -295,7 +333,7 @@ fn missing_errors() {
295333
296334 let mut d = Decoder :: new ( bdoc ! { } ) ;
297335 let a: Result < Foo , DecoderError > = Deserialize :: deserialize ( & mut d) ;
298-
336+
299337 assert ! ( a. is_err( ) ) ;
300338}
301339
@@ -305,18 +343,28 @@ fn parse_enum() {
305343 struct Foo { a : E }
306344 #[ derive( Serialize , Deserialize , PartialEq , Debug ) ]
307345 enum E {
346+ Empty ,
308347 Bar ( i32 ) ,
309348 Baz ( f64 ) ,
349+ Pair ( i32 , i32 ) ,
310350 Last ( Foo2 ) ,
351+ Vector ( Vec < i32 > ) ,
352+ Named { a : i32 } ,
353+ MultiNamed { a : i32 , b : i32 } ,
311354 }
312355 #[ derive( Serialize , Deserialize , PartialEq , Debug ) ]
313356 struct Foo2 {
314357 test : String ,
315358 }
316359
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+
317367 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
320368 assert_eq ! (
321369 encode!( v) ,
322370 bdoc! { "a" => { "Bar" => 10 } }
@@ -326,7 +374,14 @@ fn parse_enum() {
326374 let v = Foo { a : E :: Baz ( 10.2 ) } ;
327375 assert_eq ! (
328376 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 ] } }
330385 ) ;
331386 assert_eq ! ( v, decode!( encode!( v) ) ) ;
332387
@@ -336,6 +391,26 @@ fn parse_enum() {
336391 bdoc! { "a" => { "Last" => { "test" => "test" } } }
337392 ) ;
338393 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) ) ) ;
339414}
340415
341416#[ test]
0 commit comments