-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathflags.rs
111 lines (107 loc) · 3.65 KB
/
flags.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use bitflags::bitflags;
use serde::{Deserialize, Serialize};
bitflags! {
/// Flags to signal state and modify the look of a message.
#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Deserialize, Serialize)]
#[serde(transparent)]
pub struct MessageFlags: u64 {
/// Has been published to subscribed channels via Channel Following.
const CROSSPOSTED = 1;
/// Is a crosspost from another channel via Channel Following.
const IS_CROSSPOST = 1 << 1;
/// Do not include any embeds when serializing this message.
const SUPPRESS_EMBEDS = 1 << 2;
/// Source message for this message has been deleted via Channel
/// Following.
const SOURCE_MESSAGE_DELETED = 1 << 3;
/// Comes from the urgent message system.
const URGENT = 1 << 4;
/// A thread has been started from this message.
const HAS_THREAD = 1 << 5;
/// When used, only shows a message to the invoking user.
///
/// Used when responding to an [`Interaction`].
///
/// [`Interaction`]: crate::application::interaction::Interaction
const EPHEMERAL = 1 << 6;
/// This message is an Interaction Response, and the bot is "thinking".
const LOADING = 1 << 7;
/// This message failed to mention some roles in a thread, which
/// subsequently failed to add the role's members to the thread.
const FAILED_TO_MENTION_SOME_ROLES_IN_THREAD = 1 << 8;
}
}
#[cfg(test)]
mod tests {
use super::MessageFlags;
use serde::{Deserialize, Serialize};
use serde_test::Token;
use static_assertions::{assert_impl_all, const_assert_eq};
use std::{
fmt::{Binary, Debug, LowerHex, Octal, UpperHex},
hash::Hash,
ops::{
BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Sub, SubAssign,
},
};
assert_impl_all!(
MessageFlags: Binary,
BitAnd,
BitAndAssign,
BitOr,
BitOrAssign,
BitXor,
BitXorAssign,
Clone,
Copy,
Debug,
Deserialize<'static>,
Eq,
Extend<MessageFlags>,
FromIterator<MessageFlags>,
Hash,
LowerHex,
Not,
Octal,
Ord,
PartialEq,
PartialOrd,
Send,
Serialize,
Sub,
SubAssign,
Sync,
UpperHex
);
const_assert_eq!(MessageFlags::CROSSPOSTED.bits(), 1);
const_assert_eq!(MessageFlags::IS_CROSSPOST.bits(), 1 << 1);
const_assert_eq!(MessageFlags::SUPPRESS_EMBEDS.bits(), 1 << 2);
const_assert_eq!(MessageFlags::SOURCE_MESSAGE_DELETED.bits(), 1 << 3);
const_assert_eq!(MessageFlags::URGENT.bits(), 1 << 4);
const_assert_eq!(MessageFlags::HAS_THREAD.bits(), 1 << 5);
const_assert_eq!(MessageFlags::EPHEMERAL.bits(), 1 << 6);
const_assert_eq!(MessageFlags::LOADING.bits(), 1 << 7);
const_assert_eq!(
MessageFlags::FAILED_TO_MENTION_SOME_ROLES_IN_THREAD.bits(),
1 << 8
);
#[test]
fn serde() {
serde_test::assert_tokens(
&MessageFlags::CROSSPOSTED,
&[Token::U64(MessageFlags::CROSSPOSTED.bits())],
);
// Safety:
//
// Deserialization doesn't truncate unknown bits.
//
// `bitflags` requires unsafe code to create bitflags with unknown bits
// due to an unorthodox definition of unsafe:
//
// <https://github.com/bitflags/bitflags/issues/262>
#[allow(unsafe_code)]
let value = unsafe { MessageFlags::from_bits_unchecked(1 << 63) };
serde_test::assert_de_tokens(&value, &[Token::U64(1 << 63)]);
}
}