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 fn test_invalid_child() {
134 let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'><coucou/></set>"
135 .parse()
136 .unwrap();
137 let error = SetQuery::try_from(elem).unwrap_err();
138 let message = match error {
139 FromElementError::Invalid(Error::Other(string)) => string,
140 _ => panic!(),
141 };
142 assert_eq!(message, "Unknown child in SetQuery element.");
143
144 let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'><coucou/></set>"
145 .parse()
146 .unwrap();
147 let error = SetResult::try_from(elem).unwrap_err();
148 let message = match error {
149 FromElementError::Invalid(Error::Other(string)) => string,
150 _ => panic!(),
151 };
152 assert_eq!(message, "Unknown child in SetResult element.");
153 }
154
155 #[test]
156 fn test_serialise() {
157 let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'/>"
158 .parse()
159 .unwrap();
160 let rsm = SetQuery {
161 max: None,
162 after: None,
163 before: None,
164 index: None,
165 };
166 let elem2 = rsm.into();
167 assert_eq!(elem, elem2);
168
169 let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'/>"
170 .parse()
171 .unwrap();
172 let rsm = SetResult {
173 first: None,
174 last: None,
175 count: None,
176 };
177 let elem2 = rsm.into();
178 assert_eq!(elem, elem2);
179 }
180
181 #[test]
184 #[ignore]
185 fn test_serialise_empty_before() {
186 let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'><before/></set>"
187 .parse()
188 .unwrap();
189 let rsm = SetQuery {
190 max: None,
191 after: None,
192 before: Some("".into()),
193 index: None,
194 };
195 let elem2 = rsm.into();
196 assert_eq!(elem, elem2);
197 }
198
199 #[test]
200 fn test_first_index() {
201 let elem: Element =
202 "<set xmlns='http://jabber.org/protocol/rsm'><first index='4'>coucou</first></set>"
203 .parse()
204 .unwrap();
205 let elem1 = elem.clone();
206 let set = SetResult::try_from(elem).unwrap();
207 let first = set.first.unwrap();
208 assert_eq!(first.item, "coucou");
209 assert_eq!(first.index, Some(4));
210
211 let set2 = SetResult {
212 first: Some(First {
213 item: String::from("coucou"),
214 index: Some(4),
215 }),
216 last: None,
217 count: None,
218 };
219 let elem2 = set2.into();
220 assert_eq!(elem1, elem2);
221 }
222}