diff options
| author | acdenisSK <[email protected]> | 2017-08-24 15:26:49 +0200 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2017-08-24 16:36:01 +0200 |
| commit | b3a5bc89ad1c09290fb1c15ca3b36fe17c3796f3 (patch) | |
| tree | 315e16f7b252d22b5f832302e722a85c9e6a9b6e /src/model/guild | |
| parent | Allow FromStr for User to use REST (#147) (diff) | |
| download | serenity-b3a5bc89ad1c09290fb1c15ca3b36fe17c3796f3.tar.xz serenity-b3a5bc89ad1c09290fb1c15ca3b36fe17c3796f3.zip | |
Revamp `RwLock` usage in the lib
Also not quite sure if they goofed rustfmt or something, but its changes it did were a bit bizarre.
Diffstat (limited to 'src/model/guild')
| -rw-r--r-- | src/model/guild/audit_log.rs | 16 | ||||
| -rw-r--r-- | src/model/guild/guild_id.rs | 10 | ||||
| -rw-r--r-- | src/model/guild/integration.rs | 3 | ||||
| -rw-r--r-- | src/model/guild/member.rs | 35 | ||||
| -rw-r--r-- | src/model/guild/mod.rs | 82 | ||||
| -rw-r--r-- | src/model/guild/partial_guild.rs | 24 | ||||
| -rw-r--r-- | src/model/guild/role.rs | 7 |
7 files changed, 80 insertions, 97 deletions
diff --git a/src/model/guild/audit_log.rs b/src/model/guild/audit_log.rs index 962b145..6c24662 100644 --- a/src/model/guild/audit_log.rs +++ b/src/model/guild/audit_log.rs @@ -91,12 +91,9 @@ pub enum ActionEmoji { #[derive(Debug, Deserialize)] pub struct Change { - #[serde(rename = "key")] - pub name: String, - #[serde(rename = "old_value")] - pub old: String, - #[serde(rename = "new_value")] - pub new: String, + #[serde(rename = "key")] pub name: String, + #[serde(rename = "old_value")] pub old: String, + #[serde(rename = "new_value")] pub new: String, } #[derive(Debug)] @@ -126,7 +123,7 @@ pub struct AuditLogEntry { pub id: AuditLogEntryId, } -fn deserialize_target<'de, D: Deserializer<'de>>(de: D) -> Result<Target, D::Error> { +fn deserialize_target<'de, D: Deserializer<'de>>(de: D) -> Result<Target, D::Error>{ struct TargetVisitor; impl<'de> Visitor<'de> for TargetVisitor { @@ -147,7 +144,7 @@ fn deserialize_target<'de, D: Deserializer<'de>>(de: D) -> Result<Target, D::Err de.deserialize_i32(TargetVisitor) } -fn deserialize_action<'de, D: Deserializer<'de>>(de: D) -> Result<Action, D::Error> { +fn deserialize_action<'de, D: Deserializer<'de>>(de: D) -> Result<Action, D::Error>{ struct ActionVisitor; impl<'de> Visitor<'de> for ActionVisitor { @@ -180,8 +177,7 @@ impl<'de> Deserialize<'de> for AuditLogs { #[derive(Deserialize)] #[serde(field_identifier)] enum Field { - #[serde(rename = "audit_log_entries")] - Entries, + #[serde(rename = "audit_log_entries")] Entries, } struct EntriesVisitor; diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index 4d6ba9b..02775ba 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -48,7 +48,7 @@ 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: BanOptions>(&self, user: U, ban_options: BO) -> Result<()> { +pub fn ban<U: Into<UserId>, BO: BanOptions>(&self, user: U, ban_options: BO) -> Result<()>{ let dmd = ban_options.dmd(); if dmd > 7 { return Err(Error::Model(ModelError::DeleteMessageDaysAmount(dmd))); @@ -167,7 +167,7 @@ impl GuildId { /// [`Guild::create_role`]: struct.Guild.html#method.create_role /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[inline] - pub fn create_role<F: FnOnce(EditRole) -> EditRole>(&self, f: F) -> Result<Role> { +pub fn create_role<F: FnOnce(EditRole) -> EditRole>(&self, f: F) -> Result<Role>{ http::create_role(self.0, &f(EditRole::default()).0) } @@ -199,7 +199,7 @@ impl GuildId { /// /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] - pub fn delete_integration<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()> { +pub fn delete_integration<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()>{ http::delete_guild_integration(self.0, integration_id.into().0) } @@ -228,7 +228,7 @@ impl GuildId { /// [`Guild::edit`]: struct.Guild.html#method.edit /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] - pub fn edit<F: FnOnce(EditGuild) -> EditGuild>(&mut self, f: F) -> Result<PartialGuild> { +pub fn edit<F: FnOnce(EditGuild) -> EditGuild>(&mut self, f: F) -> Result<PartialGuild>{ http::edit_guild(self.0, &f(EditGuild::default()).0) } @@ -459,7 +459,7 @@ impl GuildId { /// /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] - pub fn start_integration_sync<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()> { +pub fn start_integration_sync<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()>{ http::start_integration_sync(self.0, integration_id.into().0) } diff --git a/src/model/guild/integration.rs b/src/model/guild/integration.rs index 18da39d..b903a2c 100644 --- a/src/model/guild/integration.rs +++ b/src/model/guild/integration.rs @@ -6,8 +6,7 @@ pub struct Integration { pub id: IntegrationId, pub account: IntegrationAccount, pub enabled: bool, - #[serde(rename = "expire_behaviour")] - pub expire_behaviour: u64, + #[serde(rename = "expire_behaviour")] pub expire_behaviour: u64, pub expire_grace_period: u64, pub kind: String, pub name: String, diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index f1a5114..370f555 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -168,9 +168,10 @@ impl Member { let default = Colour::default(); - roles.iter().find(|r| r.colour.0 != default.0).map( - |r| r.colour, - ) + roles + .iter() + .find(|r| r.colour.0 != default.0) + .map(|r| r.colour) } /// Calculates the member's display name. @@ -178,9 +179,10 @@ impl Member { /// The nickname takes priority over the member's username if it exists. #[inline] pub fn display_name(&self) -> Cow<String> { - self.nick.as_ref().map(Cow::Borrowed).unwrap_or_else(|| { - Cow::Owned(self.user.read().unwrap().name.clone()) - }) + self.nick + .as_ref() + .map(Cow::Borrowed) + .unwrap_or_else(|| Cow::Owned(self.user.read().unwrap().name.clone())) } /// Returns the DiscordTag of a Member, taking possible nickname into account. @@ -202,7 +204,7 @@ impl Member { /// [`Guild::edit_member`]: ../model/struct.Guild.html#method.edit_member /// [`EditMember`]: ../builder/struct.EditMember.html #[cfg(feature = "cache")] - pub fn edit<F: FnOnce(EditMember) -> EditMember>(&self, f: F) -> Result<()> { +pub fn edit<F: FnOnce(EditMember) -> EditMember>(&self, f: F) -> Result<()>{ let map = f(EditMember::default()).0; http::edit_member(self.guild_id.0, self.user.read().unwrap().id.0, &map) @@ -246,11 +248,12 @@ impl Member { { let req = permissions::KICK_MEMBERS; - let has_perms = CACHE.read().unwrap().guilds.get(&self.guild_id).map( - |guild| { - guild.read().unwrap().has_perms(req) - }, - ); + let has_perms = CACHE + .read() + .unwrap() + .guilds + .get(&self.guild_id) + .map(|guild| guild.read().unwrap().has_perms(req)); if let Some(Ok(false)) = has_perms { return Err(Error::Model(ModelError::InvalidPermissions(req))); @@ -285,10 +288,10 @@ impl Member { let guild = guild.read().unwrap(); - Ok(guild.permissions_for( - ChannelId(guild.id.0), - self.user.read().unwrap().id, - )) + Ok( + guild + .permissions_for(ChannelId(guild.id.0), self.user.read().unwrap().id), + ) } /// Removes a [`Role`] from the member, editing its roles in-place if the diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index 3fc2a86..7fdd7d4 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -168,10 +168,7 @@ impl Guild { None => return Err(Error::Model(ModelError::ItemMissing)), }; - let perms = self.permissions_for( - default_channel.id, - member.user.read().unwrap().id, - ); + let perms = self.permissions_for(default_channel.id, member.user.read().unwrap().id); permissions.remove(perms); Ok(permissions.is_empty()) @@ -445,7 +442,7 @@ impl Guild { /// /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] - pub fn delete_integration<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()> { +pub fn delete_integration<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()>{ self.id.delete_integration(integration_id) } @@ -629,9 +626,9 @@ impl Guild { /// Returns the formatted URL of the guild's icon, if one exists. pub fn icon_url(&self) -> Option<String> { - self.icon.as_ref().map( - |icon| format!(cdn!("/icons/{}/{}.webp"), self.id, icon), - ) + self.icon + .as_ref() + .map(|icon| format!(cdn!("/icons/{}/{}.webp"), self.id, icon)) } /// Gets all integration of the guild. @@ -709,10 +706,8 @@ impl Guild { for (&id, member) in &self.members { match self.presences.get(&id) { - Some(presence) => { - if status == presence.status { - members.push(member); - } + Some(presence) => if status == presence.status { + members.push(member); }, None => continue, } @@ -800,8 +795,8 @@ impl Guild { Some(everyone) => everyone, None => { error!("(╯°□°)╯︵ ┻━┻ @everyone role ({}) missing in '{}'", - self.id, - self.name); + self.id, + self.name); return Permissions::empty(); }, @@ -820,9 +815,9 @@ impl Guild { permissions |= role.permissions; } else { warn!("(╯°□°)╯︵ ┻━┻ {} on {} has non-existent role {:?}", - member.user.read().unwrap().id, - self.id, - role); + member.user.read().unwrap().id, + self.id, + role); } } @@ -836,8 +831,8 @@ impl Guild { // If this is a text channel, then throw out voice permissions. if channel.kind == ChannelType::Text { - permissions &= !(CONNECT | SPEAK | MUTE_MEMBERS | DEAFEN_MEMBERS | MOVE_MEMBERS | - USE_VAD); + permissions &= + !(CONNECT | SPEAK | MUTE_MEMBERS | DEAFEN_MEMBERS | MOVE_MEMBERS | USE_VAD); } // Apply the permission overwrites for the channel for each of the @@ -868,8 +863,8 @@ impl Guild { } } else { warn!("(╯°□°)╯︵ ┻━┻ Guild {} does not contain channel {}", - self.id, - channel_id); + self.id, + channel_id); } // The default channel is always readable. @@ -963,9 +958,9 @@ impl Guild { /// Returns the formatted URL of the guild's splash image, if one exists. pub fn splash_url(&self) -> Option<String> { - self.icon.as_ref().map( - |icon| format!(cdn!("/splashes/{}/{}.webp"), self.id, icon), - ) + self.icon + .as_ref() + .map(|icon| format!(cdn!("/splashes/{}/{}.webp"), self.id, icon)) } /// Starts an integration sync for the given integration Id. @@ -974,7 +969,7 @@ impl Guild { /// /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] - pub fn start_integration_sync<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()> { +pub fn start_integration_sync<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()>{ self.id.start_integration_sync(integration_id) } @@ -1044,18 +1039,16 @@ impl<'de> Deserialize<'de> for Guild { fn deserialize<D: Deserializer<'de>>(deserializer: D) -> StdResult<Self, D::Error> { let mut map = JsonMap::deserialize(deserializer)?; - let id = map.get("id").and_then(|x| x.as_str()).and_then(|x| { - x.parse::<u64>().ok() - }); + let id = map.get("id") + .and_then(|x| x.as_str()) + .and_then(|x| x.parse::<u64>().ok()); if let Some(guild_id) = id { if let Some(array) = map.get_mut("channels").and_then(|x| x.as_array_mut()) { for value in array { if let Some(channel) = value.as_object_mut() { - channel.insert( - "guild_id".to_owned(), - Value::Number(Number::from(guild_id)), - ); + channel + .insert("guild_id".to_owned(), Value::Number(Number::from(guild_id))); } } } @@ -1063,21 +1056,16 @@ impl<'de> Deserialize<'de> for Guild { if let Some(array) = map.get_mut("members").and_then(|x| x.as_array_mut()) { for value in array { if let Some(member) = value.as_object_mut() { - member.insert( - "guild_id".to_owned(), - Value::Number(Number::from(guild_id)), - ); + member + .insert("guild_id".to_owned(), Value::Number(Number::from(guild_id))); } } } } let afk_channel_id = match map.remove("afk_channel_id") { - Some(v) => { - serde_json::from_value::<Option<ChannelId>>(v).map_err( - DeError::custom, - )? - }, + Some(v) => serde_json::from_value::<Option<ChannelId>>(v) + .map_err(DeError::custom)?, None => None, }; let afk_timeout = map.remove("afk_timeout") @@ -1229,9 +1217,9 @@ pub struct GuildInfo { impl GuildInfo { /// Returns the formatted URL of the guild's icon, if the guild has an icon. pub fn icon_url(&self) -> Option<String> { - self.icon.as_ref().map( - |icon| format!(cdn!("/icons/{}/{}.webp"), self.id, icon), - ) + self.icon + .as_ref() + .map(|icon| format!(cdn!("/icons/{}/{}.webp"), self.id, icon)) } } @@ -1251,9 +1239,9 @@ impl From<u64> for GuildContainer { impl InviteGuild { /// Returns the formatted URL of the guild's splash image, if one exists. pub fn splash_url(&self) -> Option<String> { - self.icon.as_ref().map( - |icon| format!(cdn!("/splashes/{}/{}.webp"), self.id, icon), - ) + self.icon + .as_ref() + .map(|icon| format!(cdn!("/splashes/{}/{}.webp"), self.id, icon)) } } diff --git a/src/model/guild/partial_guild.rs b/src/model/guild/partial_guild.rs index 799f6b6..1690167 100644 --- a/src/model/guild/partial_guild.rs +++ b/src/model/guild/partial_guild.rs @@ -16,16 +16,14 @@ pub struct PartialGuild { pub default_message_notifications: u64, pub embed_channel_id: Option<ChannelId>, pub embed_enabled: bool, - #[serde(deserialize_with = "deserialize_emojis")] - pub emojis: HashMap<EmojiId, Emoji>, + #[serde(deserialize_with = "deserialize_emojis")] pub emojis: HashMap<EmojiId, Emoji>, pub features: Vec<Feature>, pub icon: Option<String>, pub mfa_level: u64, pub name: String, pub owner_id: UserId, pub region: String, - #[serde(deserialize_with = "deserialize_roles")] - pub roles: HashMap<RoleId, Role>, + #[serde(deserialize_with = "deserialize_roles")] pub roles: HashMap<RoleId, Role>, pub splash: Option<String>, pub verification_level: VerificationLevel, } @@ -152,7 +150,7 @@ impl PartialGuild { /// [`Guild::create_role`]: struct.Guild.html#method.create_role /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[inline] - pub fn create_role<F: FnOnce(EditRole) -> EditRole>(&self, f: F) -> Result<Role> { +pub fn create_role<F: FnOnce(EditRole) -> EditRole>(&self, f: F) -> Result<Role>{ self.id.create_role(f) } @@ -180,7 +178,7 @@ impl PartialGuild { /// /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] - pub fn delete_integration<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()> { +pub fn delete_integration<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()>{ self.id.delete_integration(integration_id) } @@ -317,9 +315,9 @@ impl PartialGuild { /// Returns a formatted URL of the guild's icon, if the guild has an icon. pub fn icon_url(&self) -> Option<String> { - self.icon.as_ref().map(|icon| { - format!(cdn!("/icons/{}/{}.webp"), self.id, icon) - }) + self.icon + .as_ref() + .map(|icon| format!(cdn!("/icons/{}/{}.webp"), self.id, icon)) } /// Gets all integration of the guild. @@ -419,9 +417,9 @@ impl PartialGuild { /// Returns the formatted URL of the guild's splash image, if one exists. pub fn splash_url(&self) -> Option<String> { - self.icon.as_ref().map(|icon| { - format!(cdn!("/splashes/{}/{}.webp"), self.id, icon) - }) + self.icon + .as_ref() + .map(|icon| format!(cdn!("/splashes/{}/{}.webp"), self.id, icon)) } /// Starts an integration sync for the given integration Id. @@ -430,7 +428,7 @@ impl PartialGuild { /// /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] - pub fn start_integration_sync<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()> { +pub fn start_integration_sync<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()>{ self.id.start_integration_sync(integration_id) } diff --git a/src/model/guild/role.rs b/src/model/guild/role.rs index 5956113..5a6f75c 100644 --- a/src/model/guild/role.rs +++ b/src/model/guild/role.rs @@ -92,10 +92,9 @@ impl Role { /// [`Role`]: struct.Role.html /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[cfg(all(feature = "builder", feature = "cache"))] - pub fn edit<F: FnOnce(EditRole) -> EditRole>(&self, f: F) -> Result<Role> { - self.find_guild().and_then( - |guild_id| guild_id.edit_role(self.id, f), - ) +pub fn edit<F: FnOnce(EditRole) -> EditRole>(&self, f: F) -> Result<Role>{ + self.find_guild() + .and_then(|guild_id| guild_id.edit_role(self.id, f)) } /// Searches the cache for the guild that owns the role. |