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/audit_log.rs | |
| 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/audit_log.rs')
| -rw-r--r-- | src/model/guild/audit_log.rs | 110 |
1 files changed, 105 insertions, 5 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)] |