Skip to main content

GatewayEventDeserializer

Struct GatewayEventDeserializer 

Source
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>

Source

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).

Source

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).

Source

pub const fn op(&self) -> OpCode

Returns the opcode.

discord.js counterpart: the op of GatewayReceivePayload (discord-api-types).

Source

pub const fn sequence(&self) -> Option<u64>

Returns the sequence number, if known.

discord.js counterpart: the s of GatewayReceivePayload (discord-api-types).

Source

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>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for GatewayEventDeserializer<'a>

Source§

impl<'a> Debug for GatewayEventDeserializer<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> DeserializeSeed<'de> for GatewayEventDeserializer<'_>

Source§

type Value = GatewayEvent

The type produced by using this seed.
Source§

fn deserialize<D: Deserializer<'de>>( self, deserializer: D, ) -> Result<GatewayEvent, D::Error>

Equivalent to the more common Deserialize::deserialize method, except with some initial piece of data (the seed) passed in.
Source§

impl<'a> Eq for GatewayEventDeserializer<'a>

Source§

impl<'a> PartialEq for GatewayEventDeserializer<'a>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'a> StructuralPartialEq for GatewayEventDeserializer<'a>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.