use peanuts::{ element::{FromElement, IntoElement}, Element, }; pub const XMLNS: &str = "urn:xmpp:avatar:metadata"; #[derive(Debug, Clone)] pub struct Metadata { pub info: Vec, pub pointers: Vec, } impl FromElement for Metadata { fn from_element(mut element: Element) -> peanuts::element::DeserializeResult { element.check_name("metadata")?; element.check_namespace(XMLNS)?; let info = element.pop_children()?; let pointers = element.pop_children()?; Ok(Self { info, pointers }) } } impl IntoElement for Metadata { fn builder(&self) -> peanuts::element::ElementBuilder { Element::builder("metadata", Some(XMLNS)) .push_children(self.info.clone()) .push_children(self.pointers.clone()) } } #[derive(Debug, Clone)] pub struct Info { pub bytes: u32, pub height: Option, pub id: String, pub r#type: String, pub url: Option, pub width: Option, } impl FromElement for Info { fn from_element(mut element: Element) -> peanuts::element::DeserializeResult { element.check_name("info")?; element.check_namespace(XMLNS)?; let bytes = element.attribute("bytes")?; let height = element.attribute_opt("height")?; let id = element.attribute("id")?; let r#type = element.attribute("type")?; let url = element.attribute_opt("url")?; let width = element.attribute_opt("width")?; Ok(Self { bytes, height, id, r#type, url, width, }) } } impl IntoElement for Info { fn builder(&self) -> peanuts::element::ElementBuilder { Element::builder("info", Some(XMLNS)) .push_attribute("bytes", self.bytes) .push_attribute_opt("height", self.height) .push_attribute("id", self.id.clone()) .push_attribute("type", self.r#type.clone()) .push_attribute_opt("url", self.url.clone()) .push_attribute_opt("width", self.width) } } #[derive(Debug, Clone)] pub struct Pointer(pub PointerInner); impl FromElement for Pointer { fn from_element(mut element: Element) -> peanuts::element::DeserializeResult { element.check_name("pointer")?; element.check_namespace(XMLNS)?; Ok(Self(PointerInner::Unsupported(element.pop_child_one()?))) } } impl IntoElement for Pointer { fn builder(&self) -> peanuts::element::ElementBuilder { let _builder = Element::builder("pointer", Some(XMLNS)); match &self.0 { PointerInner::Unsupported(_element) => { panic!("cannot serialize unsupported PointerInner") } } } } #[derive(Debug, Clone)] pub enum PointerInner { Unsupported(Element), }