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