Skip to main content

tuwunel_service/rooms/state_res/events/
join_rules.rs

1//! Types to deserialize `m.room.join_rules` events.
2
3use std::ops::Deref;
4
5use ruma::serde::{StringEnum, from_raw_json_value};
6use serde::Deserialize;
7use tuwunel_core::{Error, Result, err, matrix::Event};
8
9/// A helper type for an [`Event`] of type `m.room.join_rules`.
10///
11/// This is a type that deserializes each field lazily, when requested.
12#[derive(Debug, Clone)]
13pub struct RoomJoinRulesEvent<E: Event>(E);
14
15impl<E: Event> RoomJoinRulesEvent<E> {
16	/// Construct a new `RoomJoinRulesEvent` around the given event.
17	#[inline]
18	pub fn new(event: E) -> Self { Self(event) }
19
20	/// The join rule of the room.
21	pub fn join_rule(&self) -> Result<JoinRule> {
22		#[derive(Deserialize)]
23		struct RoomJoinRulesContentJoinRule {
24			join_rule: JoinRule,
25		}
26
27		let content: RoomJoinRulesContentJoinRule =
28			from_raw_json_value(self.content()).map_err(|err: Error| {
29				err!("missing or invalid `join_rule` field in `m.room.join_rules` event: {err}")
30			})?;
31
32		Ok(content.join_rule)
33	}
34}
35
36impl<E: Event> Deref for RoomJoinRulesEvent<E> {
37	type Target = E;
38
39	#[inline]
40	fn deref(&self) -> &Self::Target { &self.0 }
41}
42
43/// The possible values for the join rule of a room.
44#[derive(Clone, StringEnum)]
45#[ruma_enum(rename_all = "snake_case")]
46#[non_exhaustive]
47pub enum JoinRule {
48	/// `public`
49	Public,
50
51	/// `invite`
52	Invite,
53
54	/// `knock`
55	Knock,
56
57	/// `restricted`
58	Restricted,
59
60	/// `KnockRestricted`
61	KnockRestricted,
62
63	#[doc(hidden)]
64	_Custom(PrivOwnedStr),
65}
66
67// Wrapper around `Box<str>` that cannot be used in a meaningful way outside of
68// this crate. Used for string enums because their `_Custom` variant can't be
69// truly private (only `#[doc(hidden)]`).
70#[derive(Debug, Clone)]
71pub struct PrivOwnedStr(Box<str>);