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/misc.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/misc.rs')
| -rw-r--r-- | src/model/misc.rs | 93 |
1 files changed, 74 insertions, 19 deletions
diff --git a/src/model/misc.rs b/src/model/misc.rs index 118492c..5cf3a91 100644 --- a/src/model/misc.rs +++ b/src/model/misc.rs @@ -1,18 +1,6 @@ -use super::{ - ChannelId, - Channel, - Emoji, - Member, - RoleId, - Role, - UserId, - User, - IncidentStatus, - EmojiIdentifier -}; -use ::internal::prelude::*; -use std::str::FromStr; use std::result::Result as StdResult; +use std::str::FromStr; +use super::*; use ::utils; /// Allows something - such as a channel or role - to be mentioned in a message. @@ -130,6 +118,16 @@ impl FromStr for RoleId { } } +/// A version of an emoji used only when solely the Id and name are known. +#[derive(Clone, Debug)] +pub struct EmojiIdentifier { + /// The Id of the emoji. + pub id: EmojiId, + /// The name of the emoji. It must be at least 2 characters long and can + /// only contain alphanumeric characters and underscores. + pub name: String, +} + impl EmojiIdentifier { /// Generates a URL to the emoji's image. #[inline] @@ -171,9 +169,66 @@ impl FromStr for Channel { } } -impl IncidentStatus { - #[doc(hidden)] - pub fn decode(value: Value) -> Result<Self> { - Self::decode_str(value) - } +/// A component that was affected during a service incident. +/// +/// This is pulled from the Discord status page. +#[derive(Clone, Debug, Deserialize)] +pub struct AffectedComponent { + pub name: String, +} + +/// An incident retrieved from the Discord status page. +/// +/// This is not necessarily a representation of an ongoing incident. +#[derive(Clone, Debug, Deserialize)] +pub struct Incident { + pub created_at: String, + pub id: String, + pub impact: String, + pub incident_updates: Vec<IncidentUpdate>, + pub monitoring_at: Option<String>, + pub name: String, + pub page_id: String, + pub resolved_at: Option<String>, + pub short_link: String, + pub status: String, + pub updated_at: String, +} + +/// An update to an incident from the Discord status page. +/// +/// This will typically state what new information has been discovered about an +/// incident. +#[derive(Clone, Debug, Deserialize)] +pub struct IncidentUpdate { + pub affected_components: Vec<AffectedComponent>, + pub body: String, + pub created_at: String, + pub display_at: String, + pub id: String, + pub incident_id: String, + pub status: IncidentStatus, + pub updated_at: String, +} + +/// The type of status update during a service incident. +#[derive(Copy, Clone, Debug, Deserialize, Hash, Eq, PartialEq, PartialOrd, Ord, Serialize)] +#[serde(rename_all="snake_case")] +pub enum IncidentStatus { + Identified, + Investigating, + Monitoring, + Postmortem, + Resolved, +} + +/// A Discord status maintenance message. This can be either for active +/// maintenances or for scheduled maintenances. +#[derive(Clone, Debug, Deserialize)] +pub struct Maintenance { + pub description: String, + pub id: String, + pub name: String, + pub start: String, + pub stop: String, } |