aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLakelezz <[email protected]>2018-08-12 21:43:59 +0200
committerzeyla <[email protected]>2018-08-12 12:43:59 -0700
commit71edc3a11ac450728bca19ca7cab7c84079d59f0 (patch)
treebc6197f96ba9118ffa0b104c8434585d38bb6032 /src
parentRevert "Send silence frames upon connection (Fix #301)" (diff)
downloadserenity-71edc3a11ac450728bca19ca7cab7c84079d59f0.tar.xz
serenity-71edc3a11ac450728bca19ca7cab7c84079d59f0.zip
Use `to_`- and `as_`-methods instead of `get` and `find` on Id newtypes
Diffstat (limited to 'src')
-rw-r--r--src/builder/edit_guild.rs4
-rw-r--r--src/framework/standard/mod.rs4
-rw-r--r--src/model/channel/channel_id.rs28
-rw-r--r--src/model/channel/message.rs4
-rw-r--r--src/model/channel/mod.rs30
-rw-r--r--src/model/channel/reaction.rs4
-rw-r--r--src/model/guild/guild_id.rs28
-rw-r--r--src/model/guild/member.rs8
-rw-r--r--src/model/guild/mod.rs4
-rw-r--r--src/model/guild/partial_guild.rs6
-rw-r--r--src/model/guild/role.rs37
-rw-r--r--src/model/id.rs12
-rw-r--r--src/model/misc.rs17
-rw-r--r--src/model/user.rs27
-rw-r--r--src/model/webhook.rs12
15 files changed, 173 insertions, 52 deletions
diff --git a/src/builder/edit_guild.rs b/src/builder/edit_guild.rs
index 5db2051..ba87f0c 100644
--- a/src/builder/edit_guild.rs
+++ b/src/builder/edit_guild.rs
@@ -65,7 +65,7 @@ impl EditGuild {
/// # use std::error::Error;
/// #
/// # fn try_main() -> Result<(), Box<Error>> {
- /// # let mut guild = GuildId(0).get()?;
+ /// # let mut guild = GuildId(0).to_partial_guild()?;
/// use serenity::utils;
///
/// // assuming a `guild` has already been bound
@@ -126,7 +126,7 @@ impl EditGuild {
/// # use std::error::Error;
/// #
/// # fn try_main() -> Result<(), Box<Error>> {
- /// # let mut guild = GuildId(0).get()?;
+ /// # let mut guild = GuildId(0).to_partial_guild()?;
/// use serenity::model::guild::Region;
///
/// // assuming a `guild` has already been bound
diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs
index 86b8c59..9f5b9a9 100644
--- a/src/framework/standard/mod.rs
+++ b/src/framework/standard/mod.rs
@@ -478,7 +478,7 @@ impl StandardFramework {
return true;
}
- if let Some(guild) = guild_id.find() {
+ if let Some(guild) = guild_id.to_guild_cached() {
return self.configuration
.blocked_users
.contains(&guild.with(|g| g.owner_id));
@@ -873,7 +873,7 @@ impl StandardFramework {
///
/// client.with_framework(StandardFramework::new()
/// .before(|_, msg, cmd_name| {
- /// if let Ok(channel) = msg.channel_id.get() {
+ /// if let Ok(channel) = msg.channel_id.to_channel() {
/// // Don't run unless in nsfw channel
/// if !channel.is_nsfw() {
/// return false;
diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs
index 6715562..a897002 100644
--- a/src/model/channel/channel_id.rs
+++ b/src/model/channel/channel_id.rs
@@ -285,14 +285,35 @@ impl ChannelId {
/// Search the cache for the channel with the Id.
#[cfg(feature = "cache")]
- pub fn find(&self) -> Option<Channel> { CACHE.read().channel(*self) }
+ #[deprecated(since = "0.5.8", note = "Use the `to_channel_cached`-method instead.")]
+ pub fn find(&self) -> Option<Channel> { self.to_channel_cached() }
+
+ /// Attempts to find a [`Channel`] by its Id in the cache.
+ ///
+ /// [`Channel`]: ../channel/enum.Channel.html
+ #[cfg(feature = "cache")]
+ #[inline]
+ pub fn to_channel_cached(self) -> Option<Channel> { CACHE.read().channel(self) }
/// Search the cache for the channel. If it can't be found, the channel is
/// requested over REST.
+ #[deprecated(since = "0.5.8", note = "Use the `to_channel`-method instead.")]
pub fn get(&self) -> Result<Channel> {
+ self.to_channel()
+ }
+
+ /// First attempts to find a [`Channel`] by its Id in the cache,
+ /// upon failure requests it via the REST API.
+ ///
+ /// **Note**: If the cache is not enabled,
+ /// REST API will be used only.
+ ///
+ /// [`Channel`]: ../channel/enum.Channel.html
+ #[inline]
+ pub fn to_channel(self) -> Result<Channel> {
#[cfg(feature = "cache")]
{
- if let Some(channel) = CACHE.read().channel(*self) {
+ if let Some(channel) = CACHE.read().channel(self) {
return Ok(channel);
}
}
@@ -303,6 +324,7 @@ impl ChannelId {
/// Gets all of the channel's invites.
///
/// Requires the [Manage Channels] permission.
+ ///
/// [Manage Channels]: ../permissions/struct.Permissions.html#associatedconstant.MANAGE_CHANNELS
#[inline]
pub fn invites(&self) -> Result<Vec<RichInvite>> { http::get_channel_invites(self.0) }
@@ -363,7 +385,7 @@ impl ChannelId {
use self::Channel::*;
let finding = feature_cache! {{
- Some(self.find())
+ Some(self.to_channel_cached())
} else {
None
}};
diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs
index 8b580ff..d1c55b7 100644
--- a/src/model/channel/message.rs
+++ b/src/model/channel/message.rs
@@ -287,7 +287,7 @@ impl Message {
for id in &self.mention_roles {
let mention = id.mention();
- if let Some(role) = id.find() {
+ if let Some(role) = id.to_role_cached() {
result = result.replace(&mention, &format!("@{}", role.name));
} else {
result = result.replace(&mention, "@deleted-role");
@@ -429,7 +429,7 @@ impl Message {
///
/// [`ModelError::InvalidPermissions`]: ../error/enum.Error.html#variant.InvalidPermissions
/// [`Emoji`]: ../guild/struct.Emoji.html
- /// [Add Reactions]:
+ /// [Add Reactions]:
/// ../permissions/struct.Permissions.html#associatedconstant.ADD_REACTIONS
/// [permissions]: ../permissions/index.html
#[inline]
diff --git a/src/model/channel/mod.rs b/src/model/channel/mod.rs
index 05a117d..73e2ff3 100644
--- a/src/model/channel/mod.rs
+++ b/src/model/channel/mod.rs
@@ -34,6 +34,13 @@ use http::AttachmentType;
#[cfg(feature = "model")]
use std::fmt::{Display, Formatter, Result as FmtResult};
+#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
+use std::str::FromStr;
+#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
+use model::misc::ChannelParseError;
+#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
+use utils::parse_channel;
+
/// A container for any channel.
#[derive(Clone, Debug)]
pub enum Channel {
@@ -74,7 +81,7 @@ impl Channel {
/// #
/// # #[cfg(feature = "model")]
/// # fn main() {
- /// # let channel = ChannelId(0).get().unwrap();
+ /// # let channel = ChannelId(0).to_channel().unwrap();
/// #
/// match channel.group() {
/// Some(group_lock) => {
@@ -116,7 +123,7 @@ impl Channel {
/// #
/// # #[cfg(feature = "model")]
/// # fn main() {
- /// # let channel = ChannelId(0).get().unwrap();
+ /// # let channel = ChannelId(0).to_channel().unwrap();
/// #
/// match channel.guild() {
/// Some(guild_lock) => {
@@ -154,7 +161,7 @@ impl Channel {
/// #
/// # #[cfg(feature = "model")]
/// # fn main() {
- /// # let channel = ChannelId(0).get().unwrap();
+ /// # let channel = ChannelId(0).to_channel().unwrap();
/// #
/// match channel.private() {
/// Some(private_lock) => {
@@ -195,7 +202,7 @@ impl Channel {
/// #
/// # #[cfg(feature = "model")]
/// # fn main() {
- /// # let channel = ChannelId(0).get().unwrap();
+ /// # let channel = ChannelId(0).to_channel().unwrap();
/// #
/// match channel.category() {
/// Some(category_lock) => {
@@ -793,3 +800,18 @@ mod test {
}
}
}
+
+#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
+impl FromStr for Channel {
+ type Err = ChannelParseError;
+
+ fn from_str(s: &str) -> StdResult<Self, Self::Err> {
+ match parse_channel(s) {
+ Some(x) => match ChannelId(x).to_channel_cached() {
+ Some(channel) => Ok(channel),
+ _ => Err(ChannelParseError::NotPresentInCache),
+ },
+ _ => Err(ChannelParseError::InvalidChannel),
+ }
+ }
+}
diff --git a/src/model/channel/reaction.rs b/src/model/channel/reaction.rs
index 94b3c32..c34eff7 100644
--- a/src/model/channel/reaction.rs
+++ b/src/model/channel/reaction.rs
@@ -51,7 +51,7 @@ impl Reaction {
/// [Read Message History]: ../permissions/struct.Permissions.html#associatedconstant.READ_MESSAGE_HISTORY
#[inline]
pub fn channel(&self) -> Result<Channel> {
- self.channel_id.get()
+ self.channel_id.to_channel()
}
/// Deletes the reaction, but only if the current user is the user who made
@@ -123,7 +123,7 @@ impl Reaction {
/// the REST API for the user.
#[inline]
pub fn user(&self) -> Result<User> {
- self.user_id.get()
+ self.user_id.to_user()
}
/// Retrieves the list of [`User`]s who have reacted to a [`Message`] with a
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),
+ }
+ }
+}
diff --git a/src/model/id.rs b/src/model/id.rs
index 5ec5c75..a153d3b 100644
--- a/src/model/id.rs
+++ b/src/model/id.rs
@@ -16,6 +16,18 @@ macro_rules! id_u64 {
NaiveDateTime::from_timestamp(1_420_070_400 + offset as i64, 0)
}
+
+ /// Immutably borrow inner Id.
+ #[inline]
+ pub fn as_u64(&self) -> &u64 {
+ &self.0
+ }
+
+ /// Mutably borrow inner Id.
+ #[inline]
+ pub fn as_mut_u64(&mut self) -> &mut u64 {
+ &mut self.0
+ }
}
// This is a hack so functions can accept iterators that either:
diff --git a/src/model/misc.rs b/src/model/misc.rs
index 5c0825a..ff0f3de 100644
--- a/src/model/misc.rs
+++ b/src/model/misc.rs
@@ -119,7 +119,7 @@ impl FromStr for User {
fn from_str(s: &str) -> StdResult<Self, Self::Err> {
match utils::parse_username(s) {
Some(x) => UserId(x as u64)
- .get()
+ .to_user()
.map_err(|e| UserParseError::Rest(Box::new(e))),
_ => Err(UserParseError::InvalidUsername),
}
@@ -190,21 +190,6 @@ macro_rules! impl_from_str {
}
}
}
-
- #[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
- impl FromStr for $struct {
- type Err = $err;
-
- fn from_str(s: &str) -> StdResult<Self, Self::Err> {
- match utils::$parse_fn(s) {
- Some(x) => match $id(x).find() {
- Some(user) => Ok(user),
- _ => Err($err::NotPresentInCache),
- },
- _ => Err($err::$invalid_variant),
- }
- }
- }
)*
};
}
diff --git a/src/model/user.rs b/src/model/user.rs
index 019bec9..694f098 100644
--- a/src/model/user.rs
+++ b/src/model/user.rs
@@ -661,7 +661,7 @@ impl User {
/// println!("{:?}", client.start());
/// ```
pub fn refresh(&mut self) -> Result<()> {
- self.id.get().map(|replacement| {
+ self.id.to_user().map(|replacement| {
mem::replace(self, replacement);
()
@@ -738,17 +738,38 @@ impl UserId {
/// Search the cache for the user with the Id.
#[cfg(feature = "cache")]
- pub fn find(&self) -> Option<Arc<RwLock<User>>> { CACHE.read().user(*self) }
+ #[deprecated(since = "0.5.8", note = "Use the `to_user_cached`-method instead.")]
+ pub fn find(&self) -> Option<Arc<RwLock<User>>> { self.to_user_cached() }
+
+ /// Attempts to find a [`User`] by its Id in the cache.
+ ///
+ /// [`User`]: ../user/struct.User.html
+ #[cfg(feature = "cache")]
+ #[inline]
+ pub fn to_user_cached(self) -> Option<Arc<RwLock<User>>> { CACHE.read().user(self) }
/// Gets a user by its Id from either the cache or the REST API.
///
/// Searches the cache for the user first, if the cache is enabled. If the
/// user was not found, then the user is searched via the REST API.
#[inline]
+ #[deprecated(since = "0.5.8", note = "Use the `to_user`-method instead.")]
pub fn get(&self) -> Result<User> {
+ self.to_user()
+ }
+
+ /// First attempts to find a [`User`] by its Id in the cache,
+ /// upon failure requests it via the REST API.
+ ///
+ /// **Note**: If the cache is not enabled,
+ /// REST API will be used only.
+ ///
+ /// [`User`]: ../user/struct.User.html
+ #[inline]
+ pub fn to_user(self) -> Result<User> {
#[cfg(feature = "cache")]
{
- if let Some(user) = CACHE.read().user(*self) {
+ if let Some(user) = CACHE.read().user(self) {
return Ok(user.read().clone());
}
}
diff --git a/src/model/webhook.rs b/src/model/webhook.rs
index fca70fe..715b18b 100644
--- a/src/model/webhook.rs
+++ b/src/model/webhook.rs
@@ -226,5 +226,15 @@ impl WebhookId {
///
/// [Manage Webhooks]: ../../model/permissions/struct.Permissions.html#associatedconstant.MANAGE_WEBHOOKS
#[inline]
- pub fn get(self) -> Result<Webhook> { http::get_webhook(self.0) }
+ #[deprecated(since = "0.5.8", note = "Use the `to_webhook`-method instead.")]
+ pub fn get(self) -> Result<Webhook> { self.to_webhook() }
+
+ /// Requests [`Webhook`] over REST API.
+ ///
+ /// **Note**: Requires the [Manage Webhooks] permission.
+ ///
+ /// [`Webhook`]: struct.Webhook.html
+ /// [Manage Webhooks]: ../../model/permissions/struct.Permissions.html#associatedconstant.MANAGE_WEBHOOKS
+ #[inline]
+ pub fn to_webhook(self) -> Result<Webhook> { http::get_webhook(self.0) }
}