zaun_community_protocol/command/
role.rs1use 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#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
15pub struct Create {
16 pub name: String,
18 #[serde(default, skip_serializing_if = "Option::is_none")]
20 pub color: Option<u32>,
21 #[serde(default, skip_serializing_if = "Option::is_none")]
23 pub hoist: Option<bool>,
24 #[serde(default, skip_serializing_if = "Option::is_none")]
26 pub mentionable: Option<bool>,
27 #[serde(default, skip_serializing_if = "Option::is_none")]
29 pub permissions: Option<Permissions>,
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
37pub struct Update {
38 pub role_id: RoleId,
40 #[serde(default, skip_serializing_if = "Patch::is_keep")]
42 pub name: Patch<String>,
43 #[serde(default, skip_serializing_if = "Patch::is_keep")]
45 pub color: Patch<u32>,
46 #[serde(default, skip_serializing_if = "Patch::is_keep")]
48 pub hoist: Patch<bool>,
49 #[serde(default, skip_serializing_if = "Patch::is_keep")]
51 pub mentionable: Patch<bool>,
52 #[serde(default, skip_serializing_if = "Patch::is_keep")]
54 pub permissions: Patch<Permissions>,
55}
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
59pub struct Delete {
60 pub role_id: RoleId,
62}
63
64#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
66pub struct Reorder {
67 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 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}