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