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/webhook.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/webhook.rs')
| -rw-r--r-- | src/model/webhook.rs | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/src/model/webhook.rs b/src/model/webhook.rs index 4f831d9..0a2718e 100644 --- a/src/model/webhook.rs +++ b/src/model/webhook.rs @@ -1,10 +1,42 @@ -use serde_json::builder::ObjectBuilder; use std::mem; -use super::{Message, Webhook, WebhookId}; +use super::*; use ::utils::builder::ExecuteWebhook; use ::client::rest; use ::internal::prelude::*; +/// A representation of a webhook, which is a low-effort way to post messages to +/// channels. They do not necessarily require a bot user or authentication to +/// use. +#[derive(Clone, Debug, Deserialize)] +pub struct Webhook { + /// The unique Id. + /// + /// Can be used to calculate the creation date of the webhook. + pub id: WebhookId, + /// The default avatar. + /// + /// This can be modified via [`ExecuteWebhook::avatar`]. + /// + /// [`ExecuteWebhook::avatar`]: ../utils/builder/struct.ExecuteWebhook.html#method.avatar + pub avatar: Option<String>, + /// The Id of the channel that owns the webhook. + pub channel_id: ChannelId, + /// The Id of the guild that owns the webhook. + pub guild_id: Option<GuildId>, + /// The default name of the webhook. + /// + /// This can be modified via [`ExecuteWebhook::username`]. + /// + /// [`ExecuteWebhook::username`]: ../utils/builder/struct.ExecuteWebhook.html#method.username + pub name: Option<String>, + /// The webhook's secure token. + pub token: String, + /// The user that created the webhook. + /// + /// **Note**: This is not received when getting a webhook by its token. + pub user: Option<User>, +} + impl Webhook { /// Deletes the webhook. /// @@ -63,16 +95,15 @@ impl Webhook { /// /// [`rest::edit_webhook`]: ../client/rest/fn.edit_webhook.html /// [`rest::edit_webhook_with_token`]: ../client/rest/fn.edit_webhook_with_token.html - pub fn edit(&mut self, name: Option<&str>, avatar: Option<&str>) - -> Result<()> { + pub fn edit(&mut self, name: Option<&str>, avatar: Option<&str>) -> Result<()> { if name.is_none() && avatar.is_none() { return Ok(()); } - let mut map = ObjectBuilder::new(); + let mut map = Map::new(); if let Some(avatar) = avatar { - map = map.insert("avatar", if avatar.is_empty() { + map.insert("avatar".to_owned(), if avatar.is_empty() { Value::Null } else { Value::String(avatar.to_owned()) @@ -80,10 +111,10 @@ impl Webhook { } if let Some(name) = name { - map = map.insert("name", name); + map.insert("name".to_owned(), Value::String(name.to_owned())); } - match rest::edit_webhook_with_token(self.id.0, &self.token, &map.build()) { + match rest::edit_webhook_with_token(self.id.0, &self.token, &map) { Ok(replacement) => { mem::replace(self, replacement); @@ -142,7 +173,7 @@ impl Webhook { /// ``` #[inline] pub fn execute<F: FnOnce(ExecuteWebhook) -> ExecuteWebhook>(&self, f: F) -> Result<Message> { - rest::execute_webhook(self.id.0, &self.token, &f(ExecuteWebhook::default()).0.build()) + rest::execute_webhook(self.id.0, &self.token, &f(ExecuteWebhook::default()).0) } /// Retrieves the latest information about the webhook, editing the |