Skip to main content

zaun_community_protocol/command/community/
validate.rs

1//! State-free checks of the community commands.
2
3use super::{Create, DescriptorPublish, Get, IconGet, Leave, ROTATIONS_PAGE, Rotations, Update};
4use crate::command::{BodyError, Validate, check};
5use crate::limits::{ICON_MAX, SLOWMODE_MAX_S};
6
7impl Validate for Get {
8    fn validate(&self) -> Result<(), BodyError> {
9        Ok(())
10    }
11}
12
13impl Validate for IconGet {
14    fn validate(&self) -> Result<(), BodyError> {
15        Ok(())
16    }
17}
18
19impl Validate for Leave {
20    fn validate(&self) -> Result<(), BodyError> {
21        Ok(())
22    }
23}
24
25impl Validate for Create {
26    fn validate(&self) -> Result<(), BodyError> {
27        self.profile.check()
28    }
29}
30
31impl Validate for Rotations {
32    fn validate(&self) -> Result<(), BodyError> {
33        check::page(self.limit, ROTATIONS_PAGE)?;
34        self.after.map_or(Ok(()), |a| check::json_safe("after", a))
35    }
36}
37
38impl Validate for Update {
39    /// SPEC: "any of" is read as "at least one"; an empty update is a bad request.
40    fn validate(&self) -> Result<(), BodyError> {
41        if self.system_channel_id.is_keep() && self.default_slowmode_s.is_keep() {
42            return Err(BodyError::bad("body", "no field to update"));
43        }
44        check::not_null("default_slowmode_s", &self.default_slowmode_s)?;
45        check::set(&self.default_slowmode_s).map_or(Ok(()), |s| {
46            check::range("default_slowmode_s", *s, 0, SLOWMODE_MAX_S)
47        })
48    }
49}
50
51impl Validate for DescriptorPublish {
52    fn validate(&self) -> Result<(), BodyError> {
53        let len = self.icon.as_ref().map_or(0, Vec::len);
54        check::count("icon", len, ICON_MAX)
55    }
56}