Skip to main content

twilight_model/oauth/
application_flags.rs

1use bitflags::bitflags;
2use serde::{
3    de::{Deserialize, Deserializer},
4    ser::{Serialize, Serializer},
5};
6
7bitflags! {
8    #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
9    pub struct ApplicationFlags: u64 {
10        /// Indicates if an app uses the Auto Moderation API.
11        const APPLICATION_AUTO_MODERATION_RULE_CREATE_BADGE = 1 << 6;
12        /// Intent required for bots in 100 guilds or more to receive
13        /// [`PresenceUpdate`] events.
14        ///
15        /// [`PresenceUpdate`]: crate::gateway::payload::incoming::PresenceUpdate
16        const GATEWAY_PRESENCE = 1 << 12;
17        /// Intent required for bots in less than 100 guilds to receive
18        /// [`PresenceUpdate`] events.
19        ///
20        /// [`PresenceUpdate`]: crate::gateway::payload::incoming::PresenceUpdate
21        const GATEWAY_PRESENCE_LIMITED = 1 << 13;
22        /// Intent required for bots in 100 guilds or more to receive
23        /// member-related events like [`MemberAdd`].
24        ///
25        /// [`MemberAdd`]: crate::gateway::payload::incoming::MemberAdd
26        const GATEWAY_GUILD_MEMBERS = 1 << 14;
27        /// Intent required for bots in less than 100 guilds to receive
28        /// member-related events like [`MemberAdd`].
29        ///
30        /// [`MemberAdd`]: crate::gateway::payload::incoming::MemberAdd
31        const GATEWAY_GUILD_MEMBERS_LIMITED = 1 << 15;
32        /// Indicates unusual growth of an app that prevents verification.
33        const VERIFICATION_PENDING_GUILD_LIMIT = 1 << 16;
34        /// Indicates if an app is embedded within the Discord client.
35        const EMBEDDED = 1 << 17;
36        /// Intent required for bots in 100 guilds or more to receive
37        /// [message content].
38        ///
39        /// [message content]: https://support-dev.discord.com/hc/en-us/articles/4404772028055
40        const GATEWAY_MESSAGE_CONTENT = 1 << 18;
41        /// Intent required for bots in less than 100 guilds to receive
42        /// [message content].
43        ///
44        /// [message content]: https://support-dev.discord.com/hc/en-us/articles/4404772028055
45        const GATEWAY_MESSAGE_CONTENT_LIMITED = 1 << 19;
46
47        /// Indicates whether an app has registered global [application commands].
48        ///
49        /// [application commands]: https://discord.com/developers/docs/interactions/application-commands
50        const APPLICATION_COMMAND_BADGE = 1 << 23;
51    }
52}
53
54impl<'de> Deserialize<'de> for ApplicationFlags {
55    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
56        Ok(Self::from_bits_truncate(u64::deserialize(deserializer)?))
57    }
58}
59
60impl Serialize for ApplicationFlags {
61    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
62    where
63        S: Serializer,
64    {
65        serializer.serialize_u64(self.bits())
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::ApplicationFlags;
72    use serde::{Deserialize, Serialize};
73    use serde_test::Token;
74    use static_assertions::{assert_impl_all, const_assert_eq};
75    use std::{
76        fmt::{Binary, Debug, LowerHex, Octal, UpperHex},
77        hash::Hash,
78        ops::{
79            BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Sub, SubAssign,
80        },
81    };
82
83    assert_impl_all!(
84        ApplicationFlags: Binary,
85        BitAnd,
86        BitAndAssign,
87        BitOr,
88        BitOrAssign,
89        BitXor,
90        BitXorAssign,
91        Clone,
92        Copy,
93        Debug,
94        Deserialize<'static>,
95        Eq,
96        Extend<ApplicationFlags>,
97        FromIterator<ApplicationFlags>,
98        Hash,
99        LowerHex,
100        Not,
101        Octal,
102        PartialEq,
103        Send,
104        Serialize,
105        Sub,
106        SubAssign,
107        Sync,
108        UpperHex
109    );
110    const_assert_eq!(
111        ApplicationFlags::APPLICATION_AUTO_MODERATION_RULE_CREATE_BADGE.bits(),
112        1 << 6
113    );
114    const_assert_eq!(ApplicationFlags::GATEWAY_PRESENCE.bits(), 1 << 12);
115    const_assert_eq!(ApplicationFlags::GATEWAY_PRESENCE_LIMITED.bits(), 1 << 13);
116    const_assert_eq!(ApplicationFlags::GATEWAY_GUILD_MEMBERS.bits(), 1 << 14);
117    const_assert_eq!(
118        ApplicationFlags::GATEWAY_GUILD_MEMBERS_LIMITED.bits(),
119        1 << 15
120    );
121    const_assert_eq!(
122        ApplicationFlags::VERIFICATION_PENDING_GUILD_LIMIT.bits(),
123        1 << 16
124    );
125    const_assert_eq!(ApplicationFlags::EMBEDDED.bits(), 1 << 17);
126    const_assert_eq!(ApplicationFlags::GATEWAY_MESSAGE_CONTENT.bits(), 1 << 18);
127    const_assert_eq!(
128        ApplicationFlags::GATEWAY_MESSAGE_CONTENT_LIMITED.bits(),
129        1 << 19
130    );
131    const_assert_eq!(ApplicationFlags::APPLICATION_COMMAND_BADGE.bits(), 1 << 23);
132
133    #[test]
134    fn serde() {
135        serde_test::assert_tokens(
136            &ApplicationFlags::GATEWAY_MESSAGE_CONTENT,
137            &[Token::U64(ApplicationFlags::GATEWAY_MESSAGE_CONTENT.bits())],
138        );
139        // Deserialization truncates unknown bits.
140        serde_test::assert_de_tokens(&ApplicationFlags::empty(), &[Token::U64(1 << 63)]);
141    }
142}