ruma_events/policy/
rule.rs

1//! Modules and types for events in the `m.policy.rule` namespace.
2
3use ruma_common::serde::StringEnum;
4use serde::{Deserialize, Serialize};
5
6use crate::PrivOwnedStr;
7
8pub mod room;
9pub mod server;
10pub mod user;
11
12/// The payload for policy rule events.
13#[derive(Clone, Debug, Deserialize, Serialize)]
14#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
15pub struct PolicyRuleEventContent {
16    /// The entity affected by this rule.
17    ///
18    /// Glob characters `*` and `?` can be used to match zero or more characters or exactly one
19    /// character respectively.
20    pub entity: String,
21
22    /// The suggested action to take.
23    pub recommendation: Recommendation,
24
25    /// The human-readable description for the recommendation.
26    pub reason: String,
27}
28
29impl PolicyRuleEventContent {
30    /// Creates a new `PolicyRuleEventContent` with the given entity, recommendation and reason.
31    pub fn new(entity: String, recommendation: Recommendation, reason: String) -> Self {
32        Self { entity, recommendation, reason }
33    }
34}
35
36/// The possibly redacted form of [`PolicyRuleEventContent`].
37///
38/// This type is used when it's not obvious whether the content is redacted or not.
39#[derive(Clone, Debug, Deserialize, Serialize)]
40#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
41pub struct PossiblyRedactedPolicyRuleEventContent {
42    /// The entity affected by this rule.
43    ///
44    /// Glob characters `*` and `?` can be used to match zero or more characters or exactly one
45    /// character respectively.
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub entity: Option<String>,
48
49    /// The suggested action to take.
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub recommendation: Option<Recommendation>,
52
53    /// The human-readable description for the recommendation.
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub reason: Option<String>,
56}
57
58impl PossiblyRedactedPolicyRuleEventContent {
59    /// Creates a new `PossiblyRedactedPolicyRuleEventContent` with the given entity, recommendation
60    /// and reason.
61    pub fn new(entity: String, recommendation: Recommendation, reason: String) -> Self {
62        Self { entity: Some(entity), recommendation: Some(recommendation), reason: Some(reason) }
63    }
64
65    /// Creates an empty `PossiblyRedactedPolicyRuleEventContent`.
66    pub(crate) fn empty() -> Self {
67        Self { entity: None, recommendation: None, reason: None }
68    }
69}
70
71impl From<PolicyRuleEventContent> for PossiblyRedactedPolicyRuleEventContent {
72    fn from(value: PolicyRuleEventContent) -> Self {
73        let PolicyRuleEventContent { entity, recommendation, reason } = value;
74        Self { entity: Some(entity), recommendation: Some(recommendation), reason: Some(reason) }
75    }
76}
77
78/// The possible actions that can be taken.
79#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
80#[derive(Clone, StringEnum)]
81#[non_exhaustive]
82pub enum Recommendation {
83    /// Entities affected by the rule should be banned from participation where possible.
84    #[ruma_enum(rename = "m.ban")]
85    Ban,
86
87    #[doc(hidden)]
88    _Custom(PrivOwnedStr),
89}