twilight_cache_inmemory/model/
voice_state.rs1use serde::Serialize;
2use twilight_model::{
3 id::{
4 marker::{ChannelMarker, GuildMarker, UserMarker},
5 Id,
6 },
7 util::Timestamp,
8 voice::VoiceState,
9};
10
11use crate::CacheableVoiceState;
12
13#[allow(clippy::struct_excessive_bools)]
17#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
18pub struct CachedVoiceState {
19 channel_id: Id<ChannelMarker>,
20 deaf: bool,
21 guild_id: Id<GuildMarker>,
22 mute: bool,
23 request_to_speak_timestamp: Option<Timestamp>,
24 self_deaf: bool,
25 self_mute: bool,
26 self_stream: bool,
27 self_video: bool,
28 session_id: String,
29 suppress: bool,
30 user_id: Id<UserMarker>,
31}
32
33impl CachedVoiceState {
34 pub const fn channel_id(&self) -> Id<ChannelMarker> {
36 self.channel_id
37 }
38
39 pub const fn deaf(&self) -> bool {
41 self.deaf
42 }
43
44 pub const fn guild_id(&self) -> Id<GuildMarker> {
46 self.guild_id
47 }
48
49 pub const fn mute(&self) -> bool {
51 self.mute
52 }
53
54 pub const fn request_to_speak_timestamp(&self) -> Option<Timestamp> {
56 self.request_to_speak_timestamp
57 }
58
59 pub const fn self_deaf(&self) -> bool {
61 self.self_deaf
62 }
63
64 pub const fn self_mute(&self) -> bool {
66 self.self_mute
67 }
68
69 pub const fn self_stream(&self) -> bool {
71 self.self_stream
72 }
73
74 pub const fn self_video(&self) -> bool {
76 self.self_video
77 }
78
79 pub fn session_id(&self) -> &str {
81 &self.session_id
82 }
83
84 pub const fn suppress(&self) -> bool {
86 self.suppress
87 }
88
89 pub const fn user_id(&self) -> Id<UserMarker> {
91 self.user_id
92 }
93}
94
95impl From<(Id<ChannelMarker>, Id<GuildMarker>, VoiceState)> for CachedVoiceState {
96 fn from(
97 (channel_id, guild_id, voice_state): (Id<ChannelMarker>, Id<GuildMarker>, VoiceState),
98 ) -> Self {
99 let VoiceState {
105 channel_id: _,
106 deaf,
107 guild_id: _,
108 member: _,
109 mute,
110 self_deaf,
111 self_mute,
112 self_stream,
113 self_video,
114 session_id,
115 suppress,
116 user_id,
117 request_to_speak_timestamp,
118 } = voice_state;
119
120 Self {
121 channel_id,
122 deaf,
123 guild_id,
124 mute,
125 request_to_speak_timestamp,
126 self_deaf,
127 self_mute,
128 self_stream,
129 self_video,
130 session_id,
131 suppress,
132 user_id,
133 }
134 }
135}
136
137impl PartialEq<VoiceState> for CachedVoiceState {
138 fn eq(&self, other: &VoiceState) -> bool {
139 Some(self.channel_id) == other.channel_id
140 && self.deaf == other.deaf
141 && Some(self.guild_id) == other.guild_id
142 && self.mute == other.mute
143 && self.request_to_speak_timestamp == other.request_to_speak_timestamp
144 && self.self_deaf == other.self_deaf
145 && self.self_mute == other.self_mute
146 && self.self_stream == other.self_stream
147 && self.self_video == other.self_video
148 && self.session_id == other.session_id
149 && self.suppress == other.suppress
150 && self.user_id == other.user_id
151 }
152}
153
154impl CacheableVoiceState for CachedVoiceState {
155 fn channel_id(&self) -> Id<ChannelMarker> {
156 self.channel_id
157 }
158}
159
160#[cfg(test)]
161mod tests {
162 use super::CachedVoiceState;
163 use crate::test;
164 use serde::Serialize;
165 use static_assertions::{assert_fields, assert_impl_all};
166 use std::fmt::Debug;
167 use twilight_model::{
168 id::{
169 marker::{ChannelMarker, GuildMarker, UserMarker},
170 Id,
171 },
172 voice::VoiceState,
173 };
174
175 assert_fields!(
176 CachedVoiceState: channel_id,
177 deaf,
178 guild_id,
179 mute,
180 request_to_speak_timestamp,
181 self_deaf,
182 self_mute,
183 self_stream,
184 self_video,
185 session_id,
186 suppress,
187 user_id
188 );
189 assert_impl_all!(
190 CachedVoiceState: Clone,
191 Debug,
192 Eq,
193 PartialEq,
194 PartialEq<VoiceState>,
195 Serialize,
196 );
197
198 const CHANNEL_ID: Id<ChannelMarker> = Id::new(1);
199 const GUILD_ID: Id<GuildMarker> = Id::new(2);
200 const USER_ID: Id<UserMarker> = Id::new(3);
201
202 #[test]
203 fn eq() {
204 let voice_state = test::voice_state(GUILD_ID, Some(CHANNEL_ID), USER_ID);
205 let cached = CachedVoiceState::from((CHANNEL_ID, GUILD_ID, voice_state.clone()));
206
207 assert_eq!(cached, voice_state);
208 }
209
210 #[test]
211 fn getters() {
212 let voice_state = test::voice_state(GUILD_ID, Some(CHANNEL_ID), USER_ID);
213 let cached = CachedVoiceState::from((CHANNEL_ID, GUILD_ID, voice_state.clone()));
214
215 assert_eq!(Some(cached.channel_id()), voice_state.channel_id);
216 assert_eq!(cached.deaf(), voice_state.deaf);
217 assert_eq!(Some(cached.guild_id()), voice_state.guild_id);
218 assert_eq!(cached.mute(), voice_state.mute);
219 assert_eq!(
220 cached.request_to_speak_timestamp(),
221 voice_state.request_to_speak_timestamp
222 );
223 assert_eq!(cached.self_deaf(), voice_state.self_deaf);
224 assert_eq!(cached.self_mute(), voice_state.self_mute);
225 assert_eq!(cached.self_stream(), voice_state.self_stream);
226 assert_eq!(cached.self_video(), voice_state.self_video);
227 assert_eq!(cached.session_id(), voice_state.session_id);
228 assert_eq!(cached.suppress(), voice_state.suppress);
229 assert_eq!(cached.user_id(), voice_state.user_id);
230 }
231}