Skip to main content

zaun_community_protocol/
bits.rs

1//! The permission bitfield (permissions.md §1). Bit positions are frozen once released.
2//!
3//! The resolution algorithm lives in the `permissions` module; this module only names bits.
4
5use bitflags::bitflags;
6use serde::{Deserialize, Deserializer, Serialize, Serializer};
7
8bitflags! {
9    /// A permission set. JSON: a canonical decimal string. SQLite: `i64` by bit-cast.
10    ///
11    /// Unknown bits are preserved by [`Permissions::from_bits_retain`] (permissions §1.2) and
12    /// must be masked with [`Permissions::KNOWN`] before they grant anything.
13    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
14    pub struct Permissions: u64 {
15        const VIEW_CHANNEL = 1 << 0;
16        const MANAGE_CHANNELS = 1 << 1;
17        const MANAGE_ROLES = 1 << 2;
18        const MANAGE_COMMUNITY = 1 << 3;
19        const VIEW_AUDIT_LOG = 1 << 4;
20        const CREATE_INVITE = 1 << 5;
21        const MANAGE_INVITES = 1 << 6;
22        const KICK_MEMBERS = 1 << 7;
23        const BAN_MEMBERS = 1 << 8;
24        const TIMEOUT_MEMBERS = 1 << 9;
25        const MANAGE_WEBHOOKS = 1 << 10;
26        const MANAGE_EMOJIS = 1 << 11;
27        const ADMINISTRATOR = 1 << 12;
28        const CHANGE_NICKNAME = 1 << 13;
29        const MANAGE_NICKNAMES = 1 << 14;
30        const APPROVE_MEMBERS = 1 << 15;
31        const SEND_MESSAGES = 1 << 16;
32        const READ_MESSAGE_HISTORY = 1 << 17;
33        const MANAGE_MESSAGES = 1 << 18;
34        const ATTACH_FILES = 1 << 19;
35        const EMBED_LINKS = 1 << 20;
36        const ADD_REACTIONS = 1 << 21;
37        const MENTION_EVERYONE = 1 << 22;
38        const CREATE_THREADS = 1 << 23;
39        const MANAGE_THREADS = 1 << 24;
40        const SEND_MESSAGES_IN_THREADS = 1 << 25;
41        const BYPASS_SLOWMODE = 1 << 26;
42        const USE_APPLICATION_COMMANDS = 1 << 27;
43        const CONNECT = 1 << 32;
44        const SPEAK = 1 << 33;
45        const STREAM = 1 << 34;
46        const VIDEO = 1 << 35;
47        const MUTE_MEMBERS = 1 << 36;
48        const DEAFEN_MEMBERS = 1 << 37;
49        const MOVE_MEMBERS = 1 << 38;
50        const MANAGE_REPORTS = 1 << 48;
51        const MANAGE_SAFETY = 1 << 49;
52    }
53}
54
55impl Permissions {
56    /// Every bit assigned in protocol version 1. "All permissions" means this, never `u64::MAX`.
57    pub const KNOWN: Self = Self::all();
58
59    /// Voice and media bits (32–38), not applicable to text-like channels (permissions §1.3).
60    pub const VOICE: Self = Self::CONNECT
61        .union(Self::SPEAK)
62        .union(Self::STREAM)
63        .union(Self::VIDEO)
64        .union(Self::MUTE_MEMBERS)
65        .union(Self::DEAFEN_MEMBERS)
66        .union(Self::MOVE_MEMBERS);
67
68    /// Bits whose scope is "channel"; only these may appear in an override.
69    pub const CHANNEL_SCOPED: Self = Self::VIEW_CHANNEL
70        .union(Self::MANAGE_CHANNELS)
71        .union(Self::MANAGE_ROLES)
72        .union(Self::CREATE_INVITE)
73        .union(Self::MANAGE_WEBHOOKS)
74        .union(Self::SEND_MESSAGES)
75        .union(Self::READ_MESSAGE_HISTORY)
76        .union(Self::MANAGE_MESSAGES)
77        .union(Self::ATTACH_FILES)
78        .union(Self::EMBED_LINKS)
79        .union(Self::ADD_REACTIONS)
80        .union(Self::MENTION_EVERYONE)
81        .union(Self::CREATE_THREADS)
82        .union(Self::MANAGE_THREADS)
83        .union(Self::SEND_MESSAGES_IN_THREADS)
84        .union(Self::BYPASS_SLOWMODE)
85        .union(Self::USE_APPLICATION_COMMANDS)
86        .union(Self::VOICE);
87
88    /// Every known bit that is not channel-scoped.
89    pub const COMMUNITY_SCOPED: Self = Self::KNOWN.difference(Self::CHANNEL_SCOPED);
90
91    /// The applicable channel bits of a `category` (permissions §1.3).
92    pub const CATEGORY_APPLICABLE: Self = Self::VIEW_CHANNEL
93        .union(Self::MANAGE_CHANNELS)
94        .union(Self::MANAGE_ROLES)
95        .union(Self::CREATE_INVITE);
96
97    /// SQLite storage form (bit-cast, never compared numerically).
98    #[must_use]
99    pub const fn to_i64(self) -> i64 {
100        i64::from_ne_bytes(self.bits().to_ne_bytes())
101    }
102
103    /// From the SQLite storage form, retaining unknown bits.
104    #[must_use]
105    pub const fn from_i64(value: i64) -> Self {
106        Self::from_bits_retain(u64::from_ne_bytes(value.to_ne_bytes()))
107    }
108}
109
110impl Serialize for Permissions {
111    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
112        crate::json::u64_decimal::serialize(&self.bits(), s)
113    }
114}
115
116impl<'de> Deserialize<'de> for Permissions {
117    /// Retains unknown bits; commands that *set* bits must reject unknown ones themselves
118    /// (permissions §1.2).
119    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
120        crate::json::u64_decimal::deserialize(d).map(Self::from_bits_retain)
121    }
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127
128    #[test]
129    fn frozen_positions() {
130        assert_eq!(Permissions::APPROVE_MEMBERS.bits(), 1 << 15);
131        assert_eq!(Permissions::BYPASS_SLOWMODE.bits(), 1 << 26);
132        assert_eq!(Permissions::MANAGE_REPORTS.bits(), 1 << 48);
133        assert_eq!(Permissions::MANAGE_SAFETY.bits(), 1 << 49);
134        assert_eq!(Permissions::USE_APPLICATION_COMMANDS.bits(), 1 << 27);
135        assert_eq!(Permissions::KNOWN.bits().count_ones(), 37);
136        assert!(
137            Permissions::CHANNEL_SCOPED
138                .intersection(Permissions::COMMUNITY_SCOPED)
139                .is_empty()
140        );
141        assert!(Permissions::COMMUNITY_SCOPED.contains(Permissions::ADMINISTRATOR));
142        assert!(Permissions::COMMUNITY_SCOPED.contains(Permissions::MANAGE_COMMUNITY));
143    }
144
145    #[test]
146    fn unknown_bits_survive_json_and_sqlite() {
147        let p = Permissions::from_bits_retain(1 << 63 | 1);
148        let json = serde_json::to_string(&p).unwrap();
149        assert_eq!(json, "\"9223372036854775809\"");
150        assert_eq!(serde_json::from_str::<Permissions>(&json).unwrap(), p);
151        assert_eq!(Permissions::from_i64(p.to_i64()), p);
152        assert_eq!(
153            p.intersection(Permissions::KNOWN),
154            Permissions::VIEW_CHANNEL
155        );
156    }
157}