aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoracdenisSK <[email protected]>2017-12-06 18:49:51 +0100
committeracdenisSK <[email protected]>2017-12-06 18:49:51 +0100
commit0525ede086ccffa5781c9a1876a368ac3531813e (patch)
treee987eb3590cb8a9497b625287267553dd17bb12a
parentMake the comment for the `AsRef` impl more clear (diff)
downloadserenity-0525ede086ccffa5781c9a1876a368ac3531813e.tar.xz
serenity-0525ede086ccffa5781c9a1876a368ac3531813e.zip
Fall back to `str::parse` on `ChannelId` as well
Also give an actual error type for `Channel` too.
-rw-r--r--src/model/misc.rs63
1 files changed, 56 insertions, 7 deletions
diff --git a/src/model/misc.rs b/src/model/misc.rs
index 0f191bc..95c3fa8 100644
--- a/src/model/misc.rs
+++ b/src/model/misc.rs
@@ -228,25 +228,74 @@ impl FromStr for EmojiIdentifier {
}
#[cfg(all(feature = "model", feature = "utils"))]
+#[derive(Debug)]
+pub enum ChannelIdParseError {
+ InvalidFormat,
+}
+
+#[cfg(all(feature = "model", feature = "utils"))]
+impl fmt::Display for ChannelIdParseError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.description()) }
+}
+
+#[cfg(all(feature = "model", feature = "utils"))]
+impl StdError for ChannelIdParseError {
+ fn description(&self) -> &str {
+ use self::ChannelIdParseError::*;
+
+ match *self {
+ InvalidFormat => "invalid channel id format",
+ }
+ }
+}
+
+#[cfg(all(feature = "model", feature = "utils"))]
impl FromStr for ChannelId {
- type Err = ();
+ type Err = ChannelIdParseError;
+
+ fn from_str(s: &str) -> StdResult<Self, Self::Err> {
+ Ok(match utils::parse_channel(s) {
+ Some(channel) => ChannelId(channel),
+ None => s.parse::<u64>().map(ChannelId).map_err(|_| ChannelIdParseError::InvalidFormat)?,
+ })
+ }
+}
+
+#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
+#[derive(Debug)]
+pub enum ChannelParseError {
+ NotPresentInCache,
+ InvalidChannel,
+}
+
+#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
+impl fmt::Display for ChannelParseError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.description()) }
+}
+
+#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
+impl StdError for ChannelParseError {
+ fn description(&self) -> &str {
+ use self::ChannelParseError::*;
- fn from_str(s: &str) -> StdResult<Self, ()> {
- utils::parse_channel(s).ok_or_else(|| ()).map(ChannelId)
+ match *self {
+ NotPresentInCache => "not present in cache",
+ InvalidChannel => "invalid channel",
+ }
}
}
#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
impl FromStr for Channel {
- type Err = ();
+ type Err = ChannelParseError;
- fn from_str(s: &str) -> StdResult<Self, ()> {
+ fn from_str(s: &str) -> StdResult<Self, Self::Err> {
match utils::parse_channel(s) {
Some(x) => match ChannelId(x).find() {
Some(channel) => Ok(channel),
- _ => Err(()),
+ _ => Err(ChannelParseError::NotPresentInCache),
},
- _ => Err(()),
+ _ => Err(ChannelParseError::InvalidChannel),
}
}
}