-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathsticker.rs
252 lines (228 loc) · 6.83 KB
/
sticker.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
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
use serde::Serialize;
use twilight_model::{
channel::message::{
sticker::{StickerFormatType, StickerType},
Sticker,
},
id::{
marker::{GuildMarker, StickerMarker, StickerPackMarker, UserMarker},
Id,
},
};
use crate::CacheableSticker;
/// Representation of a cached [`Sticker`].
///
/// [`Sticker`]: twilight_model::channel::message::sticker::Sticker
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct CachedSticker {
/// Whether the sticker is available.
pub(crate) available: bool,
/// Description of the sticker.
pub(crate) description: String,
/// Format type.
pub(crate) format_type: StickerFormatType,
/// ID of the guild that owns the sticker.
pub(crate) guild_id: Option<Id<GuildMarker>>,
/// Unique ID of the sticker.
pub(crate) id: Id<StickerMarker>,
/// Kind of sticker.
pub(crate) kind: StickerType,
/// Name of the sticker.
pub(crate) name: String,
/// Unique ID of the pack the sticker is in.
pub(crate) pack_id: Option<Id<StickerPackMarker>>,
/// Sticker's sort order within a pack.
pub(crate) sort_value: Option<u64>,
/// CSV list of tags the sticker is assigned to, if any.
pub(crate) tags: String,
/// ID of the user that uploaded the sticker.
pub(crate) user_id: Option<Id<UserMarker>>,
}
impl CachedSticker {
/// Whether the sticker is available.
pub const fn available(&self) -> bool {
self.available
}
/// Description of the sticker.
pub fn description(&self) -> &str {
&self.description
}
/// Format type.
pub const fn format_type(&self) -> StickerFormatType {
self.format_type
}
/// ID of the guild that owns the sticker.
pub const fn guild_id(&self) -> Option<Id<GuildMarker>> {
self.guild_id
}
/// Unique ID of the sticker.
pub const fn id(&self) -> Id<StickerMarker> {
self.id
}
/// Kind of sticker.
pub const fn kind(&self) -> StickerType {
self.kind
}
/// Name of the sticker.
pub fn name(&self) -> &str {
&self.name
}
/// Unique ID of the pack the sticker is in.
pub const fn pack_id(&self) -> Option<Id<StickerPackMarker>> {
self.pack_id
}
/// Sticker's sort order within a pack.
pub const fn sort_value(&self) -> Option<u64> {
self.sort_value
}
/// CSV list of tags the sticker is assigned to, if any.
pub fn tags(&self) -> &str {
&self.tags
}
/// ID of the user that uploaded the sticker.
pub const fn user_id(&self) -> Option<Id<UserMarker>> {
self.user_id
}
}
impl From<Sticker> for CachedSticker {
fn from(sticker: Sticker) -> Self {
let Sticker {
available,
description,
format_type,
guild_id,
id,
kind,
name,
pack_id,
sort_value,
tags,
user,
} = sticker;
Self {
available,
description: description.unwrap_or_default(),
format_type,
guild_id,
id,
kind,
name,
pack_id,
sort_value,
tags,
user_id: user.map(|user| user.id),
}
}
}
impl PartialEq<Sticker> for CachedSticker {
fn eq(&self, other: &Sticker) -> bool {
self.available == other.available
&& self.description.as_str() == other.description.as_ref().map_or("", String::as_str)
&& self.format_type == other.format_type
&& self.guild_id == other.guild_id
&& self.id == other.id
&& self.kind == other.kind
&& self.name == other.name
&& self.pack_id == other.pack_id
&& self.sort_value == other.sort_value
&& self.tags == other.tags
&& self.user_id == other.user.as_ref().map(|user| user.id)
}
}
impl CacheableSticker for CachedSticker {
fn id(&self) -> Id<StickerMarker> {
self.id
}
}
#[cfg(test)]
mod tests {
use super::CachedSticker;
use serde::Serialize;
use static_assertions::{assert_fields, assert_impl_all};
use std::fmt::Debug;
use twilight_model::{
channel::message::{
sticker::{StickerFormatType, StickerType},
Sticker,
},
id::Id,
user::{PremiumType, User, UserFlags},
util::{image_hash::ImageHashParseError, ImageHash},
};
assert_fields!(
CachedSticker: available,
description,
format_type,
guild_id,
id,
kind,
name,
pack_id,
sort_value,
tags,
user_id
);
assert_impl_all!(
CachedSticker: Clone,
Debug,
Eq,
PartialEq,
PartialEq<Sticker>,
Send,
Serialize,
Sync
);
#[test]
fn eq_sticker() -> Result<(), ImageHashParseError> {
let avatar = ImageHash::parse(b"5bf451026c107906b4dccea015320222")?;
let sticker = Sticker {
available: true,
description: Some("sticker".into()),
format_type: StickerFormatType::Png,
guild_id: Some(Id::new(1)),
id: Id::new(2),
kind: StickerType::Guild,
name: "stick".into(),
pack_id: Some(Id::new(3)),
sort_value: Some(1),
tags: "foo,bar,baz".into(),
user: Some(User {
accent_color: None,
avatar: Some(avatar),
avatar_decoration: None,
avatar_decoration_data: None,
banner: None,
bot: false,
discriminator: 1,
email: Some("address@example.com".to_owned()),
flags: Some(UserFlags::PREMIUM_EARLY_SUPPORTER | UserFlags::VERIFIED_DEVELOPER),
global_name: Some("test".to_owned()),
id: Id::new(1),
locale: Some("en-us".to_owned()),
mfa_enabled: Some(true),
name: "test".to_owned(),
premium_type: Some(PremiumType::Nitro),
public_flags: Some(
UserFlags::PREMIUM_EARLY_SUPPORTER | UserFlags::VERIFIED_DEVELOPER,
),
system: Some(true),
verified: Some(true),
}),
};
let cached = CachedSticker {
available: true,
description: "sticker".into(),
format_type: StickerFormatType::Png,
guild_id: Some(Id::new(1)),
id: Id::new(2),
kind: StickerType::Guild,
name: "stick".into(),
pack_id: Some(Id::new(3)),
sort_value: Some(1),
tags: "foo,bar,baz".into(),
user_id: Some(Id::new(1)),
};
assert_eq!(cached, sticker);
Ok(())
}
}