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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#![cfg_attr(feature = "db", feature(once_cell))]
#![cfg_attr(any(feature = "auth", feature = "db"), feature(is_some_and))]
#![cfg_attr(feature = "async-trait", feature(async_fn_in_trait))]
#![cfg_attr(feature = "async-trait", allow(incomplete_features))]
#![allow(
clippy::module_name_repetitions,
clippy::cast_possible_truncation,
clippy::cast_lossless,
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
clippy::missing_errors_doc,
clippy::missing_panics_doc,
clippy::doc_markdown
)]
#[cfg(any(feature = "auth", feature = "token-parsing"))]
pub mod auth;
#[cfg(feature = "db")]
pub mod cache;
#[cfg(feature = "db")]
pub mod db;
pub mod error;
pub mod http;
mod maybe;
pub mod models;
mod permissions;
#[cfg(feature = "snowflakes")]
pub mod snowflake;
pub mod ws;
pub use error::{Error, NotFoundExt, Result};
pub use maybe::Maybe;
pub use permissions::{calculate_permissions, calculate_permissions_sorted};
#[cfg(feature = "utoipa")]
pub use utoipa;
#[macro_export]
macro_rules! serde_for_bitflags {
(@openapi for $t:ty => $format:ident) => {
#[cfg(feature = "utoipa")]
impl utoipa::ToSchema<'static> for $t {
fn schema() -> (&'static str, utoipa::openapi::RefOr<utoipa::openapi::Schema>) {
(
"bitflags",
utoipa::openapi::RefOr::T(
utoipa::openapi::ObjectBuilder::new()
.schema_type(utoipa::openapi::SchemaType::Integer)
.format(Some(utoipa::openapi::SchemaFormat::KnownFormat(
utoipa::openapi::KnownFormat::$format,
)))
.build()
.into(),
)
)
}
}
};
(u32: $t:ty) => {
use ::std::result::Result;
impl serde::Serialize for $t {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_u32(self.bits)
}
}
impl<'de> serde::Deserialize<'de> for $t {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let raw = u32::deserialize(deserializer)?;
Self::from_bits(raw).ok_or(serde::de::Error::custom(format!(
"invalid bitflags value: {} (expected an integer between {} and {})",
raw,
0,
Self::all().bits(),
)))
}
}
serde_for_bitflags!(@openapi for $t => Int32);
};
(i64: $t:ty) => {
impl serde::Serialize for $t {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_i64(self.bits)
}
}
impl<'de> serde::Deserialize<'de> for $t {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let raw = i64::deserialize(deserializer)?;
let max = Self::all().bits();
let (min, max) = if max > 0 { (0, max) } else { (i64::MIN, i64::MAX) };
Self::from_bits(raw).ok_or(serde::de::Error::custom(format!(
"invalid bitflags value: {} (expected an integer between {} and {})",
raw,
min,
max,
)))
}
}
serde_for_bitflags!(@openapi for $t => Int64);
};
}
#[macro_export]
macro_rules! builder_methods {
($($attr:ident: $t:ty => $name:ident $(+ $modifier:ident)?),+ $(,)?) => {
$(
#[doc = concat!("Changes the ``", stringify!($attr), "`` attribute.")]
pub fn $name(&mut self, $attr: $t) -> &mut Self {
self.$attr = $($modifier)? ($attr);
self
}
)+
};
}