twilight_util/permission_calculator/
bitops.rs

1//! Mutable `bitflags` operations are not `const`, but since immutable
2//! operations - i.e. `bits` and `from_bits_truncate` - are `const`, we can just
3//! create wrapping operations over them.
4
5use twilight_model::guild::Permissions;
6
7/// Insert permissions into another.
8pub const fn insert(permissions: Permissions, other: Permissions) -> Permissions {
9    Permissions::from_bits_truncate(permissions.bits() | other.bits())
10}
11
12/// Remove permissions from another.
13pub const fn remove(permissions: Permissions, other: Permissions) -> Permissions {
14    Permissions::from_bits_truncate(permissions.bits() & !other.bits())
15}
16
17#[cfg(test)]
18mod tests {
19    use twilight_model::guild::Permissions;
20
21    #[test]
22    fn insert() {
23        let actual = super::insert(
24            Permissions::KICK_MEMBERS,
25            Permissions::BAN_MEMBERS | Permissions::CONNECT,
26        );
27        let expected = Permissions::BAN_MEMBERS | Permissions::CONNECT | Permissions::KICK_MEMBERS;
28
29        assert_eq!(actual, expected);
30    }
31
32    #[test]
33    fn insert_duplicate() {
34        let expected = Permissions::BAN_MEMBERS | Permissions::KICK_MEMBERS;
35        let actual = super::insert(expected, Permissions::KICK_MEMBERS);
36
37        assert_eq!(actual, expected);
38    }
39
40    #[test]
41    fn remove() {
42        let actual = super::remove(
43            Permissions::BAN_MEMBERS | Permissions::KICK_MEMBERS,
44            Permissions::BAN_MEMBERS,
45        );
46
47        assert_eq!(actual, Permissions::KICK_MEMBERS);
48    }
49
50    #[test]
51    fn remove_nonexistent() {
52        let actual = super::remove(Permissions::KICK_MEMBERS, Permissions::BAN_MEMBERS);
53
54        assert_eq!(actual, Permissions::KICK_MEMBERS);
55    }
56}