diff options
| author | acdenisSK <[email protected]> | 2017-10-02 22:29:10 +0200 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2017-10-02 22:29:10 +0200 |
| commit | 6a4e52b3fac7d2e96e3a1a67901fbdd4721fb249 (patch) | |
| tree | ae9ad7d7cb73d4ece6a199796af5975545071493 /src/model | |
| parent | `to_owned` -> `to_string` (diff) | |
| download | serenity-6a4e52b3fac7d2e96e3a1a67901fbdd4721fb249.tar.xz serenity-6a4e52b3fac7d2e96e3a1a67901fbdd4721fb249.zip | |
Use the de-generification trick.
Fixes #168
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/channel/channel_id.rs | 61 | ||||
| -rw-r--r-- | src/model/channel/group.rs | 8 | ||||
| -rw-r--r-- | src/model/channel/message.rs | 6 | ||||
| -rw-r--r-- | src/model/channel/reaction.rs | 12 | ||||
| -rw-r--r-- | src/model/guild/guild_id.rs | 91 | ||||
| -rw-r--r-- | src/model/guild/member.rs | 22 | ||||
| -rw-r--r-- | src/model/guild/mod.rs | 30 | ||||
| -rw-r--r-- | src/model/invite.rs | 5 | ||||
| -rw-r--r-- | src/model/user.rs | 10 |
9 files changed, 190 insertions, 55 deletions
diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs index 4cc997d..04ed266 100644 --- a/src/model/channel/channel_id.rs +++ b/src/model/channel/channel_id.rs @@ -81,7 +81,11 @@ impl ChannelId { #[inline] pub fn create_reaction<M, R>(&self, message_id: M, reaction_type: R) -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { - http::create_reaction(self.0, message_id.into().0, &reaction_type.into()) + self._create_reaction(message_id.into(), reaction_type.into()) + } + + fn _create_reaction(&self, MessageId(id): MessageId, reaction_type: ReactionType) -> Result<()> { + http::create_reaction(self.0, id, &reaction_type) } /// Deletes this channel, returning the channel on a successful deletion. @@ -100,7 +104,11 @@ impl ChannelId { /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[inline] pub fn delete_message<M: Into<MessageId>>(&self, message_id: M) -> Result<()> { - http::delete_message(self.0, message_id.into().0) + self._delete_message(message_id.into()) + } + + fn _delete_message(&self, MessageId(id): MessageId) -> Result<()> { + http::delete_message(self.0, id) } /// Deletes all messages by Ids from the given vector in the given channel. @@ -156,11 +164,18 @@ impl ChannelId { reaction_type: R) -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { + self._delete_reaction(message_id.into(), user_id, reaction_type.into()) + } + + fn _delete_reaction(&self, + MessageId(id): MessageId, + user_id: Option<UserId>, + reaction_type: ReactionType) -> Result<()> { http::delete_reaction( self.0, - message_id.into().0, + id, user_id.map(|uid| uid.0), - &reaction_type.into(), + &reaction_type, ) } @@ -209,6 +224,11 @@ impl ChannelId { /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message> where F: FnOnce(CreateMessage) -> CreateMessage, M: Into<MessageId> { + self._edit_message(message_id.into(), f) + } + + fn _edit_message<F>(&self, MessageId(id): MessageId, f: F) -> Result<Message> + where F: FnOnce(CreateMessage) -> CreateMessage { let map = f(CreateMessage::default()).0; if let Some(content) = map.get("content") { @@ -219,7 +239,7 @@ impl ChannelId { } } - http::edit_message(self.0, message_id.into().0, &Value::Object(map)) + http::edit_message(self.0, id, &Value::Object(map)) } /// Search the cache for the channel with the Id. @@ -253,7 +273,11 @@ impl ChannelId { /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html #[inline] pub fn message<M: Into<MessageId>>(&self, message_id: M) -> Result<Message> { - http::get_message(self.0, message_id.into().0) + self._message(message_id.into()) + } + + fn _message(&self, MessageId(id): MessageId) -> Result<Message> { + http::get_message(self.0, id) .map(|mut msg| { msg.transform_content(); @@ -326,7 +350,11 @@ impl ChannelId { /// [`Message`]: struct.Message.html #[inline] pub fn pin<M: Into<MessageId>>(&self, message_id: M) -> Result<()> { - http::pin_message(self.0, message_id.into().0) + self._pin(message_id.into()) + } + + fn _pin(&self, MessageId(id): MessageId) -> Result<()> { + http::pin_message(self.0, id) } /// Gets the list of [`Message`]s which are pinned to the channel. @@ -354,14 +382,23 @@ impl ChannelId { after: Option<U>) -> Result<Vec<User>> where M: Into<MessageId>, R: Into<ReactionType>, U: Into<UserId> { + self._reaction_users(message_id.into(), reaction_type.into(), limit, after.map(|u| u.into())) + } + + fn _reaction_users(&self, + MessageId(id): MessageId, + reaction_type: ReactionType, + limit: Option<u8>, + after: Option<UserId>) + -> Result<Vec<User>> { let limit = limit.map_or(50, |x| if x > 100 { 100 } else { x }); http::get_reaction_users( self.0, - message_id.into().0, - &reaction_type.into(), + id, + &reaction_type, limit, - after.map(|u| u.into().0), + after.map(|u| u.0), ) } @@ -504,6 +541,10 @@ impl ChannelId { http::unpin_message(self.0, message_id.into().0) } + fn _unpin(&self, MessageId(id): MessageId) -> Result<()> { + http::unpin_message(self.0, id) + } + /// Retrieves the channel's webhooks. /// /// **Note**: Requires the [Manage Webhooks] permission. diff --git a/src/model/channel/group.rs b/src/model/channel/group.rs index 418ce3a..dfccca3 100644 --- a/src/model/channel/group.rs +++ b/src/model/channel/group.rs @@ -49,8 +49,10 @@ impl Group { /// /// [`http::add_group_recipient`]: ../http/fn.add_group_recipient.html pub fn add_recipient<U: Into<UserId>>(&self, user: U) -> Result<()> { - let user = user.into(); + self._add_recipient(user.into()) + } + fn _add_recipient(&self, user: UserId) -> Result<()> { // If the group already contains the recipient, do nothing. if self.recipients.contains_key(&user) { return Ok(()); @@ -253,8 +255,10 @@ impl Group { /// /// **Note**: This is only available to the group owner. pub fn remove_recipient<U: Into<UserId>>(&self, user: U) -> Result<()> { - let user = user.into(); + self._remove_recipient(user.into()) + } + fn _remove_recipient(&self, user: UserId) -> Result<()> { // If the group does not contain the recipient already, do nothing. if !self.recipients.contains_key(&user) { return Ok(()); diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index 55f61ba..52f5be5 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -419,6 +419,10 @@ impl Message { /// [Add Reactions]: permissions/constant.ADD_REACTIONS.html /// [permissions]: permissions pub fn react<R: Into<ReactionType>>(&self, reaction_type: R) -> Result<()> { + self._react(reaction_type.into()) + } + + fn _react(&self, reaction_type: ReactionType) -> Result<()> { #[cfg(feature = "cache")] { let req = Permissions::ADD_REACTIONS; @@ -428,7 +432,7 @@ impl Message { } } - http::create_reaction(self.channel_id.0, self.id.0, &reaction_type.into()) + http::create_reaction(self.channel_id.0, self.id.0, &reaction_type) } /// Replies to the user, mentioning them prior to the content in the form diff --git a/src/model/channel/reaction.rs b/src/model/channel/reaction.rs index 8edc2e9..976ea3d 100644 --- a/src/model/channel/reaction.rs +++ b/src/model/channel/reaction.rs @@ -106,12 +106,20 @@ impl Reaction { after: Option<U>) -> Result<Vec<User>> where R: Into<ReactionType>, U: Into<UserId> { + self._users(reaction_type.into(), limit, after.map(|u| u.into())) + } + + fn _users(&self, + reaction_type: ReactionType, + limit: Option<u8>, + after: Option<UserId>) + -> Result<Vec<User>> { http::get_reaction_users( self.channel_id.0, self.message_id.0, - &reaction_type.into(), + &reaction_type, limit.unwrap_or(50), - after.map(|u| u.into().0), + after.map(|u| u.0), ) } } diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index 4b9a713..bc50e16 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -49,6 +49,10 @@ impl GuildId { /// [`User`]: struct.User.html /// [Ban Members]: permissions/constant.BAN_MEMBERS.html pub fn ban<U: Into<UserId>, BO: BanOptions>(&self, user: U, ban_options: BO) -> Result<()> { + self._ban(user.into(), ban_options) + } + + fn _ban<BO: BanOptions>(&self, UserId(id): UserId, ban_options: BO) -> Result<()> { let dmd = ban_options.dmd(); if dmd > 7 { return Err(Error::Model(ModelError::DeleteMessageDaysAmount(dmd))); @@ -60,7 +64,7 @@ impl GuildId { return Err(Error::ExceededLimit(reason.to_string(), 512)); } - http::ban_user(self.0, user.into().0, dmd, &*reason) + http::ban_user(self.0, id, dmd, &reason) } /// Gets a list of the guild's bans. @@ -149,13 +153,16 @@ impl GuildId { /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html pub fn create_integration<I>(&self, integration_id: I, kind: &str) -> Result<()> where I: Into<IntegrationId> { - let integration_id = integration_id.into(); + self._create_integration(integration_id.into(), kind) + } + + fn _create_integration(&self, IntegrationId(id): IntegrationId, kind: &str) -> Result<()> { let map = json!({ - "id": integration_id.0, + "id": id, "type": kind, }); - http::create_guild_integration(self.0, integration_id.0, &map) + http::create_guild_integration(self.0, id, &map) } /// Creates a new role in the guild with the data set, if any. @@ -190,7 +197,11 @@ impl GuildId { /// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html #[inline] pub fn delete_emoji<E: Into<EmojiId>>(&self, emoji_id: E) -> Result<()> { - http::delete_emoji(self.0, emoji_id.into().0) + self._delete_emoji(emoji_id.into()) + } + + fn _delete_emoji(&self, EmojiId(id): EmojiId) -> Result<()> { + http::delete_emoji(self.0, id) } /// Deletes an integration by Id from the guild. @@ -200,7 +211,11 @@ impl GuildId { /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] pub fn delete_integration<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()> { - http::delete_guild_integration(self.0, integration_id.into().0) + self._delete_integration(integration_id.into()) + } + + fn _delete_integration(&self, IntegrationId(id): IntegrationId) -> Result<()> { + http::delete_guild_integration(self.0, id) } /// Deletes a [`Role`] by Id from the guild. @@ -215,7 +230,11 @@ impl GuildId { /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[inline] pub fn delete_role<R: Into<RoleId>>(&self, role_id: R) -> Result<()> { - http::delete_role(self.0, role_id.into().0) + self._delete_role(role_id.into()) + } + + fn _delete_role(&self, RoleId(id): RoleId) -> Result<()> { + http::delete_role(self.0, id) } /// Edits the current guild with new data where specified. @@ -243,11 +262,15 @@ impl GuildId { /// [`Emoji::edit`]: struct.Emoji.html#method.edit /// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html pub fn edit_emoji<E: Into<EmojiId>>(&self, emoji_id: E, name: &str) -> Result<Emoji> { + self._edit_emoji(emoji_id.into(), name) + } + + fn _edit_emoji(&self, EmojiId(id): EmojiId, name: &str) -> Result<Emoji> { let map = json!({ "name": name, }); - http::edit_emoji(self.0, emoji_id.into().0, &map) + http::edit_emoji(self.0, id, &map) } /// Edits the properties of member of the guild, such as muting or @@ -266,7 +289,12 @@ impl GuildId { #[inline] pub fn edit_member<F, U>(&self, user_id: U, f: F) -> Result<()> where F: FnOnce(EditMember) -> EditMember, U: Into<UserId> { - http::edit_member(self.0, user_id.into().0, &f(EditMember::default()).0) + self._edit_member(user_id.into(), f) + } + + fn _edit_member<F>(&self, UserId(id): UserId, f: F) -> Result<()> + where F: FnOnce(EditMember) -> EditMember { + http::edit_member(self.0, id, &f(EditMember::default()).0) } /// Edits the current user's nickname for the guild. @@ -300,7 +328,12 @@ impl GuildId { #[inline] pub fn edit_role<F, R>(&self, role_id: R, f: F) -> Result<Role> where F: FnOnce(EditRole) -> EditRole, R: Into<RoleId> { - http::edit_role(self.0, role_id.into().0, &f(EditRole::default()).0) + self._edit_role(role_id.into(), f) + } + + fn _edit_role<F>(&self, RoleId(id): RoleId, f: F) -> Result<Role> + where F: FnOnce(EditRole) -> EditRole { + http::edit_role(self.0, id, &f(EditRole::default()).0) } /// Search the cache for the guild. @@ -336,7 +369,11 @@ impl GuildId { /// [Kick Members]: permissions/constant.KICK_MEMBERS.html #[inline] pub fn kick<U: Into<UserId>>(&self, user_id: U) -> Result<()> { - http::kick_member(self.0, user_id.into().0) + self._kick(user_id.into()) + } + + fn _kick(&self, UserId(id): UserId) -> Result<()> { + http::kick_member(self.0, id) } /// Leaves the guild. @@ -349,7 +386,11 @@ impl GuildId { /// [`Member`]: struct.Member.html #[inline] pub fn member<U: Into<UserId>>(&self, user_id: U) -> Result<Member> { - http::get_member(self.0, user_id.into().0) + self._member(user_id.into()) + } + + fn _member(&self, UserId(id): UserId) -> Result<Member> { + http::get_member(self.0, id) } /// Gets a list of the guild's members. @@ -362,7 +403,11 @@ impl GuildId { #[inline] pub fn members<U>(&self, limit: Option<u64>, after: Option<U>) -> Result<Vec<Member>> where U: Into<UserId> { - http::get_guild_members(self.0, limit, after.map(|x| x.into().0)) + self._members(limit, after.map(|x| x.into())) + } + + fn _members(&self, limit: Option<u64>, after: Option<UserId>) -> Result<Vec<Member>> { + http::get_guild_members(self.0, limit, after.map(|x| x.0)) } /// Moves a member to a specific voice channel. @@ -372,13 +417,17 @@ impl GuildId { /// [Move Members]: permissions/constant.MOVE_MEMBERS.html pub fn move_member<C, U>(&self, user_id: U, channel_id: C) -> Result<()> where C: Into<ChannelId>, U: Into<UserId> { + self._move_member(user_id.into(), channel_id.into()) + } + + fn _move_member(&self, UserId(uid): UserId, ChannelId(cid): ChannelId) -> Result<()> { let mut map = Map::new(); map.insert( "channel_id".to_string(), - Value::Number(Number::from(channel_id.into().0)), + Value::Number(Number::from(cid)), ); - http::edit_member(self.0, user_id.into().0, &map) + http::edit_member(self.0, uid, &map) } /// Gets the number of [`Member`]s that would be pruned with the given @@ -442,7 +491,11 @@ impl GuildId { /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] pub fn start_integration_sync<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()> { - http::start_integration_sync(self.0, integration_id.into().0) + self._start_integration_sync(integration_id.into()) + } + + fn _start_integration_sync(&self, IntegrationId(id): IntegrationId) -> Result<()> { + http::start_integration_sync(self.0, id) } /// Starts a prune of [`Member`]s. @@ -471,7 +524,11 @@ impl GuildId { /// [Ban Members]: permissions/constant.BAN_MEMBERS.html #[inline] pub fn unban<U: Into<UserId>>(&self, user_id: U) -> Result<()> { - http::remove_ban(self.0, user_id.into().0) + self._unban(user_id.into()) + } + + fn _unban(&self, UserId(id): UserId) -> Result<()> { + http::remove_ban(self.0, id) } /// Retrieves the guild's webhooks. diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index 09474f9..7c0c784 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -78,15 +78,18 @@ impl Member { /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[cfg(feature = "cache")] pub fn add_role<R: Into<RoleId>>(&mut self, role_id: R) -> Result<()> { - let role_id = role_id.into(); + self._add_role(role_id.into()) + } - if self.roles.contains(&role_id) { + #[cfg(feature = "cache")] + fn _add_role(&mut self, id: RoleId) -> Result<()> { + if self.roles.contains(&id) { return Ok(()); } - match http::add_member_role(self.guild_id.0, self.user.read().unwrap().id.0, role_id.0) { + match http::add_member_role(self.guild_id.0, self.user.read().unwrap().id.0, id.0) { Ok(()) => { - self.roles.push(role_id); + self.roles.push(id); Ok(()) }, @@ -312,15 +315,18 @@ impl Member { /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[cfg(feature = "cache")] pub fn remove_role<R: Into<RoleId>>(&mut self, role_id: R) -> Result<()> { - let role_id = role_id.into(); + self._remove_role(role_id.into()) + } - if !self.roles.contains(&role_id) { + #[cfg(feature = "cache")] + fn _remove_role(&mut self, id: RoleId) -> Result<()> { + if !self.roles.contains(&id) { return Ok(()); } - match http::remove_member_role(self.guild_id.0, self.user.read().unwrap().id.0, role_id.0) { + match http::remove_member_role(self.guild_id.0, self.user.read().unwrap().id.0, id.0) { Ok(()) => { - self.roles.retain(|r| r.0 != role_id.0); + self.roles.retain(|r| r.0 != id.0); Ok(()) }, diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index 8173a04..a3fa055 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -205,6 +205,10 @@ impl Guild { /// [`User`]: struct.User.html /// [Ban Members]: permissions/constant.BAN_MEMBERS.html pub fn ban<U: Into<UserId>, BO: BanOptions>(&self, user: U, options: BO) -> Result<()> { + self._ban(user.into(), options) + } + + fn _ban<BO: BanOptions>(&self, user: UserId, options: BO) -> Result<()> { #[cfg(feature = "cache")] { let req = Permissions::BAN_MEMBERS; @@ -600,7 +604,9 @@ impl Guild { /// /// Requires that the current user be in the guild. #[inline] - pub fn get<G: Into<GuildId>>(guild_id: G) -> Result<PartialGuild> { guild_id.into().get() } + pub fn get<G: Into<GuildId>>(guild_id: G) -> Result<PartialGuild> { Self::_get(guild_id.into()) } + + fn _get(guild_id: GuildId) -> Result<PartialGuild> { guild_id.get() } /// Returns the formatted URL of the guild's icon, if one exists. pub fn icon_url(&self) -> Option<String> { @@ -757,15 +763,15 @@ impl Guild { /// [`User`]: struct.User.html pub fn permissions_for<C, U>(&self, channel_id: C, user_id: U) -> Permissions where C: Into<ChannelId>, U: Into<UserId> { - let user_id = user_id.into(); + self._permissions_for(channel_id.into(), user_id.into()) + } + fn _permissions_for(&self, ChannelId(cid): ChannelId, UserId(uid): UserId) -> Permissions { // The owner has all permissions in all cases. - if user_id == self.owner_id { + if self.owner_id == uid { return Permissions::all(); } - let channel_id = channel_id.into(); - // Start by retrieving the @everyone role's permissions. let everyone = match self.roles.get(&RoleId(self.id.0)) { Some(everyone) => everyone, @@ -783,7 +789,7 @@ impl Guild { // Create a base set of permissions, starting with `@everyone`s. let mut permissions = everyone.permissions; - let member = match self.members.get(&user_id) { + let member = match self.members.get(&UserId(uid)) { Some(member) => member, None => return everyone.permissions, }; @@ -806,7 +812,7 @@ impl Guild { return Permissions::all(); } - if let Some(channel) = self.channels.get(&channel_id) { + if let Some(channel) = self.channels.get(&ChannelId(cid)) { let channel = channel.read().unwrap(); // If this is a text channel, then throw out voice permissions. @@ -839,7 +845,7 @@ impl Guild { // Member for overwrite in &channel.permission_overwrites { - if PermissionOverwriteType::Member(user_id) != overwrite.kind { + if PermissionOverwriteType::Member(UserId(uid)) != overwrite.kind { continue; } @@ -849,12 +855,12 @@ impl Guild { warn!( "(╯°□°)╯︵ ┻━┻ Guild {} does not contain channel {}", self.id, - channel_id + cid ); } // The default channel is always readable. - if channel_id.0 == self.id.0 { + if cid == self.id.0 { permissions |= Permissions::READ_MESSAGES; } @@ -1007,6 +1013,10 @@ impl Guild { /// [`User`]: struct.User.html /// [Ban Members]: permissions/constant.BAN_MEMBERS.html pub fn unban<U: Into<UserId>>(&self, user_id: U) -> Result<()> { + self._unban(user_id.into()) + } + + fn _unban(&self, user_id: UserId) -> Result<()> { #[cfg(feature = "cache")] { let req = Permissions::BAN_MEMBERS; diff --git a/src/model/invite.rs b/src/model/invite.rs index b4f326c..9a836be 100644 --- a/src/model/invite.rs +++ b/src/model/invite.rs @@ -62,8 +62,11 @@ impl Invite { /// [permission]: permissions/index.html pub fn create<C, F>(channel_id: C, f: F) -> Result<RichInvite> where C: Into<ChannelId>, F: FnOnce(CreateInvite) -> CreateInvite { - let channel_id = channel_id.into(); + Self::_create(channel_id.into(), f) + } + fn _create<F>(channel_id: ChannelId, f: F) -> Result<RichInvite> + where F: FnOnce(CreateInvite) -> CreateInvite { #[cfg(feature = "cache")] { let req = Permissions::CREATE_INVITE; diff --git a/src/model/user.rs b/src/model/user.rs index 3ae33aa..c3c3c0d 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -573,17 +573,19 @@ impl User { // no-cache would warn on guild_id. pub fn has_role<G, R>(&self, guild: G, role: R) -> bool where G: Into<GuildContainer>, R: Into<RoleId> { - let role_id = role.into(); + self._has_role(guild.into(), role.into()) + } - match guild.into() { - GuildContainer::Guild(guild) => guild.roles.contains_key(&role_id), + fn _has_role(&self, guild: GuildContainer, role: RoleId) -> bool { + match guild { + GuildContainer::Guild(guild) => guild.roles.contains_key(&role), GuildContainer::Id(_guild_id) => { feature_cache! {{ CACHE.read() .unwrap() .guilds .get(&_guild_id) - .map(|g| g.read().unwrap().roles.contains_key(&role_id)) + .map(|g| g.read().unwrap().roles.contains_key(&role)) .unwrap_or(false) } else { true |