xmpp_parsers/
reactions.rs1use xso::{AsXml, FromXml};
8
9use crate::message::MessagePayload;
10use crate::ns;
11
12#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
14#[xml(namespace = ns::REACTIONS, name = "reactions")]
15pub struct Reactions {
16 #[xml(attribute)]
18 pub id: String,
19
20 #[xml(child(n = ..))]
22 pub reactions: Vec<Reaction>,
23}
24
25impl MessagePayload for Reactions {}
26
27#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
29#[xml(namespace = ns::REACTIONS, name = "reaction")]
30pub struct Reaction {
31 #[xml(text)]
33 pub emoji: String,
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39 use minidom::Element;
40
41 #[cfg(target_pointer_width = "32")]
42 #[test]
43 fn test_size() {
44 assert_size!(Reactions, 24);
45 assert_size!(Reaction, 12);
46 }
47
48 #[cfg(target_pointer_width = "64")]
49 #[test]
50 fn test_size() {
51 assert_size!(Reactions, 48);
52 assert_size!(Reaction, 24);
53 }
54
55 #[test]
56 fn test_empty() {
57 let elem: Element = "<reactions xmlns='urn:xmpp:reactions:0' id='foo'/>"
58 .parse()
59 .unwrap();
60 let elem2 = elem.clone();
61 let reactions = Reactions::try_from(elem2).unwrap();
62 assert_eq!(reactions.id, "foo");
63 assert_eq!(reactions.reactions.len(), 0);
64 }
65
66 #[test]
67 fn test_multi() {
68 let elem: Element =
69 "<reactions xmlns='urn:xmpp:reactions:0' id='foo'><reaction>👋</reaction><reaction>🐢</reaction></reactions>"
70 .parse()
71 .unwrap();
72 let elem2 = elem.clone();
73 let reactions = Reactions::try_from(elem2).unwrap();
74 assert_eq!(reactions.id, "foo");
75 assert_eq!(reactions.reactions.len(), 2);
76 let [hand, turtle]: [Reaction; 2] = reactions.reactions.try_into().unwrap();
77 assert_eq!(hand.emoji, "👋");
78 assert_eq!(turtle.emoji, "🐢");
79 }
80
81 #[test]
82 fn test_zwj_emoji() {
83 let elem: Element =
84 "<reactions xmlns='urn:xmpp:reactions:0' id='foo'><reaction>👩🏾❤️👩🏼</reaction></reactions>"
85 .parse()
86 .unwrap();
87 let elem2 = elem.clone();
88 let mut reactions = Reactions::try_from(elem2).unwrap();
89 assert_eq!(reactions.id, "foo");
90 assert_eq!(reactions.reactions.len(), 1);
91 let reaction = reactions.reactions.pop().unwrap();
92 assert_eq!(
93 reaction.emoji,
94 "\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FC}"
95 );
96 }
97}