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