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/guild | |
| 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/guild')
| -rw-r--r-- | src/model/guild/audit_log.rs | 110 | ||||
| -rw-r--r-- | src/model/guild/emoji.rs | 2 | ||||
| -rw-r--r-- | src/model/guild/integration.rs | 4 | ||||
| -rw-r--r-- | src/model/guild/member.rs | 5 | ||||
| -rw-r--r-- | src/model/guild/mod.rs | 20 | ||||
| -rw-r--r-- | src/model/guild/partial_guild.rs | 2 | ||||
| -rw-r--r-- | src/model/guild/role.rs | 2 |
7 files changed, 126 insertions, 19 deletions
diff --git a/src/model/guild/audit_log.rs b/src/model/guild/audit_log.rs index 7437e42..c85bab9 100644 --- a/src/model/guild/audit_log.rs +++ b/src/model/guild/audit_log.rs @@ -1,5 +1,6 @@ use internal::prelude::*; use serde::de::{self, Deserialize, Deserializer, MapAccess, Visitor}; +use serde::ser::Serializer; use super::super::prelude::*; use std::collections::HashMap; use std::mem::transmute; @@ -32,6 +33,24 @@ pub enum Action { MessageDelete, } +impl Action { + pub fn num(&self) -> u8 { + use self::Action::*; + + match *self { + GuildUpdate => 1, + Action::Channel(ref x) => x.num(), + Action::ChannelOverwrite(ref x) => x.num(), + Action::Member(ref x) => x.num(), + Action::Role(ref x) => x.num(), + Action::Invite(ref x) => x.num(), + Action::Webhook(ref x) => x.num(), + Action::Emoji(ref x) => x.num(), + Action::MessageDelete => 72, + } + } +} + #[derive(Debug)] #[repr(u8)] pub enum ActionChannel { @@ -40,6 +59,16 @@ pub enum ActionChannel { Delete = 12, } +impl ActionChannel { + pub fn num(&self) -> u8 { + match *self { + ActionChannel::Create => 10, + ActionChannel::Update => 11, + ActionChannel::Delete => 12, + } + } +} + #[derive(Debug)] #[repr(u8)] pub enum ActionChannelOverwrite { @@ -48,6 +77,16 @@ pub enum ActionChannelOverwrite { Delete = 15, } +impl ActionChannelOverwrite { + pub fn num(&self) -> u8 { + match *self { + ActionChannelOverwrite::Create => 13, + ActionChannelOverwrite::Update => 14, + ActionChannelOverwrite::Delete => 15, + } + } +} + #[derive(Debug)] #[repr(u8)] pub enum ActionMember { @@ -59,6 +98,19 @@ pub enum ActionMember { RoleUpdate = 25, } +impl ActionMember { + pub fn num(&self) -> u8 { + match *self { + ActionMember::Kick => 20, + ActionMember::Prune => 21, + ActionMember::BanAdd => 22, + ActionMember::BanRemove => 23, + ActionMember::Update => 24, + ActionMember::RoleUpdate => 25, + } + } +} + #[derive(Debug)] #[repr(u8)] pub enum ActionRole { @@ -67,6 +119,16 @@ pub enum ActionRole { Delete = 32, } +impl ActionRole { + pub fn num(&self) -> u8 { + match *self { + ActionRole::Create => 30, + ActionRole::Update => 31, + ActionRole::Delete => 32, + } + } +} + #[derive(Debug)] #[repr(u8)] pub enum ActionInvite { @@ -75,6 +137,16 @@ pub enum ActionInvite { Delete = 42, } +impl ActionInvite { + pub fn num(&self) -> u8 { + match *self { + ActionInvite::Create => 40, + ActionInvite::Update => 41, + ActionInvite::Delete => 42, + } + } +} + #[derive(Debug)] #[repr(u8)] pub enum ActionWebhook { @@ -83,6 +155,16 @@ pub enum ActionWebhook { Delete = 52, } +impl ActionWebhook { + pub fn num(&self) -> u8 { + match *self { + ActionWebhook::Create => 50, + ActionWebhook::Update => 51, + ActionWebhook::Delete => 52, + } + } +} + #[derive(Debug)] #[repr(u8)] pub enum ActionEmoji { @@ -91,7 +173,17 @@ pub enum ActionEmoji { Update = 62, } -#[derive(Debug, Deserialize)] +impl ActionEmoji { + pub fn num(&self) -> u8 { + match *self { + ActionEmoji::Create => 60, + ActionEmoji::Update => 61, + ActionEmoji::Delete => 62, + } + } +} + +#[derive(Debug, Deserialize, Serialize)] pub struct Change { #[serde(rename = "key")] pub name: String, // TODO: Change these to an actual type. @@ -106,7 +198,7 @@ pub struct AuditLogs { pub users: Vec<User>, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] pub struct AuditLogEntry { /// Determines to what entity an [`action`] was used on. /// @@ -115,7 +207,9 @@ pub struct AuditLogEntry { /// Determines what action was done on a [`target`] /// /// [`target`]: #structfield.target - #[serde(deserialize_with = "deserialize_action", rename = "action_type")] + #[serde(deserialize_with = "deserialize_action", + rename = "action_type", + serialize_with = "serialize_action")] pub action: Action, /// What was the reasoning by doing an action on a target? If there was one. pub reason: Option<String>, @@ -129,7 +223,7 @@ pub struct AuditLogEntry { pub options: Option<Options>, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] pub struct Options { /// Number of days after which inactive members were kicked. pub delete_member_days: String, @@ -145,7 +239,6 @@ pub struct Options { #[serde(rename = "type")] pub kind: String, /// Name of the role if type is "role" pub role_name: String, - } fn deserialize_action<'de, D: Deserializer<'de>>(de: D) -> StdResult<Action, D::Error> { @@ -177,6 +270,13 @@ fn deserialize_action<'de, D: Deserializer<'de>>(de: D) -> StdResult<Action, D:: de.deserialize_u8(ActionVisitor) } +fn serialize_action<S: Serializer>( + action: &Action, + serializer: S, +) -> StdResult<S::Ok, S::Error> { + serializer.serialize_u8(action.num()) +} + impl<'de> Deserialize<'de> for AuditLogs { fn deserialize<D: Deserializer<'de>>(de: D) -> StdResult<Self, D::Error> { #[derive(Deserialize)] diff --git a/src/model/guild/emoji.rs b/src/model/guild/emoji.rs index 919c29d..a4612f0 100644 --- a/src/model/guild/emoji.rs +++ b/src/model/guild/emoji.rs @@ -15,7 +15,7 @@ use {CACHE, http}; /// Represents a custom guild emoji, which can either be created using the API, /// or via an integration. Emojis created using the API only work within the /// guild it was created in. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Emoji { /// Whether the emoji is animated. #[serde(default)] diff --git a/src/model/guild/integration.rs b/src/model/guild/integration.rs index b903a2c..b9d45ac 100644 --- a/src/model/guild/integration.rs +++ b/src/model/guild/integration.rs @@ -1,7 +1,7 @@ use super::*; /// Various information about integrations. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Integration { pub id: IntegrationId, pub account: IntegrationAccount, @@ -22,7 +22,7 @@ impl From<Integration> for IntegrationId { } /// Integration account object. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct IntegrationAccount { pub id: String, pub name: String, diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index f2e798a..c8b0a3b 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -49,7 +49,7 @@ impl BanOptions for (u8, String) { } /// Information about a member of a guild. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Member { /// Indicator of whether the member can hear in voice channels. pub deaf: bool, @@ -66,7 +66,8 @@ pub struct Member { /// Vector of Ids of [`Role`]s given to the member. pub roles: Vec<RoleId>, /// Attached User struct. - #[serde(deserialize_with = "deserialize_sync_user")] + #[serde(deserialize_with = "deserialize_sync_user", + serialize_with = "serialize_sync_user")] pub user: Arc<RwLock<User>>, } diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index 44a911c..bc97ba8 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -34,7 +34,7 @@ use constants::LARGE_THRESHOLD; use std; /// A representation of a banning of a user. -#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Hash)] +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Hash, Serialize)] pub struct Ban { /// The reason given for this ban. pub reason: Option<String>, @@ -43,7 +43,7 @@ pub struct Ban { } /// Information about a Discord guild, such as channels, emojis, etc. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize)] pub struct Guild { /// Id of a voice channel that's considered the AFK channel. pub afk_channel_id: Option<ChannelId>, @@ -56,11 +56,13 @@ pub struct Guild { /// /// This contains all channels regardless of permissions (i.e. the ability /// of the bot to read from or connect to them). + #[serde(serialize_with = "serialize_gen_locked_map")] pub channels: HashMap<ChannelId, Arc<RwLock<GuildChannel>>>, /// Indicator of whether notifications for all messages are enabled by /// default in the guild. pub default_message_notifications: DefaultMessageNotificationLevel, /// All of the guild's custom emojis. + #[serde(serialize_with = "serialize_gen_map")] pub emojis: HashMap<EmojiId, Emoji>, /// Default explicit content filter level. pub explicit_content_filter: ExplicitContentFilter, @@ -98,6 +100,7 @@ pub struct Guild { /// the library. /// /// [`ReadyEvent`]: events/struct.ReadyEvent.html + #[serde(serialize_with = "serialize_gen_map")] pub members: HashMap<UserId, Member>, /// Indicator of whether the guild requires multi-factor authentication for /// [`Role`]s or [`User`]s with moderation permissions. @@ -114,10 +117,12 @@ pub struct Guild { /// A mapping of [`User`]s' Ids to their current presences. /// /// [`User`]: struct.User.html + #[serde(serialize_with = "serialize_gen_map")] pub presences: HashMap<UserId, Presence>, /// The region that the voice servers that the guild uses are located in. pub region: String, /// A mapping of the guild's roles. + #[serde(serialize_with = "serialize_gen_map")] pub roles: HashMap<RoleId, Role>, /// An identifying hash of the guild's splash icon. /// @@ -133,6 +138,7 @@ pub struct Guild { /// A mapping of of [`User`]s to their current voice state. /// /// [`User`]: struct.User.html + #[serde(serialize_with = "serialize_gen_map")] pub voice_states: HashMap<UserId, VoiceState>, } @@ -1606,7 +1612,7 @@ pub enum GuildContainer { } /// Information relating to a guild's widget embed. -#[derive(Clone, Copy, Debug, Deserialize)] +#[derive(Clone, Copy, Debug, Deserialize, Serialize)] pub struct GuildEmbed { /// The Id of the channel to show the embed for. pub channel_id: ChannelId, @@ -1616,14 +1622,14 @@ pub struct GuildEmbed { /// Representation of the number of members that would be pruned by a guild /// prune operation. -#[derive(Clone, Copy, Debug, Deserialize)] +#[derive(Clone, Copy, Debug, Deserialize, Serialize)] pub struct GuildPrune { /// The number of members that would be pruned by the operation. pub pruned: u64, } /// Basic information about a guild. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct GuildInfo { /// The unique Id of the guild. /// @@ -1674,7 +1680,7 @@ impl InviteGuild { } /// Data for an unavailable guild. -#[derive(Clone, Copy, Debug, Deserialize)] +#[derive(Clone, Copy, Debug, Deserialize, Serialize)] pub struct GuildUnavailable { /// The Id of the [`Guild`] that is unavailable. /// @@ -1687,7 +1693,7 @@ pub struct GuildUnavailable { } #[allow(large_enum_variant)] -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] #[serde(untagged)] pub enum GuildStatus { OnlinePartialGuild(PartialGuild), diff --git a/src/model/guild/partial_guild.rs b/src/model/guild/partial_guild.rs index cd51a98..e67ca03 100644 --- a/src/model/guild/partial_guild.rs +++ b/src/model/guild/partial_guild.rs @@ -8,7 +8,7 @@ use builder::{EditGuild, EditMember, EditRole}; /// like member data. /// /// [`Guild`]: struct.Guild.html -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct PartialGuild { pub id: GuildId, pub afk_channel_id: Option<ChannelId>, diff --git a/src/model/guild/role.rs b/src/model/guild/role.rs index 6215c4e..53ec478 100644 --- a/src/model/guild/role.rs +++ b/src/model/guild/role.rs @@ -14,7 +14,7 @@ use {CACHE, http}; /// are unique per guild and do not cross over to other guilds in any way, and /// can have channel-specific permission overrides in addition to guild-level /// permissions. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Role { /// The Id of the role. Can be used to calculate the role's creation date. pub id: RoleId, |