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 is an animated image
20        const IS_ANIMATED = 1 << 5;
21    }
22}
23
24impl<'de> Deserialize<'de> for AttachmentFlags {
25    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
26        Ok(Self::from_bits_truncate(u64::deserialize(deserializer)?))
27    }
28}
29
30impl Serialize for AttachmentFlags {
31    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
32    where
33        S: Serializer,
34    {
35        serializer.serialize_u64(self.bits())
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::AttachmentFlags;
42    use serde::{Deserialize, Serialize};
43    use serde_test::Token;
44    use static_assertions::{assert_impl_all, const_assert_eq};
45    use std::{
46        fmt::{Binary, Debug, LowerHex, Octal, UpperHex},
47        hash::Hash,
48        ops::{
49            BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Sub, SubAssign,
50        },
51    };
52
53    assert_impl_all!(
54        AttachmentFlags: Binary,
55        BitAnd,
56        BitAndAssign,
57        BitOr,
58        BitOrAssign,
59        BitXor,
60        BitXorAssign,
61        Clone,
62        Copy,
63        Debug,
64        Deserialize<'static>,
65        Eq,
66        Extend<AttachmentFlags>,
67        FromIterator<AttachmentFlags>,
68        Hash,
69        LowerHex,
70        Not,
71        Octal,
72        PartialEq,
73        Send,
74        Serialize,
75        Sub,
76        SubAssign,
77        Sync,
78        UpperHex
79    );
80
81    const_assert_eq!(AttachmentFlags::IS_SPOILER.bits(), 8);
82
83    #[test]
84    fn serde() {
85        serde_test::assert_tokens(
86            &AttachmentFlags::IS_SPOILER,
87            &[Token::U64(AttachmentFlags::IS_SPOILER.bits())],
88        );
89
90        // Deserialization truncates unknown bits.
91        serde_test::assert_de_tokens(&AttachmentFlags::empty(), &[Token::U64(1 << 63)]);
92    }
93}