matrix_sdk_base/
recent_emojis.rs1use ruma::{UInt, events::macros::EventContent};
7use serde::{Deserialize, Serialize};
8
9#[cfg(feature = "experimental-element-recent-emojis")]
11#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)]
12#[ruma_event(type = "io.element.recent_emoji", kind = GlobalAccountData)]
13pub struct RecentEmojisContent {
14 pub recent_emoji: Vec<(String, UInt)>,
19}
20
21#[cfg(feature = "experimental-element-recent-emojis")]
22impl RecentEmojisContent {
23 pub fn new(recent_emoji: Vec<(String, UInt)>) -> Self {
26 Self { recent_emoji }
27 }
28}
29
30#[cfg(feature = "experimental-element-recent-emojis")]
31#[cfg(test)]
32mod tests {
33 use ruma::uint;
34 use serde_json::{from_value, json, to_value};
35
36 use crate::recent_emojis::RecentEmojisContent;
37
38 #[test]
39 fn serialization() {
40 let content = RecentEmojisContent::new(vec![
41 ("😁".to_owned(), uint!(2)),
42 ("🎉".to_owned(), uint!(10)),
43 ]);
44 let json = to_value(&content).expect("recent emoji serialization failed");
45 let expected = json!({
46 "recent_emoji": [
47 ["😁", 2],
48 ["🎉", 10],
49 ]
50 });
51
52 assert_eq!(json, expected);
53 }
54
55 #[test]
56 fn deserialization() {
57 let json = json!({
58 "recent_emoji": [
59 ["😁", 2],
60 ["🎉", 10],
61 ]
62 });
63 let content =
64 from_value::<RecentEmojisContent>(json).expect("recent emoji deserialization failed");
65 let expected = RecentEmojisContent::new(vec![
66 ("😁".to_owned(), uint!(2)),
67 ("🎉".to_owned(), uint!(10)),
68 ]);
69
70 assert_eq!(content.recent_emoji, expected.recent_emoji);
71 }
72}