aboutsummaryrefslogtreecommitdiffstats
path: root/stanza/src/xep_0172.rs
blob: 1c242009f4344af0f3bff844a6ff9ebcd63d0c98 (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 = "http://jabber.org/protocol/nick";

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

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

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

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

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