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/channel/mod.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/channel/mod.rs')
| -rw-r--r-- | src/model/channel/mod.rs | 30 |
1 files changed, 26 insertions, 4 deletions
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), + } + } +} |