twilight_cache_inmemory/model/
emoji.rs1use crate::CacheableEmoji;
2use serde::Serialize;
3use twilight_model::{
4 guild::Emoji,
5 id::{
6 marker::{EmojiMarker, RoleMarker, UserMarker},
7 Id,
8 },
9};
10
11#[allow(clippy::struct_excessive_bools)]
15#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
16pub struct CachedEmoji {
17 pub(crate) animated: bool,
18 pub(crate) available: bool,
19 pub(crate) id: Id<EmojiMarker>,
20 pub(crate) managed: bool,
21 pub(crate) name: String,
22 pub(crate) require_colons: bool,
23 pub(crate) roles: Vec<Id<RoleMarker>>,
24 pub(crate) user_id: Option<Id<UserMarker>>,
25}
26
27impl CachedEmoji {
28 pub const fn available(&self) -> bool {
32 self.available
33 }
34
35 pub const fn animated(&self) -> bool {
37 self.animated
38 }
39
40 pub const fn id(&self) -> Id<EmojiMarker> {
42 self.id
43 }
44
45 pub const fn managed(&self) -> bool {
47 self.managed
48 }
49
50 pub fn name(&self) -> &str {
52 &self.name
53 }
54
55 pub const fn require_colons(&self) -> bool {
57 self.require_colons
58 }
59
60 pub fn roles(&self) -> &[Id<RoleMarker>] {
62 &self.roles
63 }
64
65 pub const fn user_id(&self) -> Option<Id<UserMarker>> {
67 self.user_id
68 }
69}
70
71impl From<Emoji> for CachedEmoji {
72 fn from(emoji: Emoji) -> Self {
73 let Emoji {
74 animated,
75 available,
76 id,
77 managed,
78 name,
79 require_colons,
80 roles,
81 user,
82 } = emoji;
83
84 Self {
85 animated,
86 available,
87 id,
88 managed,
89 name,
90 require_colons,
91 roles,
92 user_id: user.map(|user| user.id),
93 }
94 }
95}
96
97impl PartialEq<Emoji> for CachedEmoji {
98 fn eq(&self, other: &Emoji) -> bool {
99 self.id == other.id
100 && self.animated == other.animated
101 && self.managed == other.managed
102 && self.name == other.name
103 && self.require_colons == other.require_colons
104 && self.roles == other.roles
105 && self.user_id == other.user.as_ref().map(|user| user.id)
106 && self.available == other.available
107 }
108}
109
110impl CacheableEmoji for CachedEmoji {}
111
112#[cfg(test)]
113mod tests {
114 use super::CachedEmoji;
115 use serde::Serialize;
116 use static_assertions::{assert_fields, assert_impl_all};
117 use std::fmt::Debug;
118 use twilight_model::{guild::Emoji, id::Id};
119
120 assert_fields!(
121 CachedEmoji: id,
122 animated,
123 name,
124 managed,
125 require_colons,
126 roles,
127 user_id
128 );
129 assert_impl_all!(
130 CachedEmoji: Clone,
131 Debug,
132 Eq,
133 PartialEq,
134 PartialEq<Emoji>,
135 Send,
136 Serialize,
137 Sync
138 );
139
140 #[test]
141 fn eq_emoji() {
142 let emoji = Emoji {
143 id: Id::new(123),
144 animated: true,
145 name: "foo".to_owned(),
146 managed: false,
147 require_colons: true,
148 roles: vec![],
149 user: None,
150 available: true,
151 };
152 let cached = CachedEmoji {
153 id: Id::new(123),
154 animated: true,
155 name: "foo".to_owned(),
156 managed: false,
157 require_colons: true,
158 roles: vec![],
159 user_id: None,
160 available: true,
161 };
162
163 assert_eq!(cached, emoji);
164 }
165}