diff options
| author | acdenisSK <[email protected]> | 2017-11-30 22:29:57 +0100 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2017-11-30 22:29:57 +0100 |
| commit | 56b72884208df34351ac41f02b3e6731af8afd25 (patch) | |
| tree | 389b2b2575f5e256c0aa8970d1eec2113e12bf35 /src/model/utils.rs | |
| parent | Fix whitespacing and change `and` to `or`. (#228) (diff) | |
| download | serenity-56b72884208df34351ac41f02b3e6731af8afd25.tar.xz serenity-56b72884208df34351ac41f02b3e6731af8afd25.zip | |
Tune down repetition with a macro
Diffstat (limited to 'src/model/utils.rs')
| -rw-r--r-- | src/model/utils.rs | 87 |
1 files changed, 31 insertions, 56 deletions
diff --git a/src/model/utils.rs b/src/model/utils.rs index 26ec7fe..af54b37 100644 --- a/src/model/utils.rs +++ b/src/model/utils.rs @@ -187,62 +187,37 @@ pub fn user_has_perms(channel_id: ChannelId, mut permissions: Permissions) -> Re Ok(permissions.is_empty()) } -#[derive(Debug)] -pub struct U16Visitor; - -impl<'de> Visitor<'de> for U16Visitor { - type Value = u16; - - fn expecting(&self, formatter: &mut Formatter) -> FmtResult { - formatter.write_str("identifier") - } - - fn visit_str<E: DeError>(self, v: &str) -> StdResult<Self::Value, E> { - match v.parse::<u16>() { - Ok(v) => Ok(v), - Err(_) => { - let mut s = String::new(); - s.push_str("Unknown "); - s.push_str(stringify!($name)); - s.push_str(" value: "); - s.push_str(v); - - Err(DeError::custom(s)) - }, - } +macro_rules! num_visitors { + ($($visitor:ident: $type:ty),*) => { + $( + #[derive(Debug)] + pub struct $visitor; + + impl<'de> Visitor<'de> for $visitor { + type Value = $type; + + fn expecting(&self, formatter: &mut Formatter) -> FmtResult { + formatter.write_str("identifier") + } + + fn visit_str<E: DeError>(self, v: &str) -> StdResult<Self::Value, E> { + v.parse::<$type>().map_err(|_| { + let mut s = String::with_capacity(32); + s.push_str("Unknown "); + s.push_str(stringify!($type)); + s.push_str(" value: "); + s.push_str(v); + + DeError::custom(s) + }) + } + + fn visit_i64<E: DeError>(self, v: i64) -> StdResult<Self::Value, E> { Ok(v as $type) } + + fn visit_u64<E: DeError>(self, v: u64) -> StdResult<Self::Value, E> { Ok(v as $type) } + } + )* } - - fn visit_i64<E: DeError>(self, v: i64) -> StdResult<Self::Value, E> { Ok(v as u16) } - - fn visit_u64<E: DeError>(self, v: u64) -> StdResult<Self::Value, E> { Ok(v as u16) } } -#[derive(Debug)] -pub struct U64Visitor; - -impl<'de> Visitor<'de> for U64Visitor { - type Value = u64; - - fn expecting(&self, formatter: &mut Formatter) -> FmtResult { - formatter.write_str("identifier") - } - - fn visit_str<E: DeError>(self, v: &str) -> StdResult<Self::Value, E> { - match v.parse::<u64>() { - Ok(v) => Ok(v), - Err(_) => { - let mut s = String::new(); - s.push_str("Unknown "); - s.push_str(stringify!($name)); - s.push_str(" value: "); - s.push_str(v); - - Err(DeError::custom(s)) - }, - } - } - - fn visit_i64<E: DeError>(self, v: i64) -> StdResult<Self::Value, E> { Ok(v as u64) } - - fn visit_u64<E: DeError>(self, v: u64) -> StdResult<Self::Value, E> { Ok(v) } -} +num_visitors!(U16Visitor: u16, U64Visitor: u64); |