receipts.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
 7generate_empty_element!(
 8    /// Requests that this message is acked by the final recipient once
 9    /// received.
10    Request, "request", RECEIPTS
11);
12
13generate_element!(
14    /// Notes that a previous message has correctly been received, it is
15    /// referenced by its 'id' attribute.
16    Received, "received", RECEIPTS,
17    attributes: [
18        /// The 'id' attribute of the received message.
19        id: Option<String> = "id" => optional,
20    ]
21);
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26    use try_from::TryFrom;
27    use minidom::Element;
28    use ns;
29
30    #[test]
31    fn test_simple() {
32        let elem: Element = "<request xmlns='urn:xmpp:receipts'/>".parse().unwrap();
33        Request::try_from(elem).unwrap();
34
35        let elem: Element = "<received xmlns='urn:xmpp:receipts'/>".parse().unwrap();
36        Received::try_from(elem).unwrap();
37
38        let elem: Element = "<received xmlns='urn:xmpp:receipts' id='coucou'/>".parse().unwrap();
39        Received::try_from(elem).unwrap();
40    }
41
42    #[test]
43    fn test_serialise() {
44        let receipt = Request;
45        let elem: Element = receipt.into();
46        assert!(elem.is("request", ns::RECEIPTS));
47        assert_eq!(elem.attrs().count(), 0);
48
49        let receipt = Received {
50            id: Some(String::from("coucou")),
51        };
52        let elem: Element = receipt.into();
53        assert!(elem.is("received", ns::RECEIPTS));
54        assert_eq!(elem.attr("id"), Some("coucou"));
55    }
56}