aboutsummaryrefslogtreecommitdiffstats
path: root/stanza/src/xep_0084/data.rs
blob: 2a37df4df7c1096928a474aa74d5c681dc3e7553 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use peanuts::{
    element::{FromElement, IntoElement},
    Element,
};

pub const XMLNS: &str = "urn:xmpp:avatar:data";

#[derive(Debug, Clone)]
pub struct Data(pub String);

impl FromElement for Data {
    fn from_element(mut element: peanuts::Element) -> peanuts::element::DeserializeResult<Self> {
        element.check_name("data")?;
        element.check_namespace(XMLNS)?;

        Ok(Self(element.pop_value_opt()?.unwrap_or_default()))
    }
}

impl IntoElement for Data {
    fn builder(&self) -> peanuts::element::ElementBuilder {
        let builder = Element::builder("data", Some(XMLNS));

        if self.0.is_empty() {
            builder
        } else {
            builder.push_text(self.0.clone())
        }
    }
}