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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use std::fmt;
use crate::{
serde::{Incoming, StringEnum},
OwnedRoomAliasId, OwnedRoomId,
};
use js_int::UInt;
use serde::{
de::{Error, MapAccess, Visitor},
ser::SerializeStruct,
Deserialize, Deserializer, Serialize, Serializer,
};
use serde_json::Value as JsonValue;
use crate::{OwnedMxcUri, PrivOwnedStr, RoomName};
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct PublicRoomsChunk {
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(
feature = "compat",
serde(default, deserialize_with = "crate::serde::empty_string_as_none")
)]
pub canonical_alias: Option<OwnedRoomAliasId>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<Box<RoomName>>,
pub num_joined_members: UInt,
pub room_id: OwnedRoomId,
#[serde(skip_serializing_if = "Option::is_none")]
pub topic: Option<String>,
pub world_readable: bool,
pub guest_can_join: bool,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(
feature = "compat",
serde(default, deserialize_with = "crate::serde::empty_string_as_none")
)]
pub avatar_url: Option<OwnedMxcUri>,
#[serde(default, skip_serializing_if = "crate::serde::is_default")]
pub join_rule: PublicRoomJoinRule,
}
#[derive(Debug)]
#[allow(clippy::exhaustive_structs)]
pub struct PublicRoomsChunkInit {
pub num_joined_members: UInt,
pub room_id: OwnedRoomId,
pub world_readable: bool,
pub guest_can_join: bool,
}
impl From<PublicRoomsChunkInit> for PublicRoomsChunk {
fn from(init: PublicRoomsChunkInit) -> Self {
let PublicRoomsChunkInit { num_joined_members, room_id, world_readable, guest_can_join } =
init;
Self {
canonical_alias: None,
name: None,
num_joined_members,
room_id,
topic: None,
world_readable,
guest_can_join,
avatar_url: None,
join_rule: PublicRoomJoinRule::default(),
}
}
}
#[derive(Clone, Debug, Default, Incoming, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[incoming_derive(Default)]
pub struct Filter<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub generic_search_term: Option<&'a str>,
}
impl Filter<'_> {
pub fn new() -> Self {
Default::default()
}
pub fn is_empty(&self) -> bool {
self.generic_search_term.is_none()
}
}
#[derive(Clone, Debug, PartialEq, Eq, Incoming)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[incoming_derive(Clone, PartialEq, Eq, !Deserialize)]
pub enum RoomNetwork<'a> {
Matrix,
All,
ThirdParty(&'a str),
}
impl<'a> Default for RoomNetwork<'a> {
fn default() -> Self {
RoomNetwork::Matrix
}
}
impl<'a> Serialize for RoomNetwork<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state;
match self {
Self::Matrix => {
state = serializer.serialize_struct("RoomNetwork", 0)?;
}
Self::All => {
state = serializer.serialize_struct("RoomNetwork", 1)?;
state.serialize_field("include_all_networks", &true)?;
}
Self::ThirdParty(network) => {
state = serializer.serialize_struct("RoomNetwork", 1)?;
state.serialize_field("third_party_instance_id", network)?;
}
}
state.end()
}
}
impl<'de> Deserialize<'de> for IncomingRoomNetwork {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_map(RoomNetworkVisitor)
}
}
struct RoomNetworkVisitor;
impl<'de> Visitor<'de> for RoomNetworkVisitor {
type Value = IncomingRoomNetwork;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("Network selection")
}
fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
let mut include_all_networks = false;
let mut third_party_instance_id = None;
while let Some((key, value)) = access.next_entry::<String, JsonValue>()? {
match key.as_str() {
"include_all_networks" => {
include_all_networks = match value.as_bool() {
Some(b) => b,
_ => false,
}
}
"third_party_instance_id" => {
third_party_instance_id = value.as_str().map(|v| v.to_owned())
}
_ => {}
};
}
if include_all_networks {
if third_party_instance_id.is_none() {
Ok(IncomingRoomNetwork::All)
} else {
Err(M::Error::custom(
"`include_all_networks = true` and `third_party_instance_id` are mutually exclusive.",
))
}
} else {
Ok(match third_party_instance_id {
Some(network) => IncomingRoomNetwork::ThirdParty(network),
None => IncomingRoomNetwork::Matrix,
})
}
}
}
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
#[derive(Clone, Debug, PartialEq, Eq, StringEnum)]
#[ruma_enum(rename_all = "snake_case")]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub enum PublicRoomJoinRule {
Knock,
Public,
#[doc(hidden)]
_Custom(PrivOwnedStr),
}
impl PublicRoomJoinRule {
pub fn as_str(&self) -> &str {
self.as_ref()
}
}
impl Default for PublicRoomJoinRule {
fn default() -> Self {
Self::Public
}
}
#[cfg(test)]
mod tests {
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::{IncomingRoomNetwork, RoomNetwork};
#[test]
fn serialize_matrix_network_only() {
let json = json!({});
assert_eq!(to_json_value(RoomNetwork::Matrix).unwrap(), json);
}
#[test]
fn deserialize_matrix_network_only() {
let json = json!({ "include_all_networks": false });
assert_eq!(
from_json_value::<IncomingRoomNetwork>(json).unwrap(),
IncomingRoomNetwork::Matrix
);
}
#[test]
fn serialize_default_network_is_empty() {
let json = json!({});
assert_eq!(to_json_value(RoomNetwork::default()).unwrap(), json);
}
#[test]
fn deserialize_empty_network_is_default() {
let json = json!({});
assert_eq!(
from_json_value::<IncomingRoomNetwork>(json).unwrap(),
IncomingRoomNetwork::Matrix
);
}
#[test]
fn serialize_include_all_networks() {
let json = json!({ "include_all_networks": true });
assert_eq!(to_json_value(RoomNetwork::All).unwrap(), json);
}
#[test]
fn deserialize_include_all_networks() {
let json = json!({ "include_all_networks": true });
assert_eq!(from_json_value::<IncomingRoomNetwork>(json).unwrap(), IncomingRoomNetwork::All);
}
#[test]
fn serialize_third_party_network() {
let json = json!({ "third_party_instance_id": "freenode" });
assert_eq!(to_json_value(RoomNetwork::ThirdParty("freenode")).unwrap(), json);
}
#[test]
fn deserialize_third_party_network() {
let json = json!({ "third_party_instance_id": "freenode" });
assert_eq!(
from_json_value::<IncomingRoomNetwork>(json).unwrap(),
IncomingRoomNetwork::ThirdParty("freenode".into())
);
}
#[test]
fn deserialize_include_all_networks_and_third_party_exclusivity() {
let json = json!({ "include_all_networks": true, "third_party_instance_id": "freenode" });
assert_eq!(
from_json_value::<IncomingRoomNetwork>(json).unwrap_err().to_string().as_str(),
"`include_all_networks = true` and `third_party_instance_id` are mutually exclusive."
);
}
}