From d0b64cd64a18a6116267fa09a837d62c19cced42 Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Sat, 10 Jun 2017 21:55:33 -0700 Subject: Fix negative nonces failing to deserialize Negative message nonces caused deserialization errors, as serde would not deserialize integers into strings. To fix this, change `Message::nonce` into an `Option` from an `Option`. This new `Snowflake` is a wrapper around an `i64`. Use a new `I64Visitor` to deserialize i64s, u64s, and strs into the wanted i64. --- src/model/utils.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/model/utils.rs') diff --git a/src/model/utils.rs b/src/model/utils.rs index f88ecf5..6363fb6 100644 --- a/src/model/utils.rs +++ b/src/model/utils.rs @@ -240,3 +240,34 @@ impl<'de> Visitor<'de> for U64Visitor { Ok(v) } } + +pub struct I64Visitor; + +impl<'de> Visitor<'de> for I64Visitor { + type Value = i64; + + fn expecting(&self, formatter: &mut Formatter) -> FmtResult { + formatter.write_str("identifier") + } + + fn visit_str(self, v: &str) -> StdResult { + match v.parse::() { + Ok(v) => Ok(v), + Err(_) => { + let mut s = String::new(); + s.push_str("Unknown i64 value: "); + s.push_str(v); + + Err(DeError::custom(s)) + }, + } + } + + fn visit_i64(self, v: i64) -> StdResult { + Ok(v) + } + + fn visit_u64(self, v: u64) -> StdResult { + Ok(v as i64) + } +} -- cgit v1.2.3