aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoracdenisSK <[email protected]>2017-12-10 11:17:25 +0100
committeracdenisSK <[email protected]>2017-12-10 11:17:40 +0100
commit2be880c0b067c4119cb8c3afbe1be01e2a2f0969 (patch)
treefe6554f999894a6cdde287eb34eedf543e4d285c /src
parentShutdown everything on ShardManager::shutdown_all (diff)
downloadserenity-2be880c0b067c4119cb8c3afbe1be01e2a2f0969.tar.xz
serenity-2be880c0b067c4119cb8c3afbe1be01e2a2f0969.zip
Use a macro to generate FromStr impls
Diffstat (limited to 'src')
-rw-r--r--src/model/misc.rs269
1 files changed, 92 insertions, 177 deletions
diff --git a/src/model/misc.rs b/src/model/misc.rs
index 95c3fa8..b959115 100644
--- a/src/model/misc.rs
+++ b/src/model/misc.rs
@@ -96,111 +96,98 @@ impl FromStr for User {
}
}
-#[cfg(all(feature = "model", feature = "utils"))]
-#[derive(Debug)]
-pub enum UserIdParseError {
- InvalidFormat,
-}
-
-#[cfg(all(feature = "model", feature = "utils"))]
-impl fmt::Display for UserIdParseError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.description()) }
-}
-
-#[cfg(all(feature = "model", feature = "utils"))]
-impl StdError for UserIdParseError {
- fn description(&self) -> &str {
- use self::UserIdParseError::*;
-
- match *self {
- InvalidFormat => "invalid user id format",
- }
- }
-}
-
-#[cfg(all(feature = "model", feature = "utils"))]
-impl FromStr for UserId {
- type Err = UserIdParseError;
-
- fn from_str(s: &str) -> StdResult<Self, Self::Err> {
- Ok(match utils::parse_username(s) {
- Some(id) => UserId(id),
- None => s.parse::<u64>().map(UserId).map_err(|_| UserIdParseError::InvalidFormat)?,
- })
- }
-}
-
-#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
-#[derive(Debug)]
-pub enum RoleParseError {
- NotPresentInCache,
- InvalidRole,
-}
-
-#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
-impl fmt::Display for RoleParseError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.description()) }
-}
-
-#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
-impl StdError for RoleParseError {
- fn description(&self) -> &str {
- use self::RoleParseError::*;
-
- match *self {
- NotPresentInCache => "not present in cache",
- InvalidRole => "invalid role",
- }
- }
-}
-
-#[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 utils::parse_role(s) {
- Some(x) => match RoleId(x).find() {
- Some(user) => Ok(user),
- _ => Err(RoleParseError::NotPresentInCache),
- },
- _ => Err(RoleParseError::InvalidRole),
- }
- }
-}
-
-#[cfg(all(feature = "model", feature = "utils"))]
-#[derive(Debug)]
-pub enum RoleIdParseError {
- InvalidFormat,
-}
-
-#[cfg(all(feature = "model", feature = "utils"))]
-impl fmt::Display for RoleIdParseError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.description()) }
-}
-
-#[cfg(all(feature = "model", feature = "utils"))]
-impl StdError for RoleIdParseError {
- fn description(&self) -> &str {
- use self::RoleIdParseError::*;
-
- match *self {
- InvalidFormat => "invalid role id format",
- }
- }
-}
-
-#[cfg(all(feature = "model", feature = "utils"))]
-impl FromStr for RoleId {
- type Err = RoleIdParseError;
-
- fn from_str(s: &str) -> StdResult<Self, Self::Err> {
- Ok(match utils::parse_role(s) {
- Some(id) => RoleId(id),
- None => s.parse::<u64>().map(RoleId).map_err(|_| RoleIdParseError::InvalidFormat)?,
- })
- }
+macro_rules! impl_from_str {
+ (id: $($id:tt, $err:ident;)*) => {
+ $(
+ #[cfg(all(feature = "model", feature = "utils"))]
+ #[derive(Debug)]
+ pub enum $err {
+ InvalidFormat,
+ }
+
+ #[cfg(all(feature = "model", feature = "utils"))]
+ impl fmt::Display for $err {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.description()) }
+ }
+
+ #[cfg(all(feature = "model", feature = "utils"))]
+ impl StdError for $err {
+ fn description(&self) -> &str {
+ use self::$err::*;
+
+ match *self {
+ InvalidFormat => "invalid id format",
+ }
+ }
+ }
+
+ #[cfg(all(feature = "model", feature = "utils"))]
+ impl FromStr for $id {
+ type Err = $err;
+
+ fn from_str(s: &str) -> StdResult<Self, Self::Err> {
+ Ok(match utils::parse_username(s) {
+ Some(id) => $id(id),
+ None => s.parse::<u64>().map($id).map_err(|_| $err::InvalidFormat)?,
+ })
+ }
+ }
+ )*
+ };
+
+ (struct: $($struct:ty, $id:tt, $err:ident, $invalid_variant:tt, $parse_fn:ident, $desc:expr;)*) => {
+ $(
+ #[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
+ #[derive(Debug)]
+ pub enum $err {
+ NotPresentInCache,
+ $invalid_variant,
+ }
+
+ #[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
+ impl fmt::Display for $err {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.description()) }
+ }
+
+ #[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
+ impl StdError for $err {
+ fn description(&self) -> &str {
+ use self::$err::*;
+
+ match *self {
+ NotPresentInCache => "not present in cache",
+ $invalid_variant => $desc,
+ }
+ }
+ }
+
+ #[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),
+ }
+ }
+ }
+ )*
+ };
+}
+
+impl_from_str! { id:
+ UserId, UserIdParseError;
+ RoleId, RoleIdParseError;
+ ChannelId, ChannelIdParseError;
+}
+
+impl_from_str! { struct:
+ Channel, ChannelId, ChannelParseError, InvalidChannel, parse_channel, "invalid channel";
+ Role, RoleId, RoleParseError, InvalidRole, parse_role, "invalid role";
}
/// A version of an emoji used only when solely the Id and name are known.
@@ -227,78 +214,6 @@ impl FromStr for EmojiIdentifier {
fn from_str(s: &str) -> StdResult<Self, ()> { utils::parse_emoji(s).ok_or_else(|| ()) }
}
-#[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 = 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::*;
-
- match *self {
- NotPresentInCache => "not present in cache",
- InvalidChannel => "invalid channel",
- }
- }
-}
-
-#[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 utils::parse_channel(s) {
- Some(x) => match ChannelId(x).find() {
- Some(channel) => Ok(channel),
- _ => Err(ChannelParseError::NotPresentInCache),
- },
- _ => Err(ChannelParseError::InvalidChannel),
- }
- }
-}
/// A component that was affected during a service incident.
///