1use xso::{AsXml, FromXml};
8
9use crate::ns;
10
11#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
14#[xml(namespace = ns::RSM, name = "set")]
15pub struct SetQuery {
16 #[xml(extract(default, fields(text(type_ = usize))))]
18 pub max: Option<usize>,
19
20 #[xml(extract(default, fields(text(type_ = String))))]
23 pub after: Option<String>,
24
25 #[xml(extract(default, fields(text(type_ = String))))]
28 pub before: Option<String>,
29
30 #[xml(extract(default, fields(text(type_ = usize))))]
32 pub index: Option<usize>,
33}
34
35#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
37#[xml(namespace = ns::RSM, name = "first")]
38pub struct First {
39 #[xml(attribute(default))]
42 pub index: Option<usize>,
43
44 #[xml(text)]
46 pub item: String,
47}
48
49#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
51#[xml(namespace = ns::RSM, name = "set")]
52pub struct SetResult {
53 #[xml(child(default))]
55 pub first: Option<First>,
56
57 #[xml(extract(default, fields(text(type_ = String))))]
59 pub last: Option<String>,
60
61 #[xml(extract(default, fields(text(type_ = usize))))]
63 pub count: Option<usize>,
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69 use minidom::Element;
70 use xso::error::{Error, FromElementError};
71
72 #[cfg(target_pointer_width = "32")]
73 #[test]
74 fn test_size() {
75 assert_size!(SetQuery, 40);
76 assert_size!(SetResult, 40);
77 }
78
79 #[cfg(target_pointer_width = "64")]
80 #[test]
81 fn test_size() {
82 assert_size!(SetQuery, 80);
83 assert_size!(SetResult, 80);
84 }
85
86 #[test]
87 fn test_simple() {
88 let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'/>"
89 .parse()
90 .unwrap();
91 let set = SetQuery::try_from(elem).unwrap();
92 assert_eq!(set.max, None);
93 assert_eq!(set.after, None);
94 assert_eq!(set.before, None);
95 assert_eq!(set.index, None);
96
97 let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'/>"
98 .parse()
99 .unwrap();
100 let set = SetResult::try_from(elem).unwrap();
101 match set.first {
102 Some(_) => panic!(),
103 None => (),
104 }
105 assert_eq!(set.last, None);
106 assert_eq!(set.count, None);
107 }
108
109 #[test]
110 fn test_unknown() {
111 let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>"
112 .parse()
113 .unwrap();
114 let error = SetQuery::try_from(elem.clone()).unwrap_err();
115 let returned_elem = match error {
116 FromElementError::Mismatch(elem) => elem,
117 _ => panic!(),
118 };
119 assert_eq!(elem, returned_elem);
120
121 let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>"
122 .parse()
123 .unwrap();
124 let error = SetResult::try_from(elem.clone()).unwrap_err();
125 let returned_elem = match error {
126 FromElementError::Mismatch(elem) => elem,
127 _ => panic!(),
128 };
129 assert_eq!(elem, returned_elem);
130 }
131
132 #[test]
133 #[cfg_attr(feature = "disable-validation", should_panic = "Result::unwrap_err")]
134 fn test_invalid_child() {
135 let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'><coucou/></set>"
136 .parse()
137 .unwrap();
138 let error = SetQuery::try_from(elem).unwrap_err();
139 let message = match error {
140 FromElementError::Invalid(Error::Other(string)) => string,
141 _ => panic!(),
142 };
143 assert_eq!(message, "Unknown child in SetQuery element.");
144
145 let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'><coucou/></set>"
146 .parse()
147 .unwrap();
148 let error = SetResult::try_from(elem).unwrap_err();
149 let message = match error {
150 FromElementError::Invalid(Error::Other(string)) => string,
151 _ => panic!(),
152 };
153 assert_eq!(message, "Unknown child in SetResult element.");
154 }
155
156 #[test]
157 fn test_serialise() {
158 let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'/>"
159 .parse()
160 .unwrap();
161 let rsm = SetQuery {
162 max: None,
163 after: None,
164 before: None,
165 index: None,
166 };
167 let elem2 = rsm.into();
168 assert_eq!(elem, elem2);
169
170 let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'/>"
171 .parse()
172 .unwrap();
173 let rsm = SetResult {
174 first: None,
175 last: None,
176 count: None,
177 };
178 let elem2 = rsm.into();
179 assert_eq!(elem, elem2);
180 }
181
182 #[test]
185 #[ignore]
186 fn test_serialise_empty_before() {
187 let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'><before/></set>"
188 .parse()
189 .unwrap();
190 let rsm = SetQuery {
191 max: None,
192 after: None,
193 before: Some("".into()),
194 index: None,
195 };
196 let elem2 = rsm.into();
197 assert_eq!(elem, elem2);
198 }
199
200 #[test]
201 fn test_first_index() {
202 let elem: Element =
203 "<set xmlns='http://jabber.org/protocol/rsm'><first index='4'>coucou</first></set>"
204 .parse()
205 .unwrap();
206 let elem1 = elem.clone();
207 let set = SetResult::try_from(elem).unwrap();
208 let first = set.first.unwrap();
209 assert_eq!(first.item, "coucou");
210 assert_eq!(first.index, Some(4));
211
212 let set2 = SetResult {
213 first: Some(First {
214 item: String::from("coucou"),
215 index: Some(4),
216 }),
217 last: None,
218 count: None,
219 };
220 let elem2 = set2.into();
221 assert_eq!(elem1, elem2);
222 }
223}