rsm.rs

  1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
  2//
  3// This Source Code Form is subject to the terms of the Mozilla Public
  4// License, v. 2.0. If a copy of the MPL was not distributed with this
  5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6
  7use xso::{AsXml, FromXml};
  8
  9use crate::ns;
 10
 11/// Requests paging through a potentially big set of items (represented by an
 12/// UID).
 13#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
 14#[xml(namespace = ns::RSM, name = "set")]
 15pub struct SetQuery {
 16    /// Limit the number of items, or use the recipient’s defaults if None.
 17    #[xml(extract(default, fields(text(type_ = usize))))]
 18    pub max: Option<usize>,
 19
 20    /// The UID after which to give results, or if None it is the element
 21    /// “before” the first item, effectively an index of negative one.
 22    #[xml(extract(default, fields(text(type_ = String))))]
 23    pub after: Option<String>,
 24
 25    /// The UID before which to give results, or if None it starts with the
 26    /// last page of the full set.
 27    #[xml(extract(default, fields(text(type_ = String))))]
 28    pub before: Option<String>,
 29
 30    /// Numerical index of the page (deprecated).
 31    #[xml(extract(default, fields(text(type_ = usize))))]
 32    pub index: Option<usize>,
 33}
 34
 35/// The first item of the page.
 36#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
 37#[xml(namespace = ns::RSM, name = "first")]
 38pub struct First {
 39    /// The position of the [first item](#structfield.item) in the full set
 40    /// (which may be approximate).
 41    #[xml(attribute(default))]
 42    pub index: Option<usize>,
 43
 44    /// The UID of the first item of the page.
 45    #[xml(text)]
 46    pub item: String,
 47}
 48
 49/// Describes the paging result of a [query](struct.SetQuery.html).
 50#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
 51#[xml(namespace = ns::RSM, name = "set")]
 52pub struct SetResult {
 53    /// The first item of the page.
 54    #[xml(child(default))]
 55    pub first: Option<First>,
 56
 57    /// The UID of the last item of the page.
 58    #[xml(extract(default, fields(text(type_ = String))))]
 59    pub last: Option<String>,
 60
 61    /// How many items there are in the full set (which may be approximate).
 62    #[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    // TODO: This test is only ignored because <before/> and <before></before> aren’t equal in
182    // minidom, let’s fix that instead!
183    #[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}