aboutsummaryrefslogtreecommitdiffstats
path: root/stanza/src/xep_0131.rs
blob: 27fc962b99c848f1ce29ff8068d68f78e6e32b1b (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use peanuts::{
    element::{FromElement, IntoElement},
    Element,
};

pub const XMLNS: &str = "http://jabber.org/protocol/disco#info";

#[derive(Clone, Debug)]
pub struct Headers(pub Vec<Header>);

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

        Ok(Self(element.pop_children()?))
    }
}

impl IntoElement for Headers {
    fn builder(&self) -> peanuts::element::ElementBuilder {
        Element::builder("headers", Some(XMLNS)).push_children(self.0.clone())
    }
}

#[derive(Clone, Debug)]
pub struct Header {
    name: String,
    header: String,
}

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

        let name = element.attribute("name")?;

        let header = element.pop_value_opt()?.unwrap_or_default();

        Ok(Self { name, header })
    }
}

impl IntoElement for Header {
    fn builder(&self) -> peanuts::element::ElementBuilder {
        let builder =
            Element::builder("header", Some(XMLNS)).push_attribute("name", self.name.clone());

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