Skip to content

Commit

Permalink
Clean up macro usage in model::misc (#1822)
Browse files Browse the repository at this point in the history
Refactor `impl_from_str!` to cut down on complexity, and move the error
handling logic for `Channel` and `Role` into their respective modules.

Furthermore, remove the unused `misc::UserParseError` enum, as it's long
been replaced by `argument_convert::UserParseError` in use.
  • Loading branch information
mkrasnitski authored and arqunis committed Apr 18, 2022
1 parent 8c9670f commit 5a0e8f4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 59 deletions.
24 changes: 22 additions & 2 deletions src/model/channel/mod.rs
Expand Up @@ -11,6 +11,8 @@ mod partial_channel;
mod private_channel;
mod reaction;

#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
use std::error::Error as StdError;
use std::fmt;

use serde::de::{Error as DeError, Unexpected};
Expand All @@ -32,8 +34,6 @@ use crate::cache::Cache;
use crate::cache::FromStrAndCache;
#[cfg(feature = "model")]
use crate::http::CacheHttp;
#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
use crate::model::misc::ChannelParseError;
use crate::model::utils::is_false;
use crate::model::Timestamp;
#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
Expand Down Expand Up @@ -564,6 +564,26 @@ mod test {
}
}

#[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 {
match self {
ChannelParseError::NotPresentInCache => f.write_str("not present in cache"),
ChannelParseError::InvalidChannel => f.write_str("invalid channel"),
}
}
}

#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
impl StdError for ChannelParseError {}

#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
impl FromStrAndCache for Channel {
type Err = ChannelParseError;
Expand Down
24 changes: 22 additions & 2 deletions src/model/guild/role.rs
@@ -1,4 +1,6 @@
use std::cmp::Ordering;
#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
use std::error::Error as StdError;
use std::fmt;

#[cfg(feature = "model")]
Expand All @@ -11,8 +13,6 @@ use crate::cache::FromStrAndCache;
use crate::http::Http;
#[cfg(all(feature = "cache", feature = "model"))]
use crate::internal::prelude::*;
#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
use crate::model::misc::RoleParseError;
use crate::model::prelude::*;
use crate::model::utils::is_false;
#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
Expand Down Expand Up @@ -261,6 +261,26 @@ impl<'a> From<&'a Role> for RoleId {
}
}

#[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 {
match self {
RoleParseError::NotPresentInCache => f.write_str("not present in cache"),
RoleParseError::InvalidRole => f.write_str("invalid role"),
}
}
}

#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
impl StdError for RoleParseError {}

#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
impl FromStrAndCache for Role {
type Err = RoleParseError;
Expand Down
58 changes: 3 additions & 55 deletions src/model/misc.rs
Expand Up @@ -168,29 +168,8 @@ mentionable!(value:
Emoji, (value.id, value.animated);
);

#[cfg(all(feature = "model", feature = "utils"))]
#[derive(Debug)]
#[non_exhaustive]
pub enum UserParseError {
InvalidUsername,
Rest(Box<Error>),
}

#[cfg(all(feature = "model", feature = "utils"))]
impl fmt::Display for UserParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
UserParseError::InvalidUsername => f.write_str("invalid username"),
UserParseError::Rest(_) => f.write_str("could not fetch"),
}
}
}

#[cfg(all(feature = "model", feature = "utils"))]
impl StdError for UserParseError {}

macro_rules! impl_from_str {
(id: $($id:ident, $err:ident, $parse_function:ident;)*) => {
($($id:ident, $err:ident, $parse_function:ident;)+) => {
$(
#[cfg(all(feature = "model", feature = "utils"))]
#[derive(Debug)]
Expand All @@ -201,9 +180,7 @@ macro_rules! impl_from_str {
#[cfg(all(feature = "model", feature = "utils"))]
impl fmt::Display for $err {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
$err::InvalidFormat => f.write_str("invalid id format"),
}
f.write_str("invalid id format")
}
}

Expand All @@ -225,43 +202,14 @@ macro_rules! impl_from_str {
}
)*
};

(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 {
match self {
$err::NotPresentInCache => f.write_str("not present in cache"),
$err::$invalid_variant => f.write_str($desc),
}
}
}

#[cfg(all(feature = "cache", feature = "model", feature = "utils"))]
impl StdError for $err {}
)*
};
}

impl_from_str! { id:
impl_from_str! {
UserId, UserIdParseError, parse_username;
RoleId, RoleIdParseError, parse_role;
ChannelId, ChannelIdParseError, parse_channel;
}

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 animated state, Id, and name are known.
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct EmojiIdentifier {
Expand Down

0 comments on commit 5a0e8f4

Please sign in to comment.