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/utils/builder/edit_channel.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/utils/builder/edit_channel.rs')
| -rw-r--r-- | src/utils/builder/edit_channel.rs | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/src/utils/builder/edit_channel.rs b/src/utils/builder/edit_channel.rs index e136cbf..2fa61a5 100644 --- a/src/utils/builder/edit_channel.rs +++ b/src/utils/builder/edit_channel.rs @@ -1,5 +1,4 @@ -use serde_json::builder::ObjectBuilder; -use std::default::Default; +use ::internal::prelude::*; /// A builder to edit a [`GuildChannel`] for use via one of a couple methods. /// @@ -24,7 +23,8 @@ use std::default::Default; /// [`Context::edit_channel`]: ../client/struct.Context.html#method.edit_channel /// [`GuildChannel`]: ../model/struct.GuildChannel.html /// [`GuildChannel::edit`]: ../model/struct.GuildChannel.html#method.edit -pub struct EditChannel(pub ObjectBuilder); +#[derive(Default)] +pub struct EditChannel(pub JsonMap); impl EditChannel { /// The bitrate of the channel in bits. @@ -32,20 +32,26 @@ impl EditChannel { /// This is for [voice] channels only. /// /// [voice]: ../../model/enum.ChannelType.html#variant.Voice - pub fn bitrate(self, bitrate: u64) -> Self { - EditChannel(self.0.insert("bitrate", bitrate)) + pub fn bitrate(mut self, bitrate: u64) -> Self { + self.0.insert("bitrate".to_owned(), Value::Number(Number::from(bitrate))); + + self } /// The name of the channel. /// /// Must be between 2 and 100 characters long. - pub fn name(self, name: &str) -> Self { - EditChannel(self.0.insert("name", name)) + pub fn name(mut self, name: &str) -> Self { + self.0.insert("name".to_owned(), Value::String(name.to_owned())); + + self } /// The position of the channel in the channel list. - pub fn position(self, position: u64) -> Self { - EditChannel(self.0.insert("position", position)) + pub fn position(mut self, position: u64) -> Self { + self.0.insert("position".to_owned(), Value::Number(Number::from(position))); + + self } /// The topic of the channel. Can be empty. @@ -55,8 +61,10 @@ impl EditChannel { /// This is for [text] channels only. /// /// [text]: ../../model/enum.ChannelType.html#variant.Text - pub fn topic(self, topic: &str) -> Self { - EditChannel(self.0.insert("topic", topic)) + pub fn topic(mut self, topic: &str) -> Self { + self.0.insert("topic".to_owned(), Value::String(topic.to_owned())); + + self } /// The number of users that may be in the channel simultaneously. @@ -64,14 +72,9 @@ impl EditChannel { /// This is for [voice] channels only. /// /// [voice]: ../../model/enum.ChannelType.html#variant.Voice - pub fn user_limit(self, user_limit: u64) -> Self { - EditChannel(self.0.insert("user_limit", user_limit)) - } -} + pub fn user_limit(mut self, user_limit: u64) -> Self { + self.0.insert("user_limit".to_owned(), Value::Number(Number::from(user_limit))); -impl Default for EditChannel { - /// Creates a builder with no default parameters. - fn default() -> EditChannel { - EditChannel(ObjectBuilder::new()) + self } } |