xmpp_parsers/
mam_prefs.rs1use xso::{AsXml, FromXml};
8
9use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
10use crate::ns;
11use jid::Jid;
12
13generate_attribute!(
14 DefaultPrefs, "default", {
16 Always => "always",
18
19 Never => "never",
21
22 Roster => "roster",
25 }
26);
27
28#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
30#[xml(namespace = ns::MAM, name = "prefs")]
31pub struct Prefs {
32 #[xml(attribute = "default")]
35 pub default_: DefaultPrefs,
36
37 #[xml(extract(default, fields(extract(n = .., name = "jid", fields(text(type_ = Jid))))))]
39 pub always: Vec<Jid>,
40
41 #[xml(extract(default, fields(extract(n = .., name = "jid", fields(text(type_ = Jid))))))]
43 pub never: Vec<Jid>,
44}
45
46impl IqGetPayload for Prefs {}
47impl IqSetPayload for Prefs {}
48impl IqResultPayload for Prefs {}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53 use jid::BareJid;
54 use minidom::Element;
55
56 #[cfg(target_pointer_width = "32")]
57 #[test]
58 fn test_size() {
59 assert_size!(DefaultPrefs, 1);
60 assert_size!(Prefs, 28);
61 }
62
63 #[cfg(target_pointer_width = "64")]
64 #[test]
65 fn test_size() {
66 assert_size!(DefaultPrefs, 1);
67 assert_size!(Prefs, 56);
68 }
69
70 #[test]
71 fn test_prefs_get() {
72 let elem: Element = "<prefs xmlns='urn:xmpp:mam:2' default='always'/>"
73 .parse()
74 .unwrap();
75 let prefs = Prefs::try_from(elem).unwrap();
76 assert!(prefs.always.is_empty());
77 assert!(prefs.never.is_empty());
78
79 let elem: Element = r#"<prefs xmlns='urn:xmpp:mam:2' default='roster'>
80 <always/>
81 <never/>
82</prefs>
83"#
84 .parse()
85 .unwrap();
86 let prefs = Prefs::try_from(elem).unwrap();
87 assert!(prefs.always.is_empty());
88 assert!(prefs.never.is_empty());
89 }
90
91 #[test]
92 fn test_prefs_result() {
93 let elem: Element = r#"<prefs xmlns='urn:xmpp:mam:2' default='roster'>
94 <always>
95 <jid>romeo@montague.lit</jid>
96 </always>
97 <never>
98 <jid>montague@montague.lit</jid>
99 </never>
100</prefs>
101"#
102 .parse()
103 .unwrap();
104 let prefs = Prefs::try_from(elem).unwrap();
105 assert_eq!(prefs.always, [BareJid::new("romeo@montague.lit").unwrap()]);
106 assert_eq!(
107 prefs.never,
108 [BareJid::new("montague@montague.lit").unwrap()]
109 );
110
111 let elem2 = Element::from(prefs.clone());
112 println!("{:?}", elem2);
113 let prefs2 = Prefs::try_from(elem2).unwrap();
114 assert_eq!(prefs.default_, prefs2.default_);
115 assert_eq!(prefs.always, prefs2.always);
116 assert_eq!(prefs.never, prefs2.never);
117 }
118}