1use xso::{AsXml, FromXml};
8
9use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
10use crate::ns;
11use jid::Jid;
12
13#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
16#[xml(namespace = ns::BLOCKING, name = "blocklist")]
17pub struct BlocklistRequest;
18
19impl IqGetPayload for BlocklistRequest {}
20
21#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
24#[xml(namespace = ns::BLOCKING, name = "blocklist")]
25pub struct BlocklistResult {
26 #[xml(extract(n = .., name = "item", fields(attribute(name = "jid", type_ = Jid))))]
28 pub items: Vec<Jid>,
29}
30
31impl IqResultPayload for BlocklistResult {}
32
33#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
36#[xml(namespace = ns::BLOCKING, name = "block")]
37pub struct Block {
38 #[xml(extract(n = .., name = "item", fields(attribute(name = "jid", type_ = Jid))))]
40 pub items: Vec<Jid>,
41}
42
43impl IqSetPayload for Block {}
44
45#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
49#[xml(namespace = ns::BLOCKING, name = "unblock")]
50pub struct Unblock {
51 #[xml(extract(n = .., name = "item", fields(attribute(name = "jid", type_ = Jid))))]
53 pub items: Vec<Jid>,
54}
55
56impl IqSetPayload for Unblock {}
57
58#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
60#[xml(namespace = ns::BLOCKING_ERRORS, name = "blocked")]
61pub struct Blocked;
62
63#[cfg(test)]
64mod tests {
65 #[cfg(not(feature = "disable-validation"))]
66 use xso::error::{Error, FromElementError};
67
68 use super::*;
69 use minidom::Element;
70
71 #[cfg(target_pointer_width = "32")]
72 #[test]
73 fn test_size() {
74 assert_size!(BlocklistRequest, 0);
75 assert_size!(BlocklistResult, 12);
76 assert_size!(Block, 12);
77 assert_size!(Unblock, 12);
78 }
79
80 #[cfg(target_pointer_width = "64")]
81 #[test]
82 fn test_size() {
83 assert_size!(BlocklistRequest, 0);
84 assert_size!(BlocklistResult, 24);
85 assert_size!(Block, 24);
86 assert_size!(Unblock, 24);
87 }
88
89 #[test]
90 fn test_simple() {
91 let elem: Element = "<blocklist xmlns='urn:xmpp:blocking'/>".parse().unwrap();
92 let request_elem = elem.clone();
93 BlocklistRequest::try_from(request_elem).unwrap();
94
95 let result_elem = elem.clone();
96 let result = BlocklistResult::try_from(result_elem).unwrap();
97 assert!(result.items.is_empty());
98
99 let elem: Element = "<block xmlns='urn:xmpp:blocking'/>".parse().unwrap();
100 let block = Block::try_from(elem).unwrap();
101 assert!(block.items.is_empty());
102
103 let elem: Element = "<unblock xmlns='urn:xmpp:blocking'/>".parse().unwrap();
104 let unblock = Unblock::try_from(elem).unwrap();
105 assert!(unblock.items.is_empty());
106 }
107
108 #[test]
109 fn test_items() {
110 let elem: Element = "<blocklist xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></blocklist>".parse().unwrap();
111 let two_items = vec![
112 Jid::new("coucou@coucou").unwrap(),
113 Jid::new("domain").unwrap(),
114 ];
115
116 let result_elem = elem.clone();
117 let result = BlocklistResult::try_from(result_elem).unwrap();
118 assert_eq!(result.items, two_items);
119
120 let elem: Element = "<block xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></block>".parse().unwrap();
121 let block = Block::try_from(elem).unwrap();
122 assert_eq!(block.items, two_items);
123
124 let elem: Element = "<unblock xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></unblock>".parse().unwrap();
125 let unblock = Unblock::try_from(elem).unwrap();
126 assert_eq!(unblock.items, two_items);
127 }
128
129 #[cfg(not(feature = "disable-validation"))]
130 #[test]
131 fn test_invalid() {
132 let elem: Element = "<blocklist xmlns='urn:xmpp:blocking' coucou=''/>"
133 .parse()
134 .unwrap();
135 let request_elem = elem.clone();
136 let error = BlocklistRequest::try_from(request_elem).unwrap_err();
137 let message = match error {
138 FromElementError::Invalid(Error::Other(string)) => string,
139 _ => panic!(),
140 };
141 assert_eq!(message, "Unknown attribute in BlocklistRequest element.");
142
143 let result_elem = elem.clone();
144 let error = BlocklistResult::try_from(result_elem).unwrap_err();
145 let message = match error {
146 FromElementError::Invalid(Error::Other(string)) => string,
147 _ => panic!(),
148 };
149 assert_eq!(message, "Unknown attribute in BlocklistResult element.");
150
151 let elem: Element = "<block xmlns='urn:xmpp:blocking' coucou=''/>"
152 .parse()
153 .unwrap();
154 let error = Block::try_from(elem).unwrap_err();
155 let message = match error {
156 FromElementError::Invalid(Error::Other(string)) => string,
157 _ => panic!(),
158 };
159 assert_eq!(message, "Unknown attribute in Block element.");
160
161 let elem: Element = "<unblock xmlns='urn:xmpp:blocking' coucou=''/>"
162 .parse()
163 .unwrap();
164 let error = Unblock::try_from(elem).unwrap_err();
165 let message = match error {
166 FromElementError::Invalid(Error::Other(string)) => string,
167 _ => panic!(),
168 };
169 assert_eq!(message, "Unknown attribute in Unblock element.");
170 }
171
172 #[cfg(not(feature = "disable-validation"))]
173 #[test]
174 fn test_non_empty_blocklist_request() {
175 let elem: Element = "<blocklist xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></blocklist>".parse().unwrap();
176 let error = BlocklistRequest::try_from(elem).unwrap_err();
177 let message = match error {
178 FromElementError::Invalid(Error::Other(string)) => string,
179 _ => panic!(),
180 };
181 assert_eq!(message, "Unknown child in BlocklistRequest element.");
182 }
183}