aboutsummaryrefslogtreecommitdiffstats
path: root/stanza/src/xep_0084/metadata.rs
blob: c6a3fb4d20fd4835f5d128b718acb35757d019e2 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use peanuts::{
    element::{FromElement, IntoElement},
    Element,
};

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

#[derive(Debug, Clone)]
pub struct Metadata {
    pub info: Vec<Info>,
    pub pointers: Vec<Pointer>,
}

impl FromElement for Metadata {
    fn from_element(mut element: Element) -> peanuts::element::DeserializeResult<Self> {
        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<u16>,
    pub id: String,
    pub r#type: String,
    pub url: Option<String>,
    pub width: Option<u16>,
}

impl FromElement for Info {
    fn from_element(mut element: Element) -> peanuts::element::DeserializeResult<Self> {
        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<Self> {
        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),
}