djs_model/gateway/payload/incoming/
mod.rs1mod channel;
10mod guild;
11mod message;
12
13use compact_str::CompactString;
14use serde::{Deserialize, Serialize};
15
16pub use self::{channel::*, guild::*, message::*};
17use crate::{
18 application::ApplicationFlags,
19 gateway::{OpCode, ShardId},
20 guild::UnavailableGuild,
21 id::{ApplicationId, GuildId},
22 user::CurrentUser,
23};
24
25macro_rules! newtype_event {
28 ($(#[$meta:meta])* $name:ident($inner:ty)) => {
29 $(#[$meta])*
30 #[doc = concat!("Dereferences to the contained [`", stringify!($inner), "`].")]
32 #[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
33 #[serde(transparent)]
34 pub struct $name(pub $inner);
35
36 impl core::ops::Deref for $name {
37 type Target = $inner;
38
39 fn deref(&self) -> &$inner {
40 &self.0
41 }
42 }
43
44 impl core::ops::DerefMut for $name {
45 fn deref_mut(&mut self) -> &mut $inner {
46 &mut self.0
47 }
48 }
49
50 impl From<$name> for $inner {
51 fn from(event: $name) -> Self {
52 event.0
53 }
54 }
55 };
56}
57
58use newtype_event;
59
60#[derive(Default)]
62struct GuildIdExtra {
63 guild_id: Option<GuildId>,
64}
65
66impl<'de> crate::serde_util::extras::Extras<'de> for GuildIdExtra {
67 fn visit<A: serde::de::MapAccess<'de>>(
68 &mut self,
69 key: &str,
70 map: &mut A,
71 ) -> Result<crate::serde_util::extras::Visit, A::Error> {
72 if key == "guild_id" {
73 self.guild_id = Some(map.next_value()?);
74 Ok(crate::serde_util::extras::Visit::Consumed)
75 } else {
76 Ok(crate::serde_util::extras::Visit::Pass)
77 }
78 }
79}
80
81macro_rules! guild_id_event {
84 ($(#[$meta:meta])* $name:ident { $(#[$field_meta:meta])* $field:ident: $inner:ty }) => {
85 $(#[$meta])*
86 #[doc = concat!("Dereferences to the contained [`", stringify!($inner), "`].")]
88 #[derive(Clone, Debug, PartialEq)]
89 #[non_exhaustive]
90 pub struct $name {
91 pub guild_id: crate::id::GuildId,
93 $(#[$field_meta])*
94 pub $field: $inner,
95 }
96
97 impl core::ops::Deref for $name {
98 type Target = $inner;
99
100 fn deref(&self) -> &$inner {
101 &self.$field
102 }
103 }
104
105 impl core::ops::DerefMut for $name {
106 fn deref_mut(&mut self) -> &mut $inner {
107 &mut self.$field
108 }
109 }
110
111 impl<'de> serde::Deserialize<'de> for $name {
112 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
113 use serde::de::Error as _;
114
115 let (base, extra) = crate::serde_util::extras::deserialize::<
116 $inner,
117 super::GuildIdExtra,
118 D,
119 >(deserializer)?;
120 Ok(Self {
121 guild_id: extra
122 .guild_id
123 .ok_or_else(|| D::Error::missing_field("guild_id"))?,
124 $field: base.ok_or_else(|| D::Error::custom("missing event data"))?,
125 })
126 }
127 }
128
129 impl serde::Serialize for $name {
130 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
131 #[derive(serde::Serialize)]
132 struct Flat<'a> {
133 guild_id: crate::id::GuildId,
134 #[serde(flatten)]
135 $field: &'a $inner,
136 }
137
138 Flat {
139 guild_id: self.guild_id,
140 $field: &self.$field,
141 }
142 .serialize(serializer)
143 }
144 }
145 };
146}
147
148use guild_id_event;
149
150#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
155#[non_exhaustive]
156pub struct Hello {
157 pub heartbeat_interval: u64,
159}
160
161#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
169#[non_exhaustive]
170pub struct Ready {
171 #[serde(rename = "v")]
173 pub version: u8,
174 pub user: CurrentUser,
176 pub guilds: Vec<UnavailableGuild>,
179 pub session_id: CompactString,
181 pub resume_gateway_url: CompactString,
183 #[serde(default, skip_serializing_if = "Option::is_none")]
185 pub shard: Option<ShardId>,
186 pub application: ReadyApplication,
188}
189
190#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
194#[non_exhaustive]
195pub struct ReadyApplication {
196 pub id: ApplicationId,
198 #[serde(default)]
200 pub flags: ApplicationFlags,
201 #[serde(
203 default,
204 skip_serializing_if = "Option::is_none",
205 with = "crate::serde_util::option_flags_string"
206 )]
207 pub flags_new: Option<ApplicationFlags>,
208}
209
210#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
215#[non_exhaustive]
216pub struct RateLimited {
217 pub opcode: OpCode,
219 pub retry_after: f64,
221 #[serde(default, skip_serializing_if = "Option::is_none")]
224 pub meta: Option<RateLimitMetadata>,
225}
226
227#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
231#[non_exhaustive]
232pub struct RateLimitMetadata {
233 pub guild_id: GuildId,
235 #[serde(default, skip_serializing_if = "Option::is_none")]
237 pub nonce: Option<CompactString>,
238}