|
| 1 | +use xmlity::{ |
| 2 | + de::DeserializationGroupBuilder, |
| 3 | + value::{XmlAttribute, XmlText}, |
| 4 | + DeserializationGroup, Deserialize, ExpandedName, LocalName, SerializationGroup, Serialize, |
| 5 | +}; |
| 6 | + |
| 7 | +use crate::define_test; |
| 8 | + |
| 9 | +#[derive(Debug, PartialEq)] |
| 10 | +pub struct AnyAttributes { |
| 11 | + /// The attributes of the element. |
| 12 | + pub attributes: Vec<XmlAttribute>, |
| 13 | +} |
| 14 | + |
| 15 | +impl<'de> DeserializationGroupBuilder<'de> for AnyAttributes { |
| 16 | + type Value = Self; |
| 17 | + |
| 18 | + fn contribute_attributes<D: xmlity::de::AttributesAccess<'de>>( |
| 19 | + &mut self, |
| 20 | + mut access: D, |
| 21 | + ) -> Result<bool, D::Error> { |
| 22 | + if let Ok(Some(attr)) = access.next_attribute::<XmlAttribute>() { |
| 23 | + self.attributes.push(attr); |
| 24 | + Ok(true) |
| 25 | + } else { |
| 26 | + Ok(false) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + fn attributes_done(&self) -> bool { |
| 31 | + false |
| 32 | + } |
| 33 | + |
| 34 | + fn finish<E: xmlity::de::Error>(self) -> Result<Self::Value, E> { |
| 35 | + Ok(self) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl<'de> DeserializationGroup<'de> for AnyAttributes { |
| 40 | + type Builder = Self; |
| 41 | + |
| 42 | + fn builder() -> Self::Builder { |
| 43 | + Self { |
| 44 | + attributes: Vec::new(), |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl SerializationGroup for AnyAttributes { |
| 50 | + fn serialize_attributes<S: xmlity::ser::SerializeAttributes>( |
| 51 | + &self, |
| 52 | + serializer: &mut S, |
| 53 | + ) -> Result<(), S::Error> { |
| 54 | + self.attributes |
| 55 | + .iter() |
| 56 | + .try_for_each(|attr| serializer.serialize_attribute(attr).map(|_| ())) |
| 57 | + } |
| 58 | + |
| 59 | + fn serialize_children<S: xmlity::ser::SerializeSeq>( |
| 60 | + &self, |
| 61 | + serializer: &mut S, |
| 62 | + ) -> Result<(), S::Error> { |
| 63 | + let _ = serializer; |
| 64 | + |
| 65 | + Ok(()) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +#[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 70 | +#[xelement(name = "a")] |
| 71 | +pub struct A { |
| 72 | + #[xgroup] |
| 73 | + any_attributes: AnyAttributes, |
| 74 | +} |
| 75 | + |
| 76 | +define_test!( |
| 77 | + any_attributes_element, |
| 78 | + [( |
| 79 | + A { |
| 80 | + any_attributes: AnyAttributes { |
| 81 | + attributes: vec![XmlAttribute::new( |
| 82 | + ExpandedName::new(LocalName::new_dangerous("test"), None), |
| 83 | + XmlText::new("testVal") |
| 84 | + )], |
| 85 | + } |
| 86 | + }, |
| 87 | + r###"<a test="testVal"/>"### |
| 88 | + )] |
| 89 | +); |
| 90 | + |
| 91 | +#[derive(Debug, PartialEq, SerializationGroup, DeserializationGroup)] |
| 92 | +pub struct B { |
| 93 | + #[xgroup] |
| 94 | + any_attributes: AnyAttributes, |
| 95 | +} |
| 96 | + |
| 97 | +#[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 98 | +#[xelement(name = "a")] |
| 99 | +pub struct C { |
| 100 | + #[xgroup] |
| 101 | + b: B, |
| 102 | +} |
| 103 | + |
| 104 | +define_test!( |
| 105 | + any_attributes_element_in_group, |
| 106 | + [( |
| 107 | + C { |
| 108 | + b: B { |
| 109 | + any_attributes: AnyAttributes { |
| 110 | + attributes: vec![XmlAttribute::new( |
| 111 | + ExpandedName::new(LocalName::new_dangerous("test"), None), |
| 112 | + XmlText::new("testVal") |
| 113 | + )], |
| 114 | + } |
| 115 | + } |
| 116 | + }, |
| 117 | + r###"<a test="testVal"/>"### |
| 118 | + )] |
| 119 | +); |
0 commit comments