diff options
| author | Zeyla Hellyer <[email protected]> | 2018-01-01 15:55:46 -0800 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2018-01-01 15:55:46 -0800 |
| commit | 25dddb6695109eeead9e19593cb85a22096c2c7a (patch) | |
| tree | d68d78d210a2804d10027035163871bf806c7e23 /src/model/channel | |
| parent | Give hyper Response in HTTP errors (diff) | |
| download | serenity-25dddb6695109eeead9e19593cb85a22096c2c7a.tar.xz serenity-25dddb6695109eeead9e19593cb85a22096c2c7a.zip | |
Implement or derive Serialize on all models
Diffstat (limited to 'src/model/channel')
| -rw-r--r-- | src/model/channel/attachment.rs | 2 | ||||
| -rw-r--r-- | src/model/channel/channel_category.rs | 2 | ||||
| -rw-r--r-- | src/model/channel/embed.rs | 16 | ||||
| -rw-r--r-- | src/model/channel/group.rs | 5 | ||||
| -rw-r--r-- | src/model/channel/guild_channel.rs | 2 | ||||
| -rw-r--r-- | src/model/channel/message.rs | 4 | ||||
| -rw-r--r-- | src/model/channel/mod.rs | 41 | ||||
| -rw-r--r-- | src/model/channel/private_channel.rs | 6 | ||||
| -rw-r--r-- | src/model/channel/reaction.rs | 27 |
9 files changed, 86 insertions, 19 deletions
diff --git a/src/model/channel/attachment.rs b/src/model/channel/attachment.rs index a9c625b..0530b6f 100644 --- a/src/model/channel/attachment.rs +++ b/src/model/channel/attachment.rs @@ -8,7 +8,7 @@ use std::io::Read; /// A file uploaded with a message. Not to be confused with [`Embed`]s. /// /// [`Embed`]: struct.Embed.html -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Attachment { /// The unique ID given to this attachment. pub id: String, diff --git a/src/model/channel/channel_category.rs b/src/model/channel/channel_category.rs index 7d6c900..18dbde4 100644 --- a/src/model/channel/channel_category.rs +++ b/src/model/channel/channel_category.rs @@ -12,7 +12,7 @@ use utils as serenity_utils; /// A category of [`GuildChannel`]s. /// /// [`GuildChannel`]: struct.GuildChannel.html -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct ChannelCategory { /// Id of this category. pub id: ChannelId, diff --git a/src/model/channel/embed.rs b/src/model/channel/embed.rs index 5359ed5..8837a8d 100644 --- a/src/model/channel/embed.rs +++ b/src/model/channel/embed.rs @@ -17,7 +17,7 @@ use utils; /// 1024 in a field value, and 2048 in a description. /// /// [slack's attachments]: https://api.slack.com/docs/message-attachments -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Embed { /// Information about the author of the embed. pub author: Option<EmbedAuthor>, @@ -95,7 +95,7 @@ impl Embed { } /// An author object in an embed. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct EmbedAuthor { /// The URL of the author icon. /// @@ -110,7 +110,7 @@ pub struct EmbedAuthor { } /// A field object in an embed. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct EmbedField { /// Indicator of whether the field should display as inline. pub inline: bool, @@ -143,7 +143,7 @@ impl EmbedField { } /// Footer information for an embed. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct EmbedFooter { /// The URL of the footer icon. /// @@ -156,7 +156,7 @@ pub struct EmbedFooter { } /// An image object in an embed. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct EmbedImage { /// The height of the image. pub height: u64, @@ -171,7 +171,7 @@ pub struct EmbedImage { } /// The provider of an embed. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct EmbedProvider { /// The name of the provider. pub name: String, @@ -180,7 +180,7 @@ pub struct EmbedProvider { } /// The dimensions and URL of an embed thumbnail. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct EmbedThumbnail { /// The height of the thumbnail in pixels. pub height: u64, @@ -195,7 +195,7 @@ pub struct EmbedThumbnail { } /// Video information for an embed. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct EmbedVideo { /// The height of the video in pixels. pub height: u64, diff --git a/src/model/channel/group.rs b/src/model/channel/group.rs index bff58aa..4a4070a 100644 --- a/src/model/channel/group.rs +++ b/src/model/channel/group.rs @@ -17,7 +17,7 @@ use std::fmt::Write as FmtWrite; /// /// [`Guild`]: struct.Guild.html /// [`User`]: struct.User.html -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Group { /// The Id of the group channel. #[serde(rename = "id")] @@ -33,7 +33,8 @@ pub struct Group { /// The Id of the group owner. pub owner_id: UserId, /// A map of the group's recipients. - #[serde(deserialize_with = "deserialize_users")] + #[serde(deserialize_with = "deserialize_users", + serialize_with = "serialize_users")] pub recipients: HashMap<UserId, Arc<RwLock<User>>>, } diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs index c598314..749e72b 100644 --- a/src/model/channel/guild_channel.rs +++ b/src/model/channel/guild_channel.rs @@ -18,7 +18,7 @@ use utils as serenity_utils; /// Represents a guild's text or voice channel. Some methods are available only /// for voice channels and some are only available for text channels. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct GuildChannel { /// The unique Id of the channel. /// diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index 87385ac..1bb55ab 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -17,7 +17,7 @@ use {constants, http, utils as serenity_utils}; /// A representation of a message over a guild's text channel, a group, or a /// private channel. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Message { /// The unique Id of the message. Can be used to calculate the creation date /// of the message. @@ -584,7 +584,7 @@ impl<'a> From<&'a Message> for MessageId { /// /// [`count`]: #structfield.count /// [reaction type]: enum.ReactionType.html -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct MessageReaction { /// The amount of the type of reaction that have been sent for the /// associated message. diff --git a/src/model/channel/mod.rs b/src/model/channel/mod.rs index 7764fea..00fc7f6 100644 --- a/src/model/channel/mod.rs +++ b/src/model/channel/mod.rs @@ -23,6 +23,7 @@ pub use self::channel_category::*; use internal::RwLockExt; use model::prelude::*; use serde::de::Error as DeError; +use serde::ser::{SerializeStruct, Serialize, Serializer}; use serde_json; use super::utils::deserialize_u64; @@ -504,6 +505,26 @@ impl<'de> Deserialize<'de> for Channel { } } +impl Serialize for Channel { + fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error> + where S: Serializer { + match *self { + Channel::Category(ref c) => { + ChannelCategory::serialize(&*c.read(), serializer) + }, + Channel::Group(ref c) => { + Group::serialize(&*c.read(), serializer) + }, + Channel::Guild(ref c) => { + GuildChannel::serialize(&*c.read(), serializer) + }, + Channel::Private(ref c) => { + PrivateChannel::serialize(&*c.read(), serializer) + }, + } + } +} + #[cfg(feature = "model")] impl Display for Channel { /// Formats the channel into a "mentioned" string. @@ -582,7 +603,7 @@ impl ChannelType { } } -#[derive(Deserialize)] +#[derive(Deserialize, Serialize)] struct PermissionOverwriteData { allow: Permissions, deny: Permissions, @@ -617,6 +638,24 @@ impl<'de> Deserialize<'de> for PermissionOverwrite { } } +impl Serialize for PermissionOverwrite { + fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error> + where S: Serializer { + let (id, kind) = match self.kind { + PermissionOverwriteType::Member(id) => (id.0, "member"), + PermissionOverwriteType::Role(id) => (id.0, "role"), + }; + + let mut state = serializer.serialize_struct("PermissionOverwrite", 4)?; + state.serialize_field("allow", &self.allow.bits())?; + state.serialize_field("deny", &self.deny.bits())?; + state.serialize_field("id", &id)?; + state.serialize_field("type", kind)?; + + state.end() + } +} + /// The type of edit being made to a Channel's permissions. /// /// This is for use with methods such as `GuildChannel::create_permission`. diff --git a/src/model/channel/private_channel.rs b/src/model/channel/private_channel.rs index c9b6524..877fa13 100644 --- a/src/model/channel/private_channel.rs +++ b/src/model/channel/private_channel.rs @@ -11,7 +11,7 @@ use http::AttachmentType; use internal::RwLockExt; /// A Direct Message text channel with another user. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct PrivateChannel { /// The unique Id of the private channel. /// @@ -31,7 +31,9 @@ pub struct PrivateChannel { #[serde(rename = "type")] pub kind: ChannelType, /// The recipient to the private channel. - #[serde(deserialize_with = "deserialize_single_recipient", rename = "recipients")] + #[serde(deserialize_with = "deserialize_single_recipient", + rename = "recipients", + serialize_with = "serialize_sync_user")] pub recipient: Arc<RwLock<User>>, } diff --git a/src/model/channel/reaction.rs b/src/model/channel/reaction.rs index 0741cd7..42c3578 100644 --- a/src/model/channel/reaction.rs +++ b/src/model/channel/reaction.rs @@ -1,5 +1,6 @@ use model::prelude::*; use serde::de::{Deserialize, Error as DeError, MapAccess, Visitor}; +use serde::ser::{SerializeMap, Serialize, Serializer}; use std::error::Error as StdError; use std::fmt::{Display, Formatter, Result as FmtResult, Write as FmtWrite}; use std::str::FromStr; @@ -11,7 +12,7 @@ use CACHE; use http; /// An emoji reaction to a message. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Reaction { /// The [`Channel`] of the associated [`Message`]. /// @@ -255,6 +256,30 @@ impl<'de> Deserialize<'de> for ReactionType { } } +impl Serialize for ReactionType { + fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error> + where S: Serializer { + match *self { + ReactionType::Custom { animated, id, ref name } => { + let mut map = serializer.serialize_map(Some(3))?; + + map.serialize_entry("animated", &animated)?; + map.serialize_entry("id", &id.0)?; + map.serialize_entry("name", &name)?; + + map.end() + }, + ReactionType::Unicode(ref name) => { + let mut map = serializer.serialize_map(Some(1))?; + + map.serialize_entry("name", &name)?; + + map.end() + }, + } + } +} + #[cfg(any(feature = "model", feature = "http"))] impl ReactionType { /// Creates a data-esque display of the type. This is not very useful for |