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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use super::prelude::*;
use internal::RwLockExt;
#[cfg(all(feature = "model", feature = "utils"))]
use std::error::Error as StdError;
#[cfg(all(feature = "model", feature = "utils"))]
use std::result::Result as StdResult;
#[cfg(all(feature = "model", feature = "utils"))]
use std::str::FromStr;
#[cfg(all(feature = "model", feature = "utils"))]
use std::fmt;
#[cfg(all(feature = "model", any(feature = "cache", feature = "utils")))]
use utils;
pub trait Mentionable {
fn mention(&self) -> String;
}
impl Mentionable for ChannelId {
fn mention(&self) -> String { format!("<#{}>", self.0) }
}
impl Mentionable for Channel {
fn mention(&self) -> String {
match *self {
Channel::Guild(ref x) => format!("<#{}>", x.with(|x| x.id.0)),
Channel::Private(ref x) => format!("<#{}>", x.with(|x| x.id.0)),
Channel::Group(ref x) => format!("<#{}>", x.with(|x| x.channel_id.0)),
Channel::Category(_) => panic!("Categories can't be mentioned"),
}
}
}
impl Mentionable for Emoji {
fn mention(&self) -> String { format!("<:{}:{}>", self.name, self.id.0) }
}
impl Mentionable for Member {
fn mention(&self) -> String { format!("<@{}>", self.user.with(|u| u.id.0)) }
}
impl Mentionable for RoleId {
fn mention(&self) -> String { format!("<@&{}>", self.0) }
}
impl Mentionable for Role {
fn mention(&self) -> String { format!("<@&{}>", self.id.0) }
}
impl Mentionable for UserId {
fn mention(&self) -> String { format!("<@{}>", self.0) }
}
impl Mentionable for User {
fn mention(&self) -> String { format!("<@{}>", self.id.0) }
}
#[cfg(all(feature = "model", feature = "utils"))]
#[derive(Debug)]
pub enum UserParseError {
InvalidUsername,
Rest(Box<Error>),
}
#[cfg(all(feature = "model", feature = "utils"))]
impl fmt::Display for UserParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.description()) }
}
#[cfg(all(feature = "model", feature = "utils"))]
impl StdError for UserParseError {
fn description(&self) -> &str {
use self::UserParseError::*;
match *self {
InvalidUsername => "invalid username",
Rest(_) => "could not fetch",
}
}
}
#[cfg(all(feature = "model", feature = "utils"))]
impl FromStr for User {
type Err = UserParseError;
fn from_str(s: &str) -> StdResult<Self, Self::Err> {
match utils::parse_username(s) {
Some(x) => UserId(x as u64)
.get()
.map_err(|e| UserParseError::Rest(Box::new(e))),
_ => Err(UserParseError::InvalidUsername),
}
}
}
macro_rules! impl_from_str {
(id: $($id:tt, $err:ident;)*) => {
$(
#[cfg(all(feature = "model", feature = "utils"))]
#[derive(Debug)]
pub enum $err {
InvalidFormat,
}
#[cfg(all(feature = "model", feature = "utils"))]
impl fmt::Display for $err {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.description()) }
}
#[cfg(all(feature = "model", feature = "utils"))]
impl StdError for $err {
fn description(&self) -> &str {
use self::$err::*;
match *self {
InvalidFormat => "invalid id format",
}
}
}
#[cfg(all(feature = "model", feature = "utils"))]
impl FromStr for $id {
type Err = $err;
fn from_str(s: &str) -> StdResult<Self, Self::Err> {
Ok(match utils::parse_username(s) {
Some(id) => $id(id),
None => s.parse::<u64>().map($id).map_err(|_| $err::InvalidFormat)?,
})
}
}
)*
};
(struct: $($struct:ty, $id:tt, $err:ident, $invalid_variant:tt, $parse_fn:ident, $desc:expr;)*) => {
$(
#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
#[derive(Debug)]
pub enum $err {
NotPresentInCache,
$invalid_variant,
}
#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
impl fmt::Display for $err {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.description()) }
}
#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
impl StdError for $err {
fn description(&self) -> &str {
use self::$err::*;
match *self {
NotPresentInCache => "not present in cache",
$invalid_variant => $desc,
}
}
}
#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
impl FromStr for $struct {
type Err = $err;
fn from_str(s: &str) -> StdResult<Self, Self::Err> {
match utils::$parse_fn(s) {
Some(x) => match $id(x).find() {
Some(user) => Ok(user),
_ => Err($err::NotPresentInCache),
},
_ => Err($err::$invalid_variant),
}
}
}
)*
};
}
impl_from_str! { id:
UserId, UserIdParseError;
RoleId, RoleIdParseError;
ChannelId, ChannelIdParseError;
}
impl_from_str! { struct:
Channel, ChannelId, ChannelParseError, InvalidChannel, parse_channel, "invalid channel";
Role, RoleId, RoleParseError, InvalidRole, parse_role, "invalid role";
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct EmojiIdentifier {
pub id: EmojiId,
pub name: String,
}
#[cfg(all(feature = "model", feature = "utils"))]
impl EmojiIdentifier {
#[inline]
pub fn url(&self) -> String { format!(cdn!("/emojis/{}.png"), self.id) }
}
#[cfg(all(feature = "model", feature = "utils"))]
impl FromStr for EmojiIdentifier {
type Err = ();
fn from_str(s: &str) -> StdResult<Self, ()> { utils::parse_emoji(s).ok_or_else(|| ()) }
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct AffectedComponent {
pub name: String,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Incident {
pub created_at: String,
pub id: String,
pub impact: String,
pub incident_updates: Vec<IncidentUpdate>,
pub monitoring_at: Option<String>,
pub name: String,
pub page_id: String,
pub resolved_at: Option<String>,
pub short_link: String,
pub status: String,
pub updated_at: String,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct IncidentUpdate {
pub affected_components: Vec<AffectedComponent>,
pub body: String,
pub created_at: String,
pub display_at: String,
pub id: String,
pub incident_id: String,
pub status: IncidentStatus,
pub updated_at: String,
}
#[derive(Copy, Clone, Debug, Deserialize, Hash, Eq, PartialEq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum IncidentStatus {
Identified,
Investigating,
Monitoring,
Postmortem,
Resolved,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Maintenance {
pub description: String,
pub id: String,
pub name: String,
pub start: String,
pub stop: String,
}