diff options
| author | Zeyla Hellyer <[email protected]> | 2017-04-11 08:15:37 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-04-11 10:52:43 -0700 |
| commit | f6b27eb39c042e6779edc2d5d4b6e6c27d133eaf (patch) | |
| tree | a6169fee3bf9ea75391101577dcb2982e3daa388 /src/constants.rs | |
| parent | Clippy lints + permission byte literals (diff) | |
| download | serenity-f6b27eb39c042e6779edc2d5d4b6e6c27d133eaf.tar.xz serenity-f6b27eb39c042e6779edc2d5d4b6e6c27d133eaf.zip | |
Switch to using serde for deserialization
The current build system is rudimentary, incomplete, and rigid, offering
little in the way of customizing decoding options.
To solve this, switch to using serde-derive with custom Deserialization
implementations. This allows very simple deserialization when special
logic does not need to be applied, yet allows us to implement our own
deserialization logic when required.
The problem with the build system was that it built enums and structs
from YAML files. This is not so good, because it requires creating a
custom build system (which was rudimentary), creating "special struct
configs" when logic needed to be ever so slightly extended (rigid), and
if special logic needed to be applied, a custom deserialization method
would have been needed to be made anyway (incomplete).
To solve this, switch to serde-derive and implementing Deserialize
ourselves where required. This reduces YAML definitions that might
look like:
```yaml
---
name: Group
description: >
A group channel, potentially including other users, separate from a [`Guild`].
[`Guild`]: struct.Guild.html
fields:
- name: channel_id
description: The Id of the group channel.
from: id
type: ChannelId
- name: icon
description: The optional icon of the group channel.
optional: true
type: string
- name: last_message_id
description: The Id of the last message sent.
optional: true
type: MessageId
- name: last_pin_timestamp
description: Timestamp of the latest pinned message.
optional: true
type: string
- name: name
description: The name of the group channel.
optional: true
type: string
- name: owner_id
description: The Id of the group channel creator.
type: UserId
- name: recipients
description: Group channel's members.
custom: decode_users
t: UserId, Arc<RwLock<User>>
type: hashmap
```
to:
```rs
/// A group channel - potentially including other [`User`]s - separate from a
/// [`Guild`].
///
/// [`Guild`]: struct.Guild.html
/// [`User`]: struct.User.html
pub struct Group {
/// The Id of the group channel.
#[serde(rename="id")]
pub channel_id: ChannelId,
/// The optional icon of the group channel.
pub icon: Option<String>,
/// The Id of the last message sent.
pub last_message_id: Option<MessageId>,
/// Timestamp of the latest pinned message.
pub last_pin_timestamp: Option<String>,
/// The name of the group channel.
pub name: Option<String>,
/// The Id of the group owner.
pub owner_id: UserId,
/// A map of the group's recipients.
#[serde(deserialize_with="deserialize_users")]
pub recipients: HashMap<UserId, Arc<RwLock<User>>>,
}
```
This is much simpler and does not have as much boilerplate.
There should not be any backwards incompatible changes other than the
old, public - yet undocumented (and hidden from documentation) - decode
methods being removed. Due to the nature of this commit, field names may
be incorrect, and will need to be corrected as deserialization errors
are found.
Diffstat (limited to 'src/constants.rs')
| -rw-r--r-- | src/constants.rs | 106 |
1 files changed, 50 insertions, 56 deletions
diff --git a/src/constants.rs b/src/constants.rs index 99327b7..0ec973c 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,3 +1,5 @@ +use std::result::Result as StdResult; + /// The gateway version used by the library. The gateway URI is retrieved via /// the REST API. pub const GATEWAY_VERSION: u8 = 6; @@ -56,41 +58,37 @@ pub enum ErrorCode { UnknownUser, } -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum OpCode { - Event, - Heartbeat, - Identify, - StatusUpdate, - VoiceStateUpdate, - VoiceServerPing, - Resume, - Reconnect, - GetGuildMembers, - InvalidSession, - Hello, - HeartbeatAck, -} - -impl OpCode { - pub fn from_num(num: u64) -> Option<Self> { - match num { - 0 => Some(OpCode::Event), - 1 => Some(OpCode::Heartbeat), - 2 => Some(OpCode::Identify), - 3 => Some(OpCode::StatusUpdate), - 4 => Some(OpCode::VoiceStateUpdate), - 5 => Some(OpCode::VoiceServerPing), - 6 => Some(OpCode::Resume), - 7 => Some(OpCode::Reconnect), - 8 => Some(OpCode::GetGuildMembers), - 9 => Some(OpCode::InvalidSession), - 10 => Some(OpCode::Hello), - 11 => Some(OpCode::HeartbeatAck), - _ => None, - } +enum_number!( + /// Enum to map gateway opcodes. + OpCode { + /// Dispatches an event. + Event = 0, + /// Used for ping checking. + Heartbeat = 1, + /// Used for client handshake. + Identify = 2, + /// Used to update the client status. + StatusUpdate = 3, + /// Used to join/move/leave voice channels. + VoiceStateUpdate = 4, + /// Used for voice ping checking. + VoiceServerPing = 5, + /// Used to resume a closed connection. + Resume = 6, + /// Used to tell clients to reconnect to the gateway. + Reconnect = 7, + /// Used to request guild members. + GetGuildMembers = 8, + /// Used to notify clients that they have an invalid session Id. + InvalidSession = 9, + /// Sent immediately after connection, contains heartbeat + server info. + Hello = 10, + /// Sent immediately following a client heartbeat that was received. + HeartbeatAck = 11, } +); +impl OpCode { pub fn num(&self) -> u64 { match *self { OpCode::Event => 0, @@ -109,31 +107,27 @@ impl OpCode { } } -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum VoiceOpCode { - Identify, - Heartbeat, - Hello, - KeepAlive, - SelectProtocol, - SessionDescription, - Speaking, -} - -impl VoiceOpCode { - pub fn from_num(num: u64) -> Option<Self> { - match num { - 0 => Some(VoiceOpCode::Identify), - 1 => Some(VoiceOpCode::SelectProtocol), - 2 => Some(VoiceOpCode::Hello), - 3 => Some(VoiceOpCode::KeepAlive), - 4 => Some(VoiceOpCode::SessionDescription), - 5 => Some(VoiceOpCode::Speaking), - 8 => Some(VoiceOpCode::Heartbeat), - _ => None, - } +enum_number!( + /// Enum to map voice opcodes. + VoiceOpCode { + /// Used to begin a voice websocket connection. + Identify = 0, + /// Used to select the voice protocol. + SelectProtocol = 1, + /// Used to complete the websocket handshake. + Hello = 2, + /// Used to keep the websocket connection alive. + KeepAlive = 3, + /// Used to describe the session. + SessionDescription = 4, + /// Used to indicate which users are speaking. + Speaking = 5, + /// Used to heartbeat. + Heartbeat = 8, } +); +impl VoiceOpCode { pub fn num(&self) -> u64 { match *self { VoiceOpCode::Identify => 0, |