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
 7use crate::message::MessagePayload;
 8
 9generate_empty_element!(
10    /// Requests that this message is acked by the final recipient once
11    /// received.
12    Request,
13    "request",
14    RECEIPTS
15);
16
17impl MessagePayload for Request {}
18
19generate_element!(
20    /// Notes that a previous message has correctly been received, it is
21    /// referenced by its 'id' attribute.
22    Received, "received", RECEIPTS,
23    attributes: [
24        /// The 'id' attribute of the received message.
25        id: Required<String> = "id",
26    ]
27);
28
29impl MessagePayload for Received {}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34    use crate::ns;
35    use crate::util::error::Error;
36    use crate::Element;
37    use std::convert::TryFrom;
38
39    #[cfg(target_pointer_width = "32")]
40    #[test]
41    fn test_size() {
42        assert_size!(Request, 0);
43        assert_size!(Received, 12);
44    }
45
46    #[cfg(target_pointer_width = "64")]
47    #[test]
48    fn test_size() {
49        assert_size!(Request, 0);
50        assert_size!(Received, 24);
51    }
52
53    #[test]
54    fn test_simple() {
55        let elem: Element = "<request xmlns='urn:xmpp:receipts'/>".parse().unwrap();
56        Request::try_from(elem).unwrap();
57
58        let elem: Element = "<received xmlns='urn:xmpp:receipts' id='coucou'/>"
59            .parse()
60            .unwrap();
61        Received::try_from(elem).unwrap();
62    }
63
64    #[test]
65    fn test_missing_id() {
66        let elem: Element = "<received xmlns='urn:xmpp:receipts'/>".parse().unwrap();
67        let error = Received::try_from(elem).unwrap_err();
68        let message = match error {
69            Error::ParseError(string) => string,
70            _ => panic!(),
71        };
72        assert_eq!(message, "Required attribute 'id' missing.");
73    }
74
75    #[test]
76    fn test_serialise() {
77        let receipt = Request;
78        let elem: Element = receipt.into();
79        assert!(elem.is("request", ns::RECEIPTS));
80        assert_eq!(elem.attrs().count(), 0);
81
82        let receipt = Received {
83            id: String::from("coucou"),
84        };
85        let elem: Element = receipt.into();
86        assert!(elem.is("received", ns::RECEIPTS));
87        assert_eq!(elem.attr("id"), Some("coucou"));
88    }
89}