Skip to content

Commit 2ba7316

Browse files
committed
feat: add support for nested Vec attributes in serialization
1 parent 60628c8 commit 2ba7316

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

yaserde/tests/serializer.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,28 @@ fn ser_vec_as_attribute() {
442442
let content = r#"<TestTag numbers="1 2 3 4" strings="hello world" bools="true false true" floats="3.14 2.71" />"#;
443443
serialize_and_validate!(model, content);
444444
}
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+
}

yaserde_derive/src/ser/expand_struct.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,19 @@ pub fn serialize(
142142
Field::FieldOption { .. } | Field::FieldVec { .. } => {
143143
unimplemented!("Nested Option or Vec in Vec not supported for attributes")
144144
}
145-
Field::FieldStruct { .. } => {
146-
unimplemented!("Struct fields in Vec not supported for attributes")
147-
}
148-
}
145+
Field::FieldStruct { .. } => field.ser_wrap_default_attribute(
146+
Some(quote! {
147+
self.#label
148+
.iter()
149+
.map(|item| ::yaserde::ser::to_string_content(item))
150+
.collect::<::std::result::Result<::std::vec::Vec<_>, _>>()?
151+
.join(" ")
152+
}),
153+
quote!({
154+
struct_start_event.attr(#label_name, &yaserde_inner)
155+
}),
156+
),
157+
},
149158
}
150159
} else {
151160
match field.get_type() {

0 commit comments

Comments
 (0)