pub struct GatewayEventDeserializer<'a> { /* private fields */ }Expand description
A DeserializeSeed that decodes a gateway payload into a GatewayEvent.
Gateway payloads are envelopes — {"op": 0, "s": 42, "t": "MESSAGE_CREATE", "d": {…}} — whose
d type depends on op and t. Deserializing them naively requires buffering d in a
generic value first. Instead, this type is constructed with op, s and t known up front
(usually via from_json, which scans only the top-level keys), so d is
decoded in a single pass directly into its concrete type, and unhandled events are skipped
without allocating.
It works with any serde data format; from_json is a JSON-specific
helper.
discord.js counterpart: none (the decoding step discord.js leaves to JSON.parse).
§Examples
use djs_model::gateway::{GatewayEvent, GatewayEventDeserializer, OpCode};
use serde::de::DeserializeSeed;
let json = r#"{"t":null,"s":null,"op":10,"d":{"heartbeat_interval":41250}}"#;
let seed = GatewayEventDeserializer::from_json(json).unwrap();
assert_eq!(seed.op(), OpCode::Hello);
let mut deserializer = serde_json::Deserializer::from_str(json);
let event = seed.deserialize(&mut deserializer).unwrap();
assert!(matches!(event, GatewayEvent::Hello(hello) if hello.heartbeat_interval == 41250));Implementations§
Source§impl<'a> GatewayEventDeserializer<'a>
impl<'a> GatewayEventDeserializer<'a>
Sourcepub const fn new(
op: OpCode,
sequence: Option<u64>,
event_type: Option<&'a str>,
) -> Self
pub const fn new( op: OpCode, sequence: Option<u64>, event_type: Option<&'a str>, ) -> Self
Creates a deserializer from an already known opcode, sequence number and event name.
The sequence is used when the payload being deserialized has no s field; an s field
in the payload takes precedence.
discord.js counterpart: none (discord.js leaves decoding to JSON.parse).
Sourcepub fn from_json(json: &'a str) -> Option<Self>
pub fn from_json(json: &'a str) -> Option<Self>
Reads op, s and t from the top level of a JSON gateway payload.
Only the top-level object is scanned; nested values are skipped without being parsed.
Discord sends t, s and op before d, in which case scanning stops before reaching
d at all. Keys in any order are still handled correctly.
Returns None if the input is not a JSON object with an integer op field, or if t
contains escape sequences (event names never do).
discord.js counterpart: none (discord.js leaves decoding to JSON.parse).
Sourcepub const fn op(&self) -> OpCode
pub const fn op(&self) -> OpCode
Returns the opcode.
discord.js counterpart: the op of GatewayReceivePayload (discord-api-types).
Sourcepub const fn sequence(&self) -> Option<u64>
pub const fn sequence(&self) -> Option<u64>
Returns the sequence number, if known.
discord.js counterpart: the s of GatewayReceivePayload (discord-api-types).
Sourcepub const fn event_type(&self) -> Option<&'a str>
pub const fn event_type(&self) -> Option<&'a str>
Returns the dispatch event name, if any.
discord.js counterpart: the t of GatewayReceivePayload (discord-api-types).
Trait Implementations§
Source§impl<'a> Clone for GatewayEventDeserializer<'a>
impl<'a> Clone for GatewayEventDeserializer<'a>
impl<'a> Copy for GatewayEventDeserializer<'a>
Source§impl<'a> Debug for GatewayEventDeserializer<'a>
impl<'a> Debug for GatewayEventDeserializer<'a>
Source§impl<'de> DeserializeSeed<'de> for GatewayEventDeserializer<'_>
impl<'de> DeserializeSeed<'de> for GatewayEventDeserializer<'_>
Source§type Value = GatewayEvent
type Value = GatewayEvent
Source§fn deserialize<D: Deserializer<'de>>(
self,
deserializer: D,
) -> Result<GatewayEvent, D::Error>
fn deserialize<D: Deserializer<'de>>( self, deserializer: D, ) -> Result<GatewayEvent, D::Error>
Deserialize::deserialize method, except
with some initial piece of data (the seed) passed in.