ruma_common/push/condition/
push_condition_serde.rs
1use serde::{de, Deserialize, Serialize, Serializer};
2use serde_json::value::RawValue as RawJsonValue;
3
4#[cfg(feature = "unstable-msc3931")]
5use super::RoomVersionFeature;
6use super::{PushCondition, RoomMemberCountIs, ScalarJsonValue};
7use crate::serde::from_raw_json_value;
8
9impl Serialize for PushCondition {
10 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
11 where
12 S: Serializer,
13 {
14 match self {
15 PushCondition::_Custom(custom) => custom.serialize(serializer),
16 _ => PushConditionSerDeHelper::from(self.clone()).serialize(serializer),
17 }
18 }
19}
20
21impl<'de> Deserialize<'de> for PushCondition {
22 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
23 where
24 D: de::Deserializer<'de>,
25 {
26 let json = Box::<RawJsonValue>::deserialize(deserializer)?;
27 let ExtractKind { kind } = from_raw_json_value(&json)?;
28
29 match kind.as_ref() {
30 "event_match"
31 | "contains_display_name"
32 | "room_member_count"
33 | "sender_notification_permission"
34 | "event_property_is"
35 | "event_property_contains" => {
36 let helper: PushConditionSerDeHelper = from_raw_json_value(&json)?;
37 Ok(helper.into())
38 }
39 #[cfg(feature = "unstable-msc3931")]
40 "org.matrix.msc3931.room_version_supports" => {
41 let helper: PushConditionSerDeHelper = from_raw_json_value(&json)?;
42 Ok(helper.into())
43 }
44 _ => from_raw_json_value(&json).map(Self::_Custom),
45 }
46 }
47}
48
49#[derive(Deserialize)]
50struct ExtractKind {
51 kind: String,
52}
53
54#[derive(Serialize, Deserialize)]
55#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
56#[serde(tag = "kind", rename_all = "snake_case")]
57enum PushConditionSerDeHelper {
58 EventMatch {
60 key: String,
62
63 pattern: String,
68 },
69
70 ContainsDisplayName,
73
74 RoomMemberCount {
76 is: RoomMemberCountIs,
78 },
79
80 SenderNotificationPermission {
83 key: String,
88 },
89
90 #[cfg(feature = "unstable-msc3931")]
92 #[serde(rename = "org.matrix.msc3931.room_version_supports")]
93 RoomVersionSupports {
94 feature: RoomVersionFeature,
96 },
97
98 EventPropertyIs {
99 key: String,
100 value: ScalarJsonValue,
101 },
102
103 EventPropertyContains {
104 key: String,
105 value: ScalarJsonValue,
106 },
107}
108
109impl From<PushConditionSerDeHelper> for PushCondition {
110 fn from(value: PushConditionSerDeHelper) -> Self {
111 match value {
112 PushConditionSerDeHelper::EventMatch { key, pattern } => {
113 Self::EventMatch { key, pattern }
114 }
115 PushConditionSerDeHelper::ContainsDisplayName => Self::ContainsDisplayName,
116 PushConditionSerDeHelper::RoomMemberCount { is } => Self::RoomMemberCount { is },
117 PushConditionSerDeHelper::SenderNotificationPermission { key } => {
118 Self::SenderNotificationPermission { key }
119 }
120 #[cfg(feature = "unstable-msc3931")]
121 PushConditionSerDeHelper::RoomVersionSupports { feature } => {
122 Self::RoomVersionSupports { feature }
123 }
124 PushConditionSerDeHelper::EventPropertyIs { key, value } => {
125 Self::EventPropertyIs { key, value }
126 }
127 PushConditionSerDeHelper::EventPropertyContains { key, value } => {
128 Self::EventPropertyContains { key, value }
129 }
130 }
131 }
132}
133
134impl From<PushCondition> for PushConditionSerDeHelper {
135 fn from(value: PushCondition) -> Self {
136 match value {
137 PushCondition::EventMatch { key, pattern } => Self::EventMatch { key, pattern },
138 PushCondition::ContainsDisplayName => Self::ContainsDisplayName,
139 PushCondition::RoomMemberCount { is } => Self::RoomMemberCount { is },
140 PushCondition::SenderNotificationPermission { key } => {
141 Self::SenderNotificationPermission { key }
142 }
143 #[cfg(feature = "unstable-msc3931")]
144 PushCondition::RoomVersionSupports { feature } => Self::RoomVersionSupports { feature },
145 PushCondition::EventPropertyIs { key, value } => Self::EventPropertyIs { key, value },
146 PushCondition::EventPropertyContains { key, value } => {
147 Self::EventPropertyContains { key, value }
148 }
149 PushCondition::_Custom(_) => unimplemented!(),
150 }
151 }
152}