xmpp_parsers/
json_containers.rs1use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
8use crate::message::MessagePayload;
9use crate::ns;
10use crate::presence::PresencePayload;
11use crate::pubsub::PubSubPayload;
12
13use xso::{text::EmptyAsError, AsXml, FromXml};
14
15#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
17#[xml(namespace = ns::JSON_CONTAINERS, name = "json")]
18pub struct JsonContainer(#[xml(text = EmptyAsError)] String);
19
20impl MessagePayload for JsonContainer {}
21impl PresencePayload for JsonContainer {}
22impl IqGetPayload for JsonContainer {}
23impl IqSetPayload for JsonContainer {}
24impl IqResultPayload for JsonContainer {}
25impl PubSubPayload for JsonContainer {}
26
27#[cfg(test)]
28mod tests {
29 use super::*;
30 use minidom::Element;
31
32 use crate::Error::Other;
33 use xso::error::FromElementError;
34
35 #[cfg(target_pointer_width = "32")]
36 #[test]
37 fn test_size() {
38 assert_size!(JsonContainer, 24);
39 }
40
41 #[cfg(target_pointer_width = "64")]
42 #[test]
43 fn test_size() {
44 assert_size!(JsonContainer, 24);
45 }
46
47 #[test]
48 fn test_empty() {
49 let elem: Element = "<json xmlns='urn:xmpp:json:0'/>".parse().unwrap();
50 let error = JsonContainer::try_from(elem.clone()).unwrap_err();
51 match error {
52 FromElementError::Invalid(Other(err)) => assert_eq!(err, "Empty text node."),
53 _ => panic!(),
54 }
55 }
56
57 #[test]
58 fn test_simple() {
59 let elem: Element = "<json xmlns='urn:xmpp:json:0'>{'a': 1}</json>"
60 .parse()
61 .unwrap();
62 match JsonContainer::try_from(elem.clone()) {
63 Ok(json) => assert_eq!(json.0, String::from("{'a': 1}")),
64 _ => panic!(),
65 };
66 }
67}