Skip to main content

twilight_model/channel/
attachment_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 AttachmentFlags: u64 {
10        /// This attachment is a Clip from a stream
11        const IS_CLIP = 1 << 0;
12        /// This attachment is the thumbnail of a thread in a media channel, displayed in the grid but not on the message
13        const IS_THUMBNAIL = 1 << 1;
14        /// This attachment has been edited using the remix feature on mobile
15        #[deprecated]
16        const IS_REMIX = 1 << 2;
17        /// This attachment was marked as a spoiler and is blurred until clicked
18        const IS_SPOILER = 1 << 3;
19        /// This attachment was flagged as explicit by Discord's content scanning.
20        const CONTAINS_EXPLICIT_CONTENT = 1 << 4;
21        /// This attachment is an animated image
22        const IS_ANIMATED = 1 << 5;
23        /// This attachment was flagged as gore by Discord's content scanning.
24        const CONTAINS_GORE_CONTENT = 1 << 6;
25        /// This attachment was flagged as self-harm by Discord's content scanning.
26        const CONTAINS_SELF_HARM_CONTENT = 1 << 7;
27    }
28}
29
30impl<'de> Deserialize<'de> for AttachmentFlags {
31    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
32        Ok(Self::from_bits_truncate(u64::deserialize(deserializer)?))
33    }
34}
35
36impl Serialize for AttachmentFlags {
37    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
38    where
39        S: Serializer,
40    {
41        serializer.serialize_u64(self.bits())
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::AttachmentFlags;
48    use serde::{Deserialize, Serialize};
49    use serde_test::Token;
50    use static_assertions::{assert_impl_all, const_assert_eq};
51    use std::{
52        fmt::{Binary, Debug, LowerHex, Octal, UpperHex},
53        hash::Hash,
54        ops::{
55            BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Sub, SubAssign,
56        },
57    };
58
59    assert_impl_all!(
60        AttachmentFlags: Binary,
61        BitAnd,
62        BitAndAssign,
63        BitOr,
64        BitOrAssign,
65        BitXor,
66        BitXorAssign,
67        Clone,
68        Copy,
69        Debug,
70        Deserialize<'static>,
71        Eq,
72        Extend<AttachmentFlags>,
73        FromIterator<AttachmentFlags>,
74        Hash,
75        LowerHex,
76        Not,
77        Octal,
78        PartialEq,
79        Send,
80        Serialize,
81        Sub,
82        SubAssign,
83        Sync,
84        UpperHex
85    );
86
87    const_assert_eq!(AttachmentFlags::IS_SPOILER.bits(), 8);
88
89    #[test]
90    fn serde() {
91        serde_test::assert_tokens(
92            &AttachmentFlags::IS_SPOILER,
93            &[Token::U64(AttachmentFlags::IS_SPOILER.bits())],
94        );
95
96        // Deserialization truncates unknown bits.
97        serde_test::assert_de_tokens(&AttachmentFlags::empty(), &[Token::U64(1 << 63)]);
98    }
99}