diff options
| author | Austin Hellyer <[email protected]> | 2016-11-25 21:04:27 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-11-25 21:04:27 -0800 |
| commit | 4b3b3a7024ecdd77eb6ac24b6ad5bd936e7a5f3f (patch) | |
| tree | 686d13936289a76801d1188ce06119feff6675e5 /src/model | |
| parent | Catch some clippy lints (diff) | |
| download | serenity-4b3b3a7024ecdd77eb6ac24b6ad5bd936e7a5f3f.tar.xz serenity-4b3b3a7024ecdd77eb6ac24b6ad5bd936e7a5f3f.zip | |
More widely support no-cache method compiles
Conditional compiles with the 'methods' feature enabled - but the
'cache' feature disabled - were supported, but methods would call an
empty function to check if the current user has permissions. Instead,
put these function calls behind macros which check for feature cfgs.
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/channel.rs | 119 | ||||
| -rw-r--r-- | src/model/guild.rs | 137 | ||||
| -rw-r--r-- | src/model/invite.rs | 19 |
3 files changed, 160 insertions, 115 deletions
diff --git a/src/model/channel.rs b/src/model/channel.rs index 64e7c71..9a3f9ea 100644 --- a/src/model/channel.rs +++ b/src/model/channel.rs @@ -426,21 +426,24 @@ impl Message { /// /// # Errors /// - /// Returns a [`ClientError::InvalidPermissions`] if the current user does - /// not have the required permissions. + /// If the `cache` feature is enabled, then returns a + /// [`ClientError::InvalidPermissions`] if the current user does not have + /// the required permissions. /// /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [`ClientError::InvalidUser`]: ../client/enum.ClientError.html#variant.InvalidUser /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html - #[cfg(all(feature = "cache", feature = "methods"))] + #[cfg(feature = "methods")] pub fn delete(&self) -> Result<()> { - let req = permissions::MANAGE_MESSAGES; - let is_author = self.author.id != CACHE.read().unwrap().user.id; - let has_perms = try!(utils::user_has_perms(self.channel_id, req)); + feature_cache_enabled! {{ + let req = permissions::MANAGE_MESSAGES; + let is_author = self.author.id != CACHE.read().unwrap().user.id; + let has_perms = try!(utils::user_has_perms(self.channel_id, req)); - if !is_author && !has_perms { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !is_author && !has_perms { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} rest::delete_message(self.channel_id.0, self.id.0) } @@ -451,19 +454,22 @@ impl Message { /// /// # Errors /// - /// Returns a [`ClientError::InvalidPermissions`] if the current user does - /// not have the required permissions. + /// If the `cache` feature is enabled, then returns a + /// [`ClientError::InvalidPermissions`] if the current user does not have + /// the required permissions. /// /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [`Reaction`]: struct.Reaction.html /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html - #[cfg(all(feature = "cache", feature = "methods"))] + #[cfg(feature = "methods")] pub fn delete_reactions(&self) -> Result<()> { - let req = permissions::MANAGE_MESSAGES; + feature_cache_enabled! {{ + let req = permissions::MANAGE_MESSAGES; - if !try!(utils::user_has_perms(self.channel_id, req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(utils::user_has_perms(self.channel_id, req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} rest::delete_message_reactions(self.channel_id.0, self.id.0) } @@ -548,7 +554,7 @@ impl Message { /// /// # Errors /// - /// Returns a + /// If the `cache` is enabled, returns a /// [`ClientError::InvalidPermissions`] if the current user does not have /// the required permissions. /// @@ -556,11 +562,13 @@ impl Message { /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[cfg(feature = "methods")] pub fn pin(&self) -> Result<()> { - let req = permissions::MANAGE_MESSAGES; + feature_cache_enabled! {{ + let req = permissions::MANAGE_MESSAGES; - if !try!(utils::user_has_perms(self.channel_id, req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(utils::user_has_perms(self.channel_id, req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} rest::pin_message(self.channel_id.0, self.id.0) } @@ -571,8 +579,9 @@ impl Message { /// /// # Errors /// - /// Returns a [`ClientError::InvalidPermissions`] if the current user does - /// not have the required [permissions]. + /// If the `cache` is enabled, returns a + /// [`ClientError::InvalidPermissions`] if the current user does not have + /// the required [permissions]. /// /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [`Emoji`]: struct.Emoji.html @@ -580,11 +589,13 @@ impl Message { /// [permissions]: permissions #[cfg(feature = "methods")] pub fn react<R: Into<ReactionType>>(&self, reaction_type: R) -> Result<()> { - let req = permissions::ADD_REACTIONS; + feature_cache_enabled! {{ + let req = permissions::ADD_REACTIONS; - if !try!(utils::user_has_perms(self.channel_id, req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(utils::user_has_perms(self.channel_id, req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} rest::create_reaction(self.channel_id.0, self.id.0, @@ -602,8 +613,9 @@ impl Message { /// /// # Errors /// - /// Returns a [`ClientError::InvalidPermissions`] if the current user does - /// not have the required permissions. + /// If the `cache` is enabled, returns a + /// [`ClientError::InvalidPermissions`] if the current user does not have + /// the required permissions. /// /// Returns a [`ClientError::MessageTooLong`] if the content of the message /// is over the above limit, containing the number of unicode code points @@ -618,11 +630,13 @@ impl Message { return Err(Error::Client(ClientError::MessageTooLong(length_over))); } - let req = permissions::SEND_MESSAGES; + feature_cache_enabled! {{ + let req = permissions::SEND_MESSAGES; - if !try!(utils::user_has_perms(self.channel_id, req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(utils::user_has_perms(self.channel_id, req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} let mut gen = format!("{}", self.author.mention()); gen.push(':'); @@ -644,7 +658,7 @@ impl Message { /// /// # Errors /// - /// Returns a + /// If the `cache` is enabled, returns a /// [`ClientError::InvalidPermissions`] if the current user does not have /// the required permissions. /// @@ -652,11 +666,13 @@ impl Message { /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[cfg(feature = "methods")] pub fn unpin(&self) -> Result<()> { - let req = permissions::MANAGE_MESSAGES; + feature_cache_enabled! {{ + let req = permissions::MANAGE_MESSAGES; - if !try!(utils::user_has_perms(self.channel_id, req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(utils::user_has_perms(self.channel_id, req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} rest::unpin_message(self.channel_id.0, self.id.0) } @@ -850,9 +866,11 @@ impl GuildChannel { pub fn delete(&self) -> Result<Channel> { let req = permissions::MANAGE_CHANNELS; - if !try!(utils::user_has_perms(self.id, req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + feature_cache_enabled! {{ + if !try!(utils::user_has_perms(self.id, req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} rest::delete_channel(self.id.0) } @@ -862,9 +880,11 @@ impl GuildChannel { where F: FnOnce(EditChannel) -> EditChannel { let req = permissions::MANAGE_CHANNELS; - if !try!(utils::user_has_perms(self.id, req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + feature_cache_enabled! {{ + if !try!(utils::user_has_perms(self.id, req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} let map = ObjectBuilder::new() .insert("name", &self.name) @@ -928,11 +948,13 @@ impl GuildChannel { return Err(Error::Client(ClientError::MessageTooLong(length_over))); } - let req = permissions::SEND_MESSAGES; + feature_cache_enabled! {{ + let req = permissions::SEND_MESSAGES; - if !try!(utils::user_has_perms(self.id, req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(utils::user_has_perms(self.id, req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} let map = ObjectBuilder::new() .insert("content", content) @@ -1081,7 +1103,6 @@ impl ReactionType { /// /// **Note**: This is mainly for use internally. There is otherwise most /// likely little use for it. - #[inline(always)] pub fn as_data(&self) -> String { match *self { ReactionType::Custom { id, ref name } => { diff --git a/src/model/guild.rs b/src/model/guild.rs index 9cfbd7e..1fe3075 100644 --- a/src/model/guild.rs +++ b/src/model/guild.rs @@ -218,11 +218,13 @@ impl Guild { return Err(Error::Client(ClientError::DeleteMessageDaysAmount(delete_message_days))); } - let req = permissions::BAN_MEMBERS; + feature_cache_enabled! {{ + let req = permissions::BAN_MEMBERS; - if !try!(self.has_perms(req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(self.has_perms(req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} rest::ban_user(self.id.0, user.into().0, delete_message_days) } @@ -233,19 +235,21 @@ impl Guild { /// /// # Errors /// - /// Returns a [`ClientError::InvalidPermissions`] if the current user does - /// not have permission to perform bans. + /// If the `cache` is enabled, returns a [`ClientError::InvalidPermissions`] + /// if the current user does not have permission to perform bans. /// /// [`Ban`]: struct.Ban.html /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [Ban Members]: permissions/constant.BAN_MEMBERS.html #[cfg(feature = "methods")] pub fn bans(&self) -> Result<Vec<Ban>> { - let req = permissions::BAN_MEMBERS; + feature_cache_enabled! {{ + let req = permissions::BAN_MEMBERS; - if !try!(self.has_perms(req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(self.has_perms(req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} rest::get_bans(self.id.0) } @@ -266,8 +270,8 @@ impl Guild { /// /// # Errors /// - /// Returns a [`ClientError::InvalidPermissions`] if the current user does - /// not have permission to perform bans. + /// If the `cache` is enabled, returns a [`ClientError::InvalidPermissions`] + /// if the current user does not have permission to perform bans. /// /// [`Channel`]: struct.Channel.html /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions @@ -275,11 +279,13 @@ impl Guild { #[cfg(feature = "methods")] pub fn create_channel(&mut self, name: &str, kind: ChannelType) -> Result<Channel> { - let req = permissions::MANAGE_CHANNELS; + feature_cache_enabled! {{ + let req = permissions::MANAGE_CHANNELS; - if !try!(self.has_perms(req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(self.has_perms(req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} let map = ObjectBuilder::new() .insert("name", name) @@ -297,8 +303,8 @@ impl Guild { /// /// # Errors /// - /// Returns a [`ClientError::InvalidPermissions`] if the current user does - /// not have permission to perform bans. + /// If the `cache` is enabled, returns a [`ClientError::InvalidPermissions`] + /// if the current user does not have permission to perform bans. /// /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [`Context::create_role`]: ../client/struct.Context.html#method.create_role @@ -307,11 +313,13 @@ impl Guild { #[cfg(feature = "methods")] pub fn create_role<F>(&self, f: F) -> Result<Role> where F: FnOnce(EditRole) -> EditRole { - let req = permissions::MANAGE_ROLES; + feature_cache_enabled! {{ + let req = permissions::MANAGE_ROLES; - if !try!(self.has_perms(req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(self.has_perms(req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} let role = { try!(rest::create_role(self.id.0)) @@ -398,8 +406,8 @@ impl Guild { /// /// # Errors /// - /// Returns a [`ClientError::InvalidPermissions`] if the current user does - /// not have permission to perform bans. + /// If the `cache` is enabled, returns a [`ClientError::InvalidPermissions`] + /// if the current user does not have permission to perform bans. /// /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [`Context::edit_guild`]: ../client/struct.Context.html#method.edit_guild @@ -407,11 +415,13 @@ impl Guild { #[cfg(feature = "methods")] pub fn edit<F>(&mut self, f: F) -> Result<()> where F: FnOnce(EditGuild) -> EditGuild { - let req = permissions::MANAGE_GUILD; + feature_cache_enabled! {{ + let req = permissions::MANAGE_GUILD; - if !try!(self.has_perms(req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(self.has_perms(req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} let map = f(EditGuild::default()).0.build(); @@ -445,18 +455,21 @@ impl Guild { /// /// # Errors /// - /// Returns a [`ClientError::InvalidPermissions`] if the current user does - /// not have permission to change their own nickname. + /// If the `cache` is enabled, returns a [`ClientError::InvalidPermissions`] + /// if the current user does not have permission to change their own + /// nickname. /// /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [Change Nickname]: permissions/constant.CHANGE_NICKNAME.html #[cfg(feature = "methods")] pub fn edit_nickname(&self, new_nickname: Option<&str>) -> Result<()> { - let req = permissions::CHANGE_NICKNAME; + feature_cache_enabled! {{ + let req = permissions::CHANGE_NICKNAME; - if !try!(self.has_perms(req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(self.has_perms(req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} rest::edit_nickname(self.id.0, new_nickname) } @@ -475,18 +488,20 @@ impl Guild { /// /// # Errors /// - /// Returns a [`ClientError::InvalidPermissions`] if the current user does - /// not have permission to perform bans. + /// If the `cache` is enabled, returns a [`ClientError::InvalidPermissions`] + /// if the current user does not have permission to perform bans. /// /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[cfg(feature = "methods")] pub fn get_invites(&self) -> Result<Vec<RichInvite>> { - let req = permissions::MANAGE_GUILD; + feature_cache_enabled! {{ + let req = permissions::MANAGE_GUILD; - if !try!(self.has_perms(req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(self.has_perms(req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} rest::get_guild_invites(self.id.0) } @@ -675,8 +690,8 @@ impl Guild { /// /// # Errors /// - /// Returns a [`ClientError::InvalidPermissions`] if the current user does - /// not have permission to perform bans. + /// If the `cache` is enabled, returns a [`ClientError::InvalidPermissions`] + /// if the current user does not have permission to perform bans. /// /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [`GuildPrune`]: struct.GuildPrune.html @@ -684,11 +699,13 @@ impl Guild { /// [Kick Members]: permissions/constant.KICK_MEMBERS.html #[cfg(feature = "methods")] pub fn prune_count(&self, days: u16) -> Result<GuildPrune> { - let req = permissions::KICK_MEMBERS; + feature_cache_enabled! {{ + let req = permissions::KICK_MEMBERS; - if !try!(self.has_perms(req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(self.has_perms(req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} let map = ObjectBuilder::new() .insert("days", days) @@ -705,8 +722,8 @@ impl Guild { /// /// # Errors /// - /// Returns a [`ClientError::InvalidPermissions`] if the current user does - /// not have permission to perform bans. + /// If the `cache` is enabled, returns a [`ClientError::InvalidPermissions`] + /// if the current user does not have permission to perform bans. /// /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [`GuildPrune`]: struct.GuildPrune.html @@ -714,11 +731,13 @@ impl Guild { /// [Kick Members]: permissions/constant.KICK_MEMBERS.html #[cfg(feature = "methods")] pub fn start_prune(&self, days: u16) -> Result<GuildPrune> { - let req = permissions::KICK_MEMBERS; + feature_cache_enabled! {{ + let req = permissions::KICK_MEMBERS; - if !try!(self.has_perms(req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(self.has_perms(req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} let map = ObjectBuilder::new() .insert("days", days) @@ -733,19 +752,21 @@ impl Guild { /// /// # Errors /// - /// Returns a [`ClientError::InvalidPermissions`] if the current user does - /// not have permission to perform bans. + /// If the `cache` is enabled, returns a [`ClientError::InvalidPermissions`] + /// if the current user does not have permission to perform bans. /// /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [`User`]: struct.User.html /// [Ban Members]: permissions/constant.BAN_MEMBERS.html #[cfg(feature = "methods")] pub fn unban<U: Into<UserId>>(&self, user: U) -> Result<()> { - let req = permissions::BAN_MEMBERS; + feature_cache_enabled! {{ + let req = permissions::BAN_MEMBERS; - if !try!(self.has_perms(req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(self.has_perms(req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} rest::remove_ban(self.id.0, user.into().0) } diff --git a/src/model/invite.rs b/src/model/invite.rs index 8322ddb..c05b4d4 100644 --- a/src/model/invite.rs +++ b/src/model/invite.rs @@ -44,19 +44,21 @@ impl Invite { /// /// # Errors /// - /// Returns a [`ClientError::InvalidPermissions`] if the current user does - /// not have the required [permission]. + /// If the `cache` is enabled, returns a [`ClientError::InvalidPermissions`] + /// if the current user does not have the required [permission]. /// /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html /// [permission]: permissions/index.html #[cfg(feature="methods")] pub fn delete(&self) -> Result<Invite> { - let req = permissions::MANAGE_GUILD; + feature_cache_enabled! {{ + let req = permissions::MANAGE_GUILD; - if !try!(utils::user_has_perms(self.channel.id, req)) { - return Err(Error::Client(ClientError::InvalidPermissions(req))); - } + if !try!(utils::user_has_perms(self.channel.id, req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + }} rest::delete_invite(&self.code) } @@ -75,8 +77,9 @@ impl RichInvite { /// /// # Errors /// - /// Returns a [`ClientError::InvalidOperationAsBot`] if the current user is - /// a bot user. + /// If the `cache` is enabled, returns a + /// [`ClientError::InvalidOperationAsBot`] if the current user is a bot + /// user. /// /// [`ClientError::InvalidOperationAsBot`]: enum.ClientError.html#variant.InvalidOperationAsBot /// [`Guild`]: struct.Guild.html |