Skip to main content

zaun_community_protocol/permissions/
denial.rs

1//! Typed refusals: `FORBIDDEN{missing, reason}`, `BAD_REQUEST`, `CONFLICT`, `NOT_FOUND`.
2
3use crate::bits::Permissions;
4use crate::error_code::ErrorCode;
5
6/// Why a permission check failed, when a specific rule caused it (permissions §6).
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub enum DenialReason {
9    /// H1, H2 or H4 (the management hierarchy).
10    Hierarchy,
11    /// Step 7b: probation newcomer mask or the waiting period.
12    Probation,
13    /// Step 7c: an active emergency lockdown.
14    Lockdown,
15    /// Step 7d: the channel `locked` flag.
16    ChannelLocked,
17    /// Step 7a: the member is timed out.
18    Timeout,
19}
20
21impl DenialReason {
22    /// The `reason` string of a `FORBIDDEN` body, if any.
23    #[must_use]
24    pub const fn wire(self) -> Option<&'static str> {
25        match self {
26            Self::Hierarchy => Some("hierarchy"),
27            Self::Probation => Some("probation"),
28            // SPEC: specification §8.2 calls `locked: true` "a lockdown", so a denial caused
29            // by the flag carries reason `lockdown`.
30            Self::Lockdown | Self::ChannelLocked => Some("lockdown"),
31            Self::Timeout => None,
32        }
33    }
34}
35
36/// A failed permission or hierarchy check.
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38pub struct Denial {
39    /// Required bits the actor lacks; empty when a non-bit rule failed.
40    pub missing: Permissions,
41    /// The rule that caused it, when the bits were granted but then restricted, or when a
42    /// hierarchy rule failed.
43    pub reason: Option<DenialReason>,
44}
45
46impl Denial {
47    /// A hierarchy denial (H1, H2, H4).
48    #[must_use]
49    pub const fn hierarchy() -> Self {
50        Self {
51            missing: Permissions::empty(),
52            reason: Some(DenialReason::Hierarchy),
53        }
54    }
55
56    /// A denial for missing bits with no specific cause.
57    #[must_use]
58    pub const fn missing(bits: Permissions) -> Self {
59        Self {
60            missing: bits,
61            reason: None,
62        }
63    }
64
65    /// The response code: `TIMED_OUT` when the timeout caused it (specification §8
66    /// preamble), otherwise `FORBIDDEN`.
67    #[must_use]
68    pub const fn error_code(&self) -> ErrorCode {
69        match self.reason {
70            Some(DenialReason::Timeout) => ErrorCode::TimedOut,
71            _ => ErrorCode::Forbidden,
72        }
73    }
74}
75
76/// A malformed request (`BAD_REQUEST`).
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
78pub enum Invalid {
79    /// An override has a bit in both `allow` and `deny`.
80    AllowDenyOverlap,
81    /// An override holds a known bit that is not channel-scoped.
82    NotChannelScoped,
83    /// The command sets an unknown bit that was not already present.
84    SetsUnknownBits,
85    /// `newcomer_restrictions` contains `VIEW_CHANNEL` or `READ_MESSAGE_HISTORY`.
86    NewcomerMaskHidesChannels,
87    /// `@everyone` was listed where it may not be (reorder, delete, assign).
88    Everyone,
89    /// A role appears twice in one request.
90    DuplicateRole,
91    /// The resulting role positions are not unique, or a position is 0.
92    BadPosition,
93    /// `pre_approved` together with `requires_approval`.
94    PreApprovedWithApproval,
95    /// Invitation admission overrides are not stricter than the policy.
96    LooserAdmission,
97    /// A duration is outside its bounds (timeout, lockdown).
98    Duration,
99}
100
101/// Why a state transition does not apply (`CONFLICT`).
102#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
103pub enum ConflictReason {
104    /// The target is already an admitted member (`already_member`).
105    AlreadyMember,
106    /// The target is in another state the command does not apply to (`invalid_state`).
107    WrongState,
108}
109
110impl ConflictReason {
111    /// The `reason` string of a `CONFLICT` body (specification §11): `already_member` or
112    /// `invalid_state`.
113    #[must_use]
114    pub const fn wire(self) -> Option<&'static str> {
115        match self {
116            Self::AlreadyMember => Some("already_member"),
117            Self::WrongState => Some("invalid_state"),
118        }
119    }
120}
121
122/// Every way a pure check can refuse a command.
123#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
124pub enum Refusal {
125    /// `FORBIDDEN`, or `TIMED_OUT` (see [`Denial::error_code`]).
126    Forbidden(Denial),
127    /// `BAD_REQUEST`.
128    BadRequest(Invalid),
129    /// `CONFLICT`.
130    Conflict(ConflictReason),
131    /// `NOT_FOUND` (a listed role does not exist).
132    NotFound,
133}
134
135impl Refusal {
136    /// The response code.
137    #[must_use]
138    pub const fn error_code(&self) -> ErrorCode {
139        match self {
140            Self::Forbidden(d) => d.error_code(),
141            Self::BadRequest(_) => ErrorCode::BadRequest,
142            Self::Conflict(_) => ErrorCode::Conflict,
143            Self::NotFound => ErrorCode::NotFound,
144        }
145    }
146}
147
148impl From<Denial> for Refusal {
149    fn from(d: Denial) -> Self {
150        Self::Forbidden(d)
151    }
152}
153
154impl From<Invalid> for Refusal {
155    fn from(i: Invalid) -> Self {
156        Self::BadRequest(i)
157    }
158}