Skip to main content

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