Skip to main content

zaun_community_protocol/
error_code.rs

1//! Error codes (specification §11). The `envelope` module builds error bodies around these.
2
3use serde::{Deserialize, Serialize};
4
5/// A response error code. Clients branch on this, never on the message text.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
8pub enum ErrorCode {
9    BadRequest,
10    PayloadTooLarge,
11    UnsupportedVersion,
12    UnsupportedCommand,
13    UnsupportedFeature,
14    HelloRequired,
15    NotBound,
16    WrongCommunity,
17    HostNotAuthorized,
18    Forbidden,
19    PendingApproval,
20    Suspended,
21    AdmissionClosed,
22    NotFound,
23    Conflict,
24    InvalidSignature,
25    InviteInvalid,
26    SlowmodeActive,
27    RateLimited,
28    TimedOut,
29    Banned,
30    Busy,
31    ResyncRequired,
32    Internal,
33}
34
35impl ErrorCode {
36    /// Whether a response with this code is stored as the idempotency record (§7.3): every
37    /// error except the transient ones, which may be retried with the same `request_id`.
38    #[must_use]
39    pub const fn is_recorded(self) -> bool {
40        !matches!(
41            self,
42            Self::Busy | Self::RateLimited | Self::Internal | Self::SlowmodeActive
43        )
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn wire_names() {
53        assert_eq!(
54            serde_json::to_string(&ErrorCode::SlowmodeActive).unwrap(),
55            "\"SLOWMODE_ACTIVE\""
56        );
57        assert_eq!(
58            serde_json::to_string(&ErrorCode::HostNotAuthorized).unwrap(),
59            "\"HOST_NOT_AUTHORIZED\""
60        );
61        assert!(!ErrorCode::Busy.is_recorded());
62        assert!(ErrorCode::Forbidden.is_recorded());
63    }
64}