Skip to main content

zaun_community_protocol/command/member/
validate.rs

1//! State-free checks of the member commands.
2
3use super::{BanMember, Bans, Get, Kick, List, RolesUpdate, Timeout, Unban, Update};
4use crate::command::{BodyError, Validate, check};
5use crate::limits::{
6    BAN_DELETE_WINDOW_MAX_MS, DISPLAY_NAME, PAGE_LARGE, PAGE_SMALL, REASON_MAX, ROLES_PER_MEMBER,
7};
8
9impl Validate for List {
10    fn validate(&self) -> Result<(), BodyError> {
11        check::page(self.limit, PAGE_LARGE)
12    }
13}
14
15impl Validate for Get {
16    fn validate(&self) -> Result<(), BodyError> {
17        Ok(())
18    }
19}
20
21impl Validate for Update {
22    fn validate(&self) -> Result<(), BodyError> {
23        check::text("display_name", &self.display_name, DISPLAY_NAME)
24    }
25}
26
27impl Validate for RolesUpdate {
28    /// `@everyone` is never listed; no role twice (across both lists); at least one entry.
29    fn validate(&self) -> Result<(), BodyError> {
30        if self.add.is_empty() && self.remove.is_empty() {
31            return Err(BodyError::bad("body", "no role to change"));
32        }
33        check::count("add", self.add.len(), ROLES_PER_MEMBER)?;
34        check::count("remove", self.remove.len(), ROLES_PER_MEMBER)?;
35        check::not_everyone("add", &self.add)?;
36        check::not_everyone("remove", &self.remove)?;
37        check::unique("add", &self.add)?;
38        check::unique("remove", self.add.iter().chain(&self.remove))
39    }
40}
41
42impl Validate for Kick {
43    fn validate(&self) -> Result<(), BodyError> {
44        check::opt_text("reason", self.reason.as_deref(), (0, REASON_MAX))
45    }
46}
47
48impl Validate for BanMember {
49    fn validate(&self) -> Result<(), BodyError> {
50        check::opt_text("reason", self.reason.as_deref(), (0, REASON_MAX))?;
51        check::range(
52            "delete_messages_s",
53            self.delete_messages_s,
54            0,
55            BAN_DELETE_WINDOW_MAX_MS / 1000,
56        )
57    }
58}
59
60impl Validate for Unban {
61    fn validate(&self) -> Result<(), BodyError> {
62        Ok(())
63    }
64}
65
66impl Validate for Bans {
67    fn validate(&self) -> Result<(), BodyError> {
68        check::page(self.limit, PAGE_SMALL)
69    }
70}
71
72impl Validate for Timeout {
73    fn validate(&self) -> Result<(), BodyError> {
74        check::opt_text("reason", self.reason.as_deref(), (0, REASON_MAX))?;
75        self.until.map_or(Ok(()), |u| check::json_safe("until", u))
76    }
77}