@@ -415,3 +415,138 @@ fn ser_custom() {
415415 let content = "<Date><Year>2020</Year><Month>1</Month><DoubleDay>10</DoubleDay></Date>" ;
416416 serialize_and_validate ! ( model, content) ;
417417}
418+
419+ #[ test]
420+ fn ser_vec_as_attribute ( ) {
421+ #[ derive( YaSerialize , PartialEq , Debug ) ]
422+ #[ yaserde( rename = "TestTag" ) ]
423+ pub struct VecAttributeStruct {
424+ #[ yaserde( attribute = true ) ]
425+ numbers : Vec < u32 > ,
426+ #[ yaserde( attribute = true ) ]
427+ strings : Vec < String > ,
428+ #[ yaserde( attribute = true ) ]
429+ bools : Vec < bool > ,
430+ #[ yaserde( attribute = true ) ]
431+ floats : Vec < f64 > ,
432+ }
433+
434+ let model = VecAttributeStruct {
435+ numbers : vec ! [ 1 , 2 , 3 , 4 ] ,
436+ strings : vec ! [ "hello" . to_string( ) , "world" . to_string( ) ] ,
437+ bools : vec ! [ true , false , true ] ,
438+ floats : vec ! [ 6.14 , 2.71 ] ,
439+ } ;
440+
441+ // Expected XML with space-separated attribute values
442+ let content = r#"<TestTag numbers="1 2 3 4" strings="hello world" bools="true false true" floats="6.14 2.71" />"# ;
443+ serialize_and_validate ! ( model, content) ;
444+ }
445+
446+ #[ test]
447+ fn ser_vec_as_attribute_nested ( ) {
448+ #[ derive( YaSerialize , PartialEq , Debug ) ]
449+ #[ yaserde( rename = "TestTag" ) ]
450+ struct VecAttributeStruct {
451+ #[ yaserde( attribute = true ) ]
452+ outer : Vec < Inner > ,
453+ }
454+
455+ #[ derive( YaSerialize , PartialEq , Debug ) ]
456+ #[ yaserde( rename = "TestTag" ) ]
457+ enum Inner {
458+ One ,
459+ Two ,
460+ }
461+
462+ let model = VecAttributeStruct {
463+ outer : vec ! [ Inner :: One , Inner :: Two ] ,
464+ } ;
465+
466+ // Expected XML with space-separated attribute values
467+ let content = r#"<TestTag outer="One Two" />"# ;
468+ serialize_and_validate ! ( model, content) ;
469+ }
470+
471+ #[ test]
472+ fn ser_option_vec_as_attribute ( ) {
473+ #[ derive( YaSerialize , PartialEq , Debug ) ]
474+ #[ yaserde( rename = "TestTag" ) ]
475+ pub struct OptionVecAttributeStruct {
476+ #[ yaserde( attribute = true ) ]
477+ field : Option < Vec < u32 > > ,
478+ }
479+
480+ // Expected XML with space-separated attribute values
481+ let model = OptionVecAttributeStruct {
482+ field : Some ( vec ! [ 1 , 2 , 3 , 4 ] ) ,
483+ } ;
484+ let content = r#"<TestTag field="1 2 3 4" />"# ;
485+ serialize_and_validate ! ( model, content) ;
486+
487+ let model = OptionVecAttributeStruct {
488+ field : Some ( vec ! [ ] ) ,
489+ } ;
490+ let content = r#"<TestTag field="" />"# ;
491+ serialize_and_validate ! ( model, content) ;
492+
493+ // Expected XML with no attributes
494+ let model = OptionVecAttributeStruct { field : None } ;
495+ let content = r#"<TestTag />"# ;
496+ serialize_and_validate ! ( model, content) ;
497+ }
498+
499+ #[ test]
500+ fn ser_option_vec_complex ( ) {
501+ #[ derive( Default , PartialEq , Debug , YaSerialize ) ]
502+ pub struct Start {
503+ #[ yaserde( attribute = true , rename = "value" ) ]
504+ pub value : String ,
505+ }
506+
507+ #[ derive( Default , PartialEq , Debug , YaSerialize ) ]
508+ #[ yaserde( rename = "String" ) ]
509+ pub struct StringStruct {
510+ #[ yaserde( rename = "Start" ) ]
511+ pub start : Option < Vec < Start > > ,
512+ }
513+
514+ // Test serialization with Some(vec)
515+ let model = StringStruct {
516+ start : Some ( vec ! [
517+ Start {
518+ value: "First string" . to_string( ) ,
519+ } ,
520+ Start {
521+ value: "Second string" . to_string( ) ,
522+ } ,
523+ Start {
524+ value: "Third string" . to_string( ) ,
525+ } ,
526+ ] ) ,
527+ } ;
528+
529+ let content = yaserde:: ser:: to_string ( & model) . unwrap ( ) ;
530+ assert_eq ! (
531+ content,
532+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?><String><Start value=\" First string\" /><Start value=\" Second string\" /><Start value=\" Third string\" /></String>"
533+ ) ;
534+
535+ // Test serialization with None
536+ let model_none = StringStruct { start : None } ;
537+ let content_none = yaserde:: ser:: to_string ( & model_none) . unwrap ( ) ;
538+ assert_eq ! (
539+ content_none,
540+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?><String />"
541+ ) ;
542+
543+ // Test serialization with Some(empty_vec)
544+ let model_empty = StringStruct {
545+ start : Some ( vec ! [ ] ) ,
546+ } ;
547+ let content_empty = yaserde:: ser:: to_string ( & model_empty) . unwrap ( ) ;
548+ assert_eq ! (
549+ content_empty,
550+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?><String />"
551+ ) ;
552+ }
0 commit comments