ruma_common/
power_levels.rs

1//! Common types for the [`m.room.power_levels` event][power_levels].
2//!
3//! [power_levels]: https://spec.matrix.org/latest/client-server-api/#mroompower_levels
4
5use js_int::{int, Int};
6use ruma_macros::StringEnum;
7use serde::{Deserialize, Serialize};
8
9use crate::PrivOwnedStr;
10
11/// The power level requirements for specific notification types.
12#[derive(Clone, Debug, Deserialize, Serialize)]
13#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
14pub struct NotificationPowerLevels {
15    /// The level required to trigger an `@room` notification.
16    ///
17    /// Defaults to `50`.
18    #[serde(
19        default = "default_power_level",
20        deserialize_with = "crate::serde::deserialize_v1_powerlevel"
21    )]
22    pub room: Int,
23}
24
25impl NotificationPowerLevels {
26    /// Create a new `NotificationPowerLevels` with all-default values.
27    pub fn new() -> Self {
28        Self { room: default_power_level() }
29    }
30
31    /// Value associated with the given `key`.
32    pub fn get(&self, key: &NotificationPowerLevelsKey) -> Option<&Int> {
33        match key {
34            NotificationPowerLevelsKey::Room => Some(&self.room),
35            NotificationPowerLevelsKey::_Custom(_) => None,
36        }
37    }
38
39    /// Whether all fields have their default values.
40    pub fn is_default(&self) -> bool {
41        self.room == default_power_level()
42    }
43}
44
45impl Default for NotificationPowerLevels {
46    fn default() -> Self {
47        Self::new()
48    }
49}
50
51/// Used to default power levels to 50 during deserialization.
52pub fn default_power_level() -> Int {
53    int!(50)
54}
55
56/// The possible keys of [`NotificationPowerLevels`].
57#[derive(Clone, PartialEq, Eq, StringEnum)]
58#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
59#[ruma_enum(rename_all = "lowercase")]
60#[non_exhaustive]
61pub enum NotificationPowerLevelsKey {
62    /// The key for `@room` notifications.
63    Room,
64
65    #[doc(hidden)]
66    _Custom(PrivOwnedStr),
67}