Skip to main content

zaun_community_protocol/command/
reserved.rs

1//! Classification of command `type` strings (specification §8.11, §13).
2
3use super::Command;
4
5/// The reserved governance commands of §8.11: answered with `UNSUPPORTED_FEATURE`.
6pub const RESERVED_COMMANDS: &[&str] = &[
7    "automod.rule.create",
8    "automod.rule.update",
9    "automod.rule.delete",
10    "automod.rule.list",
11    "verification.request",
12    "verification.complete",
13];
14
15/// Reserved `type` namespaces of §13 (prefixes including the dot): answered with
16/// `UNSUPPORTED_COMMAND`; v1 clients ignore events in them.
17///
18/// SPEC: `signal.*` is reserved only "beyond §10.5", so `signal.typing` and
19/// `signal.presence` are known commands and every other `signal.` name is reserved.
20pub const RESERVED_NAMESPACES: &[&str] = &[
21    "voice.",
22    "media.",
23    "thread.",
24    "forum.",
25    "webhook.",
26    "emoji.",
27    "sticker.",
28    "event.",
29    "signal.",
30    "automod.",
31    "verification.",
32    "directory.",
33    "migration.",
34    "e2ee.",
35];
36
37/// What a command `type` string is to a v1 host.
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
39pub enum TypeClass {
40    /// A v1 command ([`Command::NAMES`]).
41    Known,
42    /// A named reserved command (§8.11): `UNSUPPORTED_FEATURE{feature: type}`.
43    ReservedNamed,
44    /// In a reserved namespace (§13): `UNSUPPORTED_COMMAND`.
45    ReservedNamespace,
46    /// Anything else: `UNSUPPORTED_COMMAND`.
47    Unknown,
48}
49
50/// Classifies a command `type`. Exact, case-sensitive matching.
51#[must_use]
52pub fn classify(type_: &str) -> TypeClass {
53    if Command::NAMES.contains(&type_) {
54        TypeClass::Known
55    } else if RESERVED_COMMANDS.contains(&type_) {
56        TypeClass::ReservedNamed
57    } else if RESERVED_NAMESPACES.iter().any(|ns| type_.starts_with(ns)) {
58        TypeClass::ReservedNamespace
59    } else {
60        TypeClass::Unknown
61    }
62}
63
64/// The largest command envelope for `type_` in bytes (§12): `max_frame` for
65/// `community.create` and `community.descriptor.publish`, else 65 536.
66#[must_use]
67pub fn max_envelope(type_: &str) -> usize {
68    match type_ {
69        "community.create" | "community.descriptor.publish" => crate::limits::MAX_FRAME,
70        _ => crate::limits::MAX_COMMAND,
71    }
72}