diff options
| author | Lakelezz <[email protected]> | 2018-08-12 21:43:59 +0200 |
|---|---|---|
| committer | zeyla <[email protected]> | 2018-08-12 12:43:59 -0700 |
| commit | 71edc3a11ac450728bca19ca7cab7c84079d59f0 (patch) | |
| tree | bc6197f96ba9118ffa0b104c8434585d38bb6032 /src/model/guild | |
| parent | Revert "Send silence frames upon connection (Fix #301)" (diff) | |
| download | serenity-71edc3a11ac450728bca19ca7cab7c84079d59f0.tar.xz serenity-71edc3a11ac450728bca19ca7cab7c84079d59f0.zip | |
Use `to_`- and `as_`-methods instead of `get` and `find` on Id newtypes
Diffstat (limited to 'src/model/guild')
| -rw-r--r-- | src/model/guild/guild_id.rs | 28 | ||||
| -rw-r--r-- | src/model/guild/member.rs | 8 | ||||
| -rw-r--r-- | src/model/guild/mod.rs | 4 | ||||
| -rw-r--r-- | src/model/guild/partial_guild.rs | 6 | ||||
| -rw-r--r-- | src/model/guild/role.rs | 37 |
5 files changed, 66 insertions, 17 deletions
diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index 4b2bd21..4309349 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -402,17 +402,35 @@ impl GuildId { http::edit_role_position(self.0, role_id.0, position) } - /// Search the cache for the guild. #[cfg(feature = "cache")] - pub fn find(&self) -> Option<Arc<RwLock<Guild>>> { CACHE.read().guild(*self) } + #[deprecated(since = "0.5.8", note = "Use the `to_guild_cached`-method instead.")] + pub fn find(&self) -> Option<Arc<RwLock<Guild>>> { self.to_guild_cached() } + + /// Tries to find the [`Guild`] by its Id in the cache. + /// + /// [`Guild`]: ../guild/struct.Guild.html + #[cfg(feature = "cache")] + #[inline] + pub fn to_guild_cached(self) -> Option<Arc<RwLock<Guild>>> { CACHE.read().guild(self) } /// Requests the guild over REST. /// /// Note that this will not be a complete guild, as REST does not send /// all data with a guild retrieval. #[inline] - pub fn get(&self) -> Result<PartialGuild> { http::get_guild(self.0) } + #[deprecated(since = "0.5.8", note = "Use the `to_partial_guild`-method instead.")] + pub fn get(&self) -> Result<PartialGuild> { self.to_partial_guild() } + + /// Requests [`PartialGuild`] over REST API. + /// + /// **Note**: This will not be a [`Guild`], as the REST API does not send + /// all data with a guild retrieval. + /// + /// [`PartialGuild`]: ../guild/struct.PartialGuild.html + /// [`Guild`]: ../guild/struct.Guild.html + #[inline] + pub fn to_partial_guild(self) -> Result<PartialGuild> { http::get_guild(self.0) } /// Gets all integration of the guild. /// @@ -443,8 +461,8 @@ impl GuildId { #[inline] pub fn leave(&self) -> Result<()> { http::leave_guild(self.0) } - /// Gets a user's [`Member`] for the guild by Id. - /// + /// Gets a user's [`Member`] for the guild by Id. + /// /// If the cache feature is enabled the cache will be checked /// first. If not found it will resort to an http request. /// diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index 0b31ba5..0aa7dd0 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -191,7 +191,7 @@ impl Member { /// one returns `None`) #[cfg(feature = "cache")] pub fn default_channel(&self) -> Option<Arc<RwLock<GuildChannel>>> { - let guild = match self.guild_id.find() { + let guild = match self.guild_id.to_guild_cached() { Some(guild) => guild, None => return None, }; @@ -257,7 +257,7 @@ impl Member { /// role with the lowest ID is the highest. #[cfg(feature = "cache")] pub fn highest_role_info(&self) -> Option<(RoleId, i64)> { - let guild = self.guild_id.find()?; + let guild = self.guild_id.to_guild_cached()?; let reader = guild.try_read()?; let mut highest = None; @@ -356,7 +356,7 @@ impl Member { /// [`ModelError::ItemMissing`]: ../error/enum.Error.html#variant.ItemMissing #[cfg(feature = "cache")] pub fn permissions(&self) -> Result<Permissions> { - let guild = match self.guild_id.find() { + let guild = match self.guild_id.to_guild_cached() { Some(guild) => guild, None => return Err(From::from(ModelError::GuildNotFound)), }; @@ -427,7 +427,7 @@ impl Member { pub fn roles(&self) -> Option<Vec<Role>> { self .guild_id - .find() + .to_guild_cached() .map(|g| g .read() .roles diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index b5c6967..43cde4b 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -656,7 +656,7 @@ 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> { guild_id.into().to_partial_guild() } /// Returns which of two [`User`]s has a higher [`Member`] hierarchy. /// @@ -1518,7 +1518,7 @@ impl Guild { /// /// impl EventHandler for Handler { /// fn message(&self, _: Context, msg: Message) { - /// if let Some(arc) = msg.guild_id().unwrap().find() { + /// if let Some(arc) = msg.guild_id().unwrap().to_guild_cached() { /// if let Some(role) = arc.read().role_by_name("role_name") { /// println!("{:?}", role); /// } diff --git a/src/model/guild/partial_guild.rs b/src/model/guild/partial_guild.rs index 7fff7be..0fd20fb 100644 --- a/src/model/guild/partial_guild.rs +++ b/src/model/guild/partial_guild.rs @@ -241,7 +241,7 @@ impl PartialGuild { /// /// [`Emoji`]: struct.Emoji.html /// [`Emoji::edit`]: struct.Emoji.html#method.edit - /// [Manage Emojis]: + /// [Manage Emojis]: /// ../permissions/struct.Permissions.html#associatedconstant.MANAGE_EMOJIS #[inline] pub fn edit_emoji<E: Into<EmojiId>>(&self, emoji_id: E, name: &str) -> Result<Emoji> { @@ -292,7 +292,7 @@ impl PartialGuild { /// /// 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> { guild_id.into().to_partial_guild() } /// Kicks a [`Member`] from the guild. /// @@ -468,7 +468,7 @@ impl PartialGuild { /// /// impl EventHandler for Handler { /// fn message(&self, _: Context, msg: Message) { - /// let guild = msg.guild_id().unwrap().get().unwrap(); + /// let guild = msg.guild_id().unwrap().to_partial_guild().unwrap(); /// let possible_role = guild.role_by_name("role_name"); /// /// if let Some(role) = possible_role { diff --git a/src/model/guild/role.rs b/src/model/guild/role.rs index b66c0a0..ac969c0 100644 --- a/src/model/guild/role.rs +++ b/src/model/guild/role.rs @@ -8,6 +8,13 @@ use internal::prelude::*; #[cfg(all(feature = "cache", feature = "model"))] use {CACHE, http}; +#[cfg(all(feature = "cache", feature = "model", feature = "utils"))] +use std::str::FromStr; +#[cfg(all(feature = "cache", feature = "model", feature = "utils"))] +use model::misc::RoleParseError; +#[cfg(all(feature = "cache", feature = "model", feature = "utils"))] +use utils::parse_role; + /// Information about a role within a guild. A role represents a set of /// permissions, and can be attached to one or multiple users. A role has /// various miscellaneous configurations, such as being assigned a colour. Roles @@ -80,7 +87,7 @@ impl Role { /// /// ```rust,no_run /// # use serenity::model::id::RoleId; - /// # let role = RoleId(7).find().unwrap(); + /// # let role = RoleId(7).to_role_cached().unwrap(); /// // assuming a `role` has already been bound // /// role.edit(|r| r.hoist(true)); @@ -165,17 +172,26 @@ impl PartialOrd for Role { impl RoleId { /// Search the cache for the role. #[cfg(feature = "cache")] + #[deprecated(since = "0.5.8", note = "Use the `to_role_cached`-method instead.")] pub fn find(&self) -> Option<Role> { + self.to_role_cached() + } + + /// Tries to find the [`Role`] by its Id in the cache. + /// + /// [`Role`]: ../guild/struct.Role.html + #[cfg(feature = "cache")] + pub fn to_role_cached(self) -> Option<Role> { let cache = CACHE.read(); for guild in cache.guilds.values() { let guild = guild.read(); - if !guild.roles.contains_key(self) { + if !guild.roles.contains_key(&self) { continue; } - if let Some(role) = guild.roles.get(self) { + if let Some(role) = guild.roles.get(&self) { return Some(role.clone()); } } @@ -193,3 +209,18 @@ impl<'a> From<&'a Role> for RoleId { /// Gets the Id of a role. fn from(role: &Role) -> RoleId { role.id } } + +#[cfg(all(feature = "cache", feature = "model", feature = "utils"))] +impl FromStr for Role { + type Err = RoleParseError; + + fn from_str(s: &str) -> StdResult<Self, Self::Err> { + match parse_role(s) { + Some(x) => match RoleId(x).to_role_cached() { + Some(role) => Ok(role), + _ => Err(RoleParseError::NotPresentInCache), + }, + _ => Err(RoleParseError::InvalidRole), + } + } +} |