diff options
| author | Zeyla Hellyer <[email protected]> | 2017-04-25 15:46:18 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-04-25 15:46:18 -0700 |
| commit | 9739d918931978f46cfb16ca1d860936d7a4a6dd (patch) | |
| tree | f8c6c7e3105747514adbce4a9975553a0cbc6af1 /src | |
| parent | Fix embed length check (diff) | |
| download | serenity-9739d918931978f46cfb16ca1d860936d7a4a6dd.tar.xz serenity-9739d918931978f46cfb16ca1d860936d7a4a6dd.zip | |
Fix decoding for `CurrentUser.discriminator`
Due to the serde 1.0 upgrade, integers are no longer automatically
deserialized from Strings. To resolve this, create a visitor that
performs the operation based on the input.
Diffstat (limited to 'src')
| -rw-r--r-- | src/model/user.rs | 2 | ||||
| -rw-r--r-- | src/model/utils.rs | 37 |
2 files changed, 39 insertions, 0 deletions
diff --git a/src/model/user.rs b/src/model/user.rs index 8f7349c..686d394 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -1,5 +1,6 @@ use serde_json; use std::{fmt, mem}; +use super::utils::deserialize_u16; use super::*; use time::Timespec; use ::client::rest::{self, GuildPagination}; @@ -54,6 +55,7 @@ pub struct CurrentUser { pub avatar: Option<String>, #[serde(default)] pub bot: bool, + #[serde(deserialize_with="deserialize_u16")] pub discriminator: u16, pub email: Option<String>, pub mfa_enabled: bool, diff --git a/src/model/utils.rs b/src/model/utils.rs index 309b9de..a01f134 100644 --- a/src/model/utils.rs +++ b/src/model/utils.rs @@ -115,6 +115,11 @@ pub fn deserialize_users<'de, D: Deserializer<'de>>(deserializer: D) Ok(users) } +pub fn deserialize_u16<'de, D: Deserializer<'de>>(deserializer: D) + -> StdResult<u16, D::Error> { + deserializer.deserialize_u16(U16Visitor) +} + pub fn deserialize_u64<'de, D: Deserializer<'de>>(deserializer: D) -> StdResult<u64, D::Error> { deserializer.deserialize_u64(U64Visitor) @@ -161,6 +166,38 @@ pub fn user_has_perms(channel_id: ChannelId, mut permissions: Permissions) -> Re Ok(permissions.is_empty()) } +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)) + }, + } + } + + 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) + } +} pub struct U64Visitor; |