1use xso::{AsXml, FromXml};
8
9use crate::message::MessagePayload;
10use crate::ns;
11
12#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
15#[xml(namespace = ns::RECEIPTS, name = "request")]
16pub struct Request;
17
18impl MessagePayload for Request {}
19
20#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
23#[xml(namespace = ns::RECEIPTS, name = "received")]
24pub struct Received {
25 #[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
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 FromElementError::Invalid(Error::Other(string)) => string,
70 _ => panic!(),
71 };
72 assert_eq!(
73 message,
74 "Required attribute field 'id' on Received element missing."
75 );
76 }
77
78 #[test]
79 fn test_serialise() {
80 let receipt = Request;
81 let elem: Element = receipt.into();
82 assert!(elem.is("request", ns::RECEIPTS));
83 assert_eq!(elem.attrs().count(), 0);
84
85 let receipt = Received {
86 id: String::from("coucou"),
87 };
88 let elem: Element = receipt.into();
89 assert!(elem.is("received", ns::RECEIPTS));
90 assert_eq!(elem.attr("id"), Some("coucou"));
91 }
92}