ruma_events/
marked_unread.rs
1use ruma_macros::EventContent;
6use serde::{Deserialize, Serialize};
7
8#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
14#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
15#[ruma_event(type = "m.marked_unread", kind = RoomAccountData)]
16pub struct MarkedUnreadEventContent {
17 pub unread: bool,
19}
20
21impl MarkedUnreadEventContent {
22 pub fn new(unread: bool) -> Self {
24 Self { unread }
25 }
26}
27
28#[cfg(feature = "unstable-msc2867")]
37#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
38#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
39#[ruma_event(type = "com.famedly.marked_unread", kind = RoomAccountData)]
40#[serde(transparent)]
41pub struct UnstableMarkedUnreadEventContent(pub MarkedUnreadEventContent);
42
43#[cfg(feature = "unstable-msc2867")]
44impl std::ops::Deref for UnstableMarkedUnreadEventContent {
45 type Target = MarkedUnreadEventContent;
46
47 fn deref(&self) -> &Self::Target {
48 &self.0
49 }
50}
51
52#[cfg(feature = "unstable-msc2867")]
53impl From<MarkedUnreadEventContent> for UnstableMarkedUnreadEventContent {
54 fn from(value: MarkedUnreadEventContent) -> Self {
55 Self(value)
56 }
57}
58
59#[cfg(feature = "unstable-msc2867")]
60impl From<UnstableMarkedUnreadEventContent> for MarkedUnreadEventContent {
61 fn from(value: UnstableMarkedUnreadEventContent) -> Self {
62 value.0
63 }
64}
65
66#[cfg(all(test, feature = "unstable-msc2867"))]
67mod tests {
68 use assert_matches2::assert_matches;
69 use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
70
71 use super::{MarkedUnreadEventContent, UnstableMarkedUnreadEventContent};
72 use crate::{AnyRoomAccountDataEvent, RoomAccountDataEvent};
73
74 #[test]
75 fn deserialize() {
76 let raw_unstable_marked_unread = json!({
77 "type": "com.famedly.marked_unread",
78 "content": {
79 "unread": true,
80 },
81 });
82 let unstable_marked_unread_account_data =
83 from_json_value::<AnyRoomAccountDataEvent>(raw_unstable_marked_unread).unwrap();
84 assert_matches!(
85 unstable_marked_unread_account_data,
86 AnyRoomAccountDataEvent::UnstableMarkedUnread(unstable_marked_unread)
87 );
88 assert!(unstable_marked_unread.content.unread);
89
90 let raw_marked_unread = json!({
91 "type": "m.marked_unread",
92 "content": {
93 "unread": true,
94 },
95 });
96 let marked_unread_account_data =
97 from_json_value::<AnyRoomAccountDataEvent>(raw_marked_unread).unwrap();
98 assert_matches!(
99 marked_unread_account_data,
100 AnyRoomAccountDataEvent::MarkedUnread(marked_unread)
101 );
102 assert!(marked_unread.content.unread);
103 }
104
105 #[test]
106 fn serialize() {
107 let marked_unread = MarkedUnreadEventContent::new(true);
108 let marked_unread_account_data = RoomAccountDataEvent { content: marked_unread.clone() };
109 assert_eq!(
110 to_json_value(marked_unread_account_data).unwrap(),
111 json!({
112 "type": "m.marked_unread",
113 "content": {
114 "unread": true,
115 },
116 })
117 );
118
119 let unstable_marked_unread = UnstableMarkedUnreadEventContent::from(marked_unread);
120 let unstable_marked_unread_account_data =
121 RoomAccountDataEvent { content: unstable_marked_unread };
122 assert_eq!(
123 to_json_value(unstable_marked_unread_account_data).unwrap(),
124 json!({
125 "type": "com.famedly.marked_unread",
126 "content": {
127 "unread": true,
128 },
129 })
130 );
131 }
132}