aboutsummaryrefslogtreecommitdiffstats
path: root/stanza/src/xep_0059.rs
blob: 01dbc6c21d7495a7fcb78ad200340b68c628606f (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use peanuts::{
    element::{FromElement, IntoElement},
    Element,
};

pub const XMLNS: &str = "http://jabber.org/protocol/rsm";

#[derive(Debug, Clone)]
pub struct Set {
    after: Option<After>,
    before: Option<Before>,
    count: Option<Count>,
    first: Option<First>,
    index: Option<Index>,
    last: Option<Last>,
    max: Option<Max>,
}

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

        let after = element.pop_child_opt()?;
        let before = element.pop_child_opt()?;
        let count = element.pop_child_opt()?;
        let first = element.pop_child_opt()?;
        let index = element.pop_child_opt()?;
        let last = element.pop_child_opt()?;
        let max = element.pop_child_opt()?;

        Ok(Self {
            after,
            before,
            count,
            first,
            index,
            last,
            max,
        })
    }
}

impl IntoElement for Set {
    fn builder(&self) -> peanuts::element::ElementBuilder {
        Element::builder("set", Some(XMLNS))
            .push_child_opt(self.after.clone())
            .push_child_opt(self.before.clone())
            .push_child_opt(self.count.clone())
            .push_child_opt(self.first.clone())
            .push_child_opt(self.index.clone())
            .push_child_opt(self.last.clone())
            .push_child_opt(self.max.clone())
    }
}

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

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

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

impl IntoElement for After {
    fn builder(&self) -> peanuts::element::ElementBuilder {
        // TODO: better way for push_text to work, empty string should be empty element no matter what
        let builder = Element::builder("after", Some(XMLNS));

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

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

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

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

impl IntoElement for Before {
    fn builder(&self) -> peanuts::element::ElementBuilder {
        // TODO: better way for push_text to work, empty string should be empty element no matter what
        let builder = Element::builder("before", Some(XMLNS));

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

#[derive(Debug, Clone)]
pub struct Count(pub i32);

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

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

impl IntoElement for Count {
    fn builder(&self) -> peanuts::element::ElementBuilder {
        Element::builder("count", Some(XMLNS)).push_text(self.0)
    }
}

#[derive(Debug, Clone)]
pub struct Index(pub i32);

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

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

impl IntoElement for Index {
    fn builder(&self) -> peanuts::element::ElementBuilder {
        Element::builder("index", Some(XMLNS)).push_text(self.0)
    }
}

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

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

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

impl IntoElement for Last {
    fn builder(&self) -> peanuts::element::ElementBuilder {
        // TODO: better way for push_text to work, empty string should be empty element no matter what
        let builder = Element::builder("last", Some(XMLNS));

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

#[derive(Debug, Clone)]
pub struct Max(pub i32);

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

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

impl IntoElement for Max {
    fn builder(&self) -> peanuts::element::ElementBuilder {
        Element::builder("max", Some(XMLNS)).push_text(self.0)
    }
}

#[derive(Debug, Clone)]
pub struct First {
    index: Option<i32>,
    first: String,
}

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

        let index = element.attribute_opt("index")?;

        let first = element.value_opt()?.unwrap_or_default();

        Ok(Self { index, first })
    }
}

impl IntoElement for First {
    fn builder(&self) -> peanuts::element::ElementBuilder {
        let builder =
            Element::builder("first", Some(XMLNS)).push_attribute_opt("index", self.index);

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