diff options
| author | acdenisSK <[email protected]> | 2017-07-10 21:47:00 +0200 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2017-07-10 21:47:00 +0200 |
| commit | 710fa02405d8d740c4ee952822d856af0e845aa8 (patch) | |
| tree | 586c1ccb096e2d8f0b2916f566ec63fcbcc7cf16 | |
| parent | Fixed clippy warnings (#120) (diff) | |
| download | serenity-710fa02405d8d740c4ee952822d856af0e845aa8.tar.xz serenity-710fa02405d8d740c4ee952822d856af0e845aa8.zip | |
Use a trait way of overloading the `ban` function instead of an enum
Possibly removes some overhead introduced by enums but makes the underlaying code of the function easier to read and is more concise
| -rw-r--r-- | src/model/guild/guild_id.rs | 28 | ||||
| -rw-r--r-- | src/model/guild/member.rs | 61 |
2 files changed, 37 insertions, 52 deletions
diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index 68212c7..c2cfc3d 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -46,30 +46,14 @@ impl GuildId { /// [`Guild::ban`]: struct.Guild.html#method.ban /// [`User`]: struct.User.html /// [Ban Members]: permissions/constant.BAN_MEMBERS.html - pub fn ban<U: Into<UserId>, BO: Into<BanOptions>>(&self, user: U, ban_options: BO) + pub fn ban<U: Into<UserId>, BO: BanOptions>(&self, user: U, ban_options: BO) -> Result<()> { - - use self::BanOptions::*; - - match ban_options.into() { - DeleteMessageDays(dmd) => { - if dmd > 7 { - return Err(Error::Model(ModelError::DeleteMessageDaysAmount(dmd))); - } - - http::ban_user(self.0, user.into().0, dmd, "") - }, - DMDReason(dmd, reason) => { - if dmd > 7 { - return Err(Error::Model(ModelError::DeleteMessageDaysAmount(dmd))); - } - - http::ban_user(self.0, user.into().0, dmd, &*reason) - }, - Reason(reason) => { - http::ban_user(self.0, user.into().0, 0, &*reason) - }, + let dmd = ban_options.dmd(); + if dmd > 7 { + return Err(Error::Model(ModelError::DeleteMessageDaysAmount(dmd))); } + + http::ban_user(self.0, user.into().0, dmd, &*ban_options.reason()) } /// Gets a list of the guild's bans. diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index ebf99d0..b128887 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -16,33 +16,46 @@ use ::builder::EditMember; #[cfg(all(feature="cache", feature="model", feature="utils"))] use ::utils::Colour; -pub enum BanOptions { - DeleteMessageDays(u8), - DMDReason(u8, String), - Reason(String), +pub trait BanOptions { + fn dmd(&self) -> u8 { 0 } + fn reason(&self) -> String { "".to_string() } } -impl From<u8> for BanOptions { - fn from(dmd: u8) -> Self { - BanOptions::DeleteMessageDays(dmd) +impl BanOptions for u8 { + fn dmd(&self) -> u8 { + *self } } -impl<'a> From<&'a str> for BanOptions { - fn from(reason: &'a str) -> Self { - BanOptions::Reason(reason.to_string()) +impl BanOptions for str { + fn reason(&self) -> String { + self.to_string() } } -impl From<String> for BanOptions { - fn from(reason: String) -> Self { - BanOptions::Reason(reason) +impl BanOptions for String { + fn reason(&self) -> String { + self.clone() } } -impl<'a> From<(u8, &'a str)> for BanOptions { - fn from(dmdreason: (u8, &'a str)) -> Self { - BanOptions::DMDReason(dmdreason.0, dmdreason.1.to_string()) +impl<'a> BanOptions for (u8, &'a str) { + fn dmd(&self) -> u8 { + self.0 + } + + fn reason(&self) -> String { + self.1.to_string() + } +} + +impl BanOptions for (u8, String) { + fn dmd(&self) -> u8 { + self.0 + } + + fn reason(&self) -> String { + self.1.clone() } } @@ -132,20 +145,8 @@ impl Member { /// /// [Ban Members]: permissions/constant.BAN_MEMBERS.html #[cfg(feature="cache")] - pub fn ban<BO: Into<BanOptions>>(&self, ban_options: BO) -> Result<()> { - use self::BanOptions::*; - - match ban_options.into() { - DeleteMessageDays(dmd) => { - http::ban_user(self.guild_id.0, self.user.read().unwrap().id.0, dmd, "") - }, - DMDReason(dmd, reason) => { - http::ban_user(self.guild_id.0, self.user.read().unwrap().id.0, dmd, &*reason) - }, - Reason(reason) => { - http::ban_user(self.guild_id.0, self.user.read().unwrap().id.0, 0, &*reason) - }, - } + pub fn ban<BO: BanOptions>(&self, ban_options: BO) -> Result<()> { + http::ban_user(self.guild_id.0, self.user.read().unwrap().id.0, ban_options.dmd(), &*ban_options.reason()) } /// Determines the member's colour. |