Skip to main content

zaun_community_protocol/command/
role.rs

1//! Role commands (specification §8.4, permissions §4.2). Responses are
2//! [`Role`](super::object::Role) or `{}`.
3
4use serde::{Deserialize, Serialize};
5
6use super::object::{COLOR_MAX, RolePosition};
7use super::{BodyError, Validate, check};
8use crate::bits::Permissions;
9use crate::ids::RoleId;
10use crate::json::Patch;
11use crate::limits::{ROLE_NAME, ROLES_PER_COMMUNITY};
12
13/// `role.create` (§8.4).
14#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
15pub struct Create {
16    /// 1–100 scalars.
17    pub name: String,
18    /// `0x000000`–`0xFFFFFF`.
19    #[serde(default, skip_serializing_if = "Option::is_none")]
20    pub color: Option<u32>,
21    /// Shown separately.
22    #[serde(default, skip_serializing_if = "Option::is_none")]
23    pub hoist: Option<bool>,
24    /// Mentionable by anyone.
25    #[serde(default, skip_serializing_if = "Option::is_none")]
26    pub mentionable: Option<bool>,
27    /// Known bits only.
28    #[serde(default, skip_serializing_if = "Option::is_none")]
29    pub permissions: Option<Permissions>,
30}
31
32/// `role.update` (§8.4): any of the `role.create` fields.
33///
34/// SPEC: "any of the fields above" is read as the `role.create` fields (positions change
35/// only through `role.reorder`).
36#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
37pub struct Update {
38    /// The role.
39    pub role_id: RoleId,
40    /// New name.
41    #[serde(default, skip_serializing_if = "Patch::is_keep")]
42    pub name: Patch<String>,
43    /// New colour; `null` clears.
44    #[serde(default, skip_serializing_if = "Patch::is_keep")]
45    pub color: Patch<u32>,
46    /// New hoist flag.
47    #[serde(default, skip_serializing_if = "Patch::is_keep")]
48    pub hoist: Patch<bool>,
49    /// New mentionable flag.
50    #[serde(default, skip_serializing_if = "Patch::is_keep")]
51    pub mentionable: Patch<bool>,
52    /// New permission set.
53    #[serde(default, skip_serializing_if = "Patch::is_keep")]
54    pub permissions: Patch<Permissions>,
55}
56
57/// `role.delete` (§8.4).
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
59pub struct Delete {
60    /// The role; never `@everyone`.
61    pub role_id: RoleId,
62}
63
64/// `role.reorder` (§8.4, permissions §4.2).
65#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
66pub struct Reorder {
67    /// `(role_id, position)` pairs; no `@everyone`, unique roles and positions, positions ≥ 1.
68    pub positions: Vec<RolePosition>,
69}
70
71fn check_color(color: u32) -> Result<(), BodyError> {
72    check::range("color", color, 0, COLOR_MAX)
73}
74
75impl Validate for Create {
76    fn validate(&self) -> Result<(), BodyError> {
77        check::text("name", &self.name, ROLE_NAME)?;
78        self.color.map_or(Ok(()), check_color)?;
79        self.permissions
80            .map_or(Ok(()), |p| check::known_bits("permissions", p))
81    }
82}
83
84impl Validate for Update {
85    /// SPEC: unknown bits already on a role may pass through unchanged (permissions §1.2),
86    /// so `permissions` is not checked for unknown bits here; the engine compares with the
87    /// stored value. `@everyone` may change only `permissions` (§8.4): any other field is
88    /// a bad request.
89    fn validate(&self) -> Result<(), BodyError> {
90        let others_untouched = self.name.is_keep()
91            && self.color.is_keep()
92            && self.hoist.is_keep()
93            && self.mentionable.is_keep();
94        if others_untouched && self.permissions.is_keep() {
95            return Err(BodyError::bad("body", "no field to update"));
96        }
97        if self.role_id.is_everyone() && !others_untouched {
98            return Err(BodyError::bad(
99                "role_id",
100                "@everyone may change only permissions",
101            ));
102        }
103        check::not_null("name", &self.name)?;
104        check::not_null("hoist", &self.hoist)?;
105        check::not_null("mentionable", &self.mentionable)?;
106        check::not_null("permissions", &self.permissions)?;
107        if let Some(name) = check::set(&self.name) {
108            check::text("name", name, ROLE_NAME)?;
109        }
110        check::set(&self.color).map_or(Ok(()), |c| check_color(*c))
111    }
112}
113
114impl Validate for Delete {
115    fn validate(&self) -> Result<(), BodyError> {
116        check::not_everyone("role_id", [&self.role_id])
117    }
118}
119
120impl Validate for Reorder {
121    fn validate(&self) -> Result<(), BodyError> {
122        if self.positions.is_empty() {
123            return Err(BodyError::bad("positions", "is empty"));
124        }
125        check::count("positions", self.positions.len(), ROLES_PER_COMMUNITY)?;
126        check::not_everyone("positions", self.positions.iter().map(|p| &p.role_id))?;
127        check::unique("positions", self.positions.iter().map(|p| &p.role_id))?;
128        check::unique("positions", self.positions.iter().map(|p| &p.position))?;
129        match self.positions.iter().any(|p| p.position == 0) {
130            true => Err(BodyError::bad("positions", "position 0 is @everyone's")),
131            false => Ok(()),
132        }
133    }
134}