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/role.rs | |
| 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/role.rs')
| -rw-r--r-- | src/model/guild/role.rs | 37 |
1 files changed, 34 insertions, 3 deletions
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), + } + } +} |