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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
use std::fmt;
use chrono::{DateTime, Utc};
use serde::de::{self, Deserialize, Deserializer, IgnoredAny, MapAccess, Visitor};
use super::{
guild::Member,
id::{ChannelId, GuildId, RoleId, UserId},
user::User,
};
#[cfg(feature = "unstable_discord_api")]
use crate::model::permissions::Permissions;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct VoiceRegion {
pub custom: bool,
pub deprecated: bool,
pub id: String,
pub name: String,
pub optimal: bool,
pub vip: bool,
}
#[derive(Clone, Serialize)]
#[non_exhaustive]
pub struct VoiceState {
pub channel_id: Option<ChannelId>,
pub deaf: bool,
pub guild_id: Option<GuildId>,
pub member: Option<Member>,
pub mute: bool,
pub self_deaf: bool,
pub self_mute: bool,
pub self_stream: Option<bool>,
pub self_video: bool,
pub session_id: String,
pub suppress: bool,
pub token: Option<String>,
pub user_id: UserId,
pub request_to_speak_timestamp: Option<DateTime<Utc>>,
}
impl fmt::Debug for VoiceState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("VoiceState")
.field("channel_id", &self.channel_id)
.field("deaf", &self.deaf)
.field("guild_id", &self.guild_id)
.field("member", &self.member)
.field("mute", &self.mute)
.field("self_deaf", &self.self_deaf)
.field("self_mute", &self.self_mute)
.field("self_stream", &self.self_stream)
.field("self_video", &self.self_video)
.field("session_id", &self.session_id)
.field("suppress", &self.suppress)
.field("user_id", &self.user_id)
.field("request_to_speak_timestamp", &self.request_to_speak_timestamp)
.finish()
}
}
impl<'de> Deserialize<'de> for VoiceState {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
#[derive(Deserialize)]
#[serde(field_identifier, rename_all = "snake_case")]
enum Field {
ChannelId,
Deaf,
GuildId,
Member,
Mute,
SelfDeaf,
SelfMute,
SelfStream,
SelfVideo,
SessionId,
Suppress,
Token,
UserId,
RequestToSpeakTimestamp,
}
#[derive(Deserialize)]
#[non_exhaustive]
struct PartialMember {
deaf: bool,
joined_at: Option<DateTime<Utc>>,
mute: bool,
nick: Option<String>,
roles: Vec<RoleId>,
user: User,
#[serde(default)]
pending: bool,
premium_since: Option<DateTime<Utc>>,
#[cfg(feature = "unstable_discord_api")]
permissions: Option<Permissions>,
avatar: Option<String>,
communication_disabled_until: Option<DateTime<Utc>>,
}
struct VoiceStateVisitor;
impl<'de> Visitor<'de> for VoiceStateVisitor {
type Value = VoiceState;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("struct VoiceState")
}
fn visit_map<V: MapAccess<'de>>(self, mut map: V) -> Result<Self::Value, V::Error> {
let mut channel_id = None;
let mut deaf = None;
let mut guild_id = None;
let mut member = None;
let mut mute = None;
let mut self_deaf = None;
let mut self_mute = None;
let mut self_stream = None;
let mut self_video = None;
let mut session_id = None;
let mut suppress = None;
let mut token = None;
let mut user_id = None;
let mut request_to_speak_timestamp = None;
loop {
let key = match map.next_key() {
Ok(Some(key)) => key,
Ok(None) => break,
Err(_) => {
map.next_value::<IgnoredAny>()?;
continue;
},
};
match key {
Field::ChannelId => {
if channel_id.is_some() {
return Err(de::Error::duplicate_field("channel_id"));
}
channel_id = map.next_value()?;
},
Field::Deaf => {
if deaf.is_some() {
return Err(de::Error::duplicate_field("deaf"));
}
deaf = Some(map.next_value()?);
},
Field::GuildId => {
if guild_id.is_some() {
return Err(de::Error::duplicate_field("guild_id"));
}
guild_id = map.next_value()?;
},
Field::Member => {
if member.is_some() {
return Err(de::Error::duplicate_field("member"));
}
let partial_member: Option<PartialMember> = map.next_value()?;
if let Some(partial_member) = partial_member {
member = Some(Member {
deaf: partial_member.deaf,
guild_id: GuildId(0),
joined_at: partial_member.joined_at,
mute: partial_member.mute,
nick: partial_member.nick,
roles: partial_member.roles,
user: partial_member.user,
pending: partial_member.pending,
premium_since: partial_member.premium_since,
#[cfg(feature = "unstable_discord_api")]
permissions: partial_member.permissions,
avatar: partial_member.avatar,
communication_disabled_until: partial_member
.communication_disabled_until,
});
}
},
Field::Mute => {
if mute.is_some() {
return Err(de::Error::duplicate_field("mute"));
}
mute = Some(map.next_value()?);
},
Field::SelfDeaf => {
if self_deaf.is_some() {
return Err(de::Error::duplicate_field("self_deaf"));
}
self_deaf = Some(map.next_value()?);
},
Field::SelfMute => {
if self_mute.is_some() {
return Err(de::Error::duplicate_field("self_mute"));
}
self_mute = Some(map.next_value()?);
},
Field::SelfStream => {
if self_stream.is_some() {
return Err(de::Error::duplicate_field("self_stream"));
}
self_stream = map.next_value()?;
},
Field::SelfVideo => {
if self_video.is_some() {
return Err(de::Error::duplicate_field("self_video"));
}
self_video = Some(map.next_value()?);
},
Field::SessionId => {
if session_id.is_some() {
return Err(de::Error::duplicate_field("session_id"));
}
session_id = Some(map.next_value()?);
},
Field::Suppress => {
if suppress.is_some() {
return Err(de::Error::duplicate_field("suppress"));
}
suppress = Some(map.next_value()?);
},
Field::Token => {
if token.is_some() {
return Err(de::Error::duplicate_field("token"));
}
token = map.next_value()?;
},
Field::UserId => {
if user_id.is_some() {
return Err(de::Error::duplicate_field("user_id"));
}
user_id = Some(map.next_value()?);
},
Field::RequestToSpeakTimestamp => {
if request_to_speak_timestamp.is_some() {
return Err(de::Error::duplicate_field(
"request_to_speak_timestamp",
));
}
request_to_speak_timestamp = Some(map.next_value()?);
},
}
}
let deaf = deaf.ok_or_else(|| de::Error::missing_field("deaf"))?;
let mute = mute.ok_or_else(|| de::Error::missing_field("mute"))?;
let self_deaf = self_deaf.ok_or_else(|| de::Error::missing_field("self_deaf"))?;
let self_mute = self_mute.ok_or_else(|| de::Error::missing_field("self_mute"))?;
let self_video =
self_video.ok_or_else(|| de::Error::missing_field("self_video"))?;
let session_id =
session_id.ok_or_else(|| de::Error::missing_field("session_id"))?;
let suppress = suppress.ok_or_else(|| de::Error::missing_field("suppress"))?;
let user_id = user_id.ok_or_else(|| de::Error::missing_field("user_id"))?;
let request_to_speak_timestamp = request_to_speak_timestamp.unwrap_or(None);
if let (Some(guild_id), Some(member)) = (guild_id, member.as_mut()) {
member.guild_id = guild_id;
}
Ok(VoiceState {
channel_id,
deaf,
guild_id,
member,
mute,
self_deaf,
self_mute,
self_stream,
self_video,
session_id,
suppress,
token,
user_id,
request_to_speak_timestamp,
})
}
}
const FIELDS: &[&str] = &[
"channel_id",
"deaf",
"guild_id",
"member",
"mute",
"self_deaf",
"self_mute",
"self_stream",
"self_video",
"session_id",
"suppress",
"token",
"user_id",
"request_to_speak_timestamp",
];
deserializer.deserialize_struct("VoiceState", FIELDS, VoiceStateVisitor)
}
}