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/model/gateway.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/model/gateway.rs')
| -rw-r--r-- | src/model/gateway.rs | 188 |
1 files changed, 152 insertions, 36 deletions
diff --git a/src/model/gateway.rs b/src/model/gateway.rs index dc25d0d..826f723 100644 --- a/src/model/gateway.rs +++ b/src/model/gateway.rs @@ -1,29 +1,40 @@ +use serde::de::Error as DeError; +use serde_json; use std::sync::{Arc, RwLock}; use super::utils::*; use super::*; -use ::internal::prelude::*; -impl Game { - #[doc(hidden)] - pub fn decode(value: Value) -> Result<Option<Game>> { - let mut map = into_map(value)?; - - let name = match map.remove("name") { - Some(Value::Null) | None => return Ok(None), - Some(v) => into_string(v)?, - }; - - if name.trim().is_empty() { - return Ok(None); - } +/// A representation of the data retrieved from the bot gateway endpoint. +/// +/// This is different from the [`Gateway`], as this includes the number of +/// shards that Discord recommends to use for a bot user. +/// +/// This is only applicable to bot users. +#[derive(Clone, Debug, Deserialize)] +pub struct BotGateway { + /// The number of shards that is recommended to be used by the current bot + /// user. + pub shards: u64, + /// The gateway to connect to. + pub url: String, +} - Ok(Some(Game { - name: name, - kind: opt(&mut map, "type", GameType::decode)?.unwrap_or(GameType::Playing), - url: opt(&mut map, "url", into_string)?, - })) - } +/// Representation of a game that a [`User`] is playing -- or streaming in the +/// case that a stream URL is provided. +#[derive(Clone, Debug)] +pub struct Game { + /// The type of game status. + pub kind: GameType, + /// The name of the game being played. + pub name: String, + /// The Stream URL if [`kind`] is [`GameType::Streaming`]. + /// + /// [`GameType::Streaming`]: enum.GameType.html#variant.Streaming + /// [`kind`]: #structfield.kind + pub url: Option<String>, +} +impl Game { /// Creates a `Game` struct that appears as a `Playing <name>` status. /// /// **Note**: Maximum `name` length is 128. @@ -47,31 +58,136 @@ impl Game { } } -impl Presence { - #[doc(hidden)] - pub fn decode(value: Value) -> Result<Presence> { - let mut value = into_map(value)?; - let mut user_map = remove(&mut value, "user").and_then(into_map)?; +impl Deserialize for Game { + fn deserialize<D: Deserializer>(deserializer: D) -> StdResult<Self, D::Error> { + let mut map = JsonMap::deserialize(deserializer)?; + let kind = map.remove("type") + .and_then(|v| GameType::deserialize(v).ok()) + .unwrap_or(GameType::Playing); + let name = map.remove("name") + .and_then(|v| String::deserialize(v).ok()) + .unwrap_or_else(String::new); + let url = map.remove("url").and_then(|v| serde_json::from_value::<String>(v).ok()); + + Ok(Game { + kind: kind, + name: name, + url: url + }) + } +} + +enum_number!( + /// The type of activity that is being performed when playing a game. + GameType { + /// An indicator that the user is playing a game. + Playing = 0, + /// An indicator that the user is streaming to a service. + Streaming = 1, + } +); + +impl Default for GameType { + fn default() -> Self { + GameType::Playing + } +} + +/// A representation of the data retrieved from the gateway endpoint. +/// +/// For the bot-specific gateway, refer to [`BotGateway`]. +/// +/// [`BotGateway`]: struct.BotGateway.html +#[derive(Clone, Debug, Deserialize)] +pub struct Gateway { + /// The gateway to connect to. + pub url: String, +} + +/// Information detailing the current online status of a [`User`]. +/// +/// [`User`]: struct.User.html +#[derive(Clone, Debug)] +pub struct Presence { + /// The game that a [`User`] is current playing. + /// + /// [`User`]: struct.User.html + pub game: Option<Game>, + /// The date of the last presence update. + pub last_modified: Option<u64>, + /// The nickname of the member, if applicable. + pub nick: Option<String>, + /// The user's online status. + pub status: OnlineStatus, + /// The Id of the [`User`]. Can be used to calculate the user's creation + /// date. + pub user_id: UserId, + /// The associated user instance. + pub user: Option<Arc<RwLock<User>>>, +} + +impl Deserialize for Presence { + fn deserialize<D: Deserializer>(deserializer: D) -> StdResult<Presence, D::Error> { + let mut map = JsonMap::deserialize(deserializer)?; + let mut user_map = map.remove("user") + .ok_or_else(|| DeError::custom("expected presence user")) + .and_then(JsonMap::deserialize) + .map_err(DeError::custom)?; let (user_id, user) = if user_map.len() > 1 { - let user = User::decode(Value::Object(user_map))?; - (user.id, Some(user)) + let user = User::deserialize(Value::Object(user_map)).map_err(DeError::custom)?; + + (user.id, Some(Arc::new(RwLock::new(user)))) } else { - (remove(&mut user_map, "id").and_then(UserId::decode)?, None) + let user_id = user_map.remove("id") + .ok_or_else(|| DeError::custom("Missing presence user id")) + .and_then(|x| UserId::deserialize(x.clone())) + .map_err(DeError::custom)?; + + (user_id, None) }; - let game = match value.remove("game") { - None | Some(Value::Null) => None, - Some(v) => Game::decode(v)?, + let game = match map.remove("game") { + Some(v) => serde_json::from_value::<Option<Game>>(v).map_err(DeError::custom)?, + None => None, + }; + let last_modified = match map.remove("last_modified") { + Some(v) => Some(u64::deserialize(v).map_err(DeError::custom)?), + None => None, + }; + let nick = match map.remove("nick") { + Some(v) => serde_json::from_value::<Option<String>>(v).map_err(DeError::custom)?, + None => None, }; + let status = map.remove("status") + .ok_or_else(|| DeError::custom("expected presence status")) + .and_then(OnlineStatus::deserialize) + .map_err(DeError::custom)?; Ok(Presence { - user_id: user_id, - status: remove(&mut value, "status").and_then(OnlineStatus::decode_str)?, - last_modified: opt(&mut value, "last_modified", |v| Ok(req!(v.as_u64())))?, game: game, - user: user.map(RwLock::new).map(Arc::new), - nick: opt(&mut value, "nick", into_string)?, + last_modified: last_modified, + nick: nick, + status: status, + user: user, + user_id: user_id, }) } } + +/// An initial set of information given after IDENTIFYing to the gateway. +#[derive(Clone, Debug, Deserialize)] +pub struct Ready { + pub guilds: Vec<GuildStatus>, + #[serde(deserialize_with="deserialize_presences")] + pub presences: HashMap<UserId, Presence>, + #[serde(deserialize_with="deserialize_private_channels")] + pub private_channels: HashMap<ChannelId, Channel>, + pub session_id: String, + pub shard: Option<[u64; 2]>, + #[serde(default, rename="_trace")] + pub trace: Vec<String>, + pub user: CurrentUser, + #[serde(rename="v")] + pub version: u64, +} |