aboutsummaryrefslogtreecommitdiff
path: root/src/model/channel
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/model/channel
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/model/channel')
-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
4 files changed, 55 insertions, 11 deletions
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