From c832009eae235881815186f740b716e0b7e63951 Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Fri, 23 Jun 2017 11:15:46 -0700 Subject: [PATCH] Make Message::nonce a serde_json::Value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nonces can actually be almost anything - including booleans - so just use a Value to represent it since very few users will need it. This fixes errors like: ``` WARN:serenity::internal::ws_impl: (╯°□°)╯︵ ┻━┻ Error decoding: {"t":"MESSAGE_CREATE","s":12187872,"op":0,"d":{"type":0,"tts":false,"timestamp":"2017-06-01T01:00:00.000000+00:00","pinned":false,"nonce":"","mentions":[{"username":"redacted","id":"redacted","discriminator":"redacted","avatar":"redacted"}],"mention_roles":[],"mention_everyone":false,"id":"redacted","embeds":[],"edited_timestamp":null,"content":"redacted","channel_id":"redacted","author":{"username":"redacted","id":"redacted","discriminator":"redacted","bot":true,"avatar":"redacted"},"attachments":[]}} ERROR:serenity::client: Shard handler received err: Json(ErrorImpl { code: Message("Unknown i64 value: "), line: 0, column: 0 }) ``` and: ``` WARN:serenity::internal::ws_impl: (╯°□°)╯︵ ┻━┻ Error decoding: {"t":"MESSAGE_CREATE","s":1001192,"op":0,"d":{"type":0,"tts":false,"timestamp":"2017-06-01T01:01:01.000000+00:00","pinned":false,"nonce":true,"mentions":[],"mention_roles":[],"mention_everyone":false,"id":"redacted","embeds":[],"edited_timestamp":null,"content":"bork","channel_id":"redacted","author":{"username":"redacted","id":"redacted","discriminator":"redacted","bot":true,"avatar":"redacted"},"attachments":[]}} ERROR:serenity::client: Shard handler received err: Json(ErrorImpl { code: Message("invalid type: boolean `true`, expected identifier"), line: 0, column: 0 }) ``` --- src/model/channel/message.rs | 4 ++- src/model/mod.rs | 49 ------------------------------------ src/model/utils.rs | 31 ----------------------- 3 files changed, 3 insertions(+), 81 deletions(-) diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index 15200fe68b2..89973d0764e 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -1,4 +1,5 @@ use chrono::{DateTime, FixedOffset}; +use serde_json::Value; use ::model::*; #[cfg(feature="cache")] @@ -48,7 +49,8 @@ pub struct Message { /// Array of users mentioned in the message. pub mentions: Vec, /// Non-repeating number used for ensuring message order. - pub nonce: Option, + #[serde(default)] + pub nonce: Value, /// Indicator of whether the message is pinned. pub pinned: bool, /// Array of reactions performed on the message. diff --git a/src/model/mod.rs b/src/model/mod.rs index ace59693037..21348b90f03 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -53,50 +53,6 @@ use ::utils::Colour; fn default_true() -> bool { true } -macro_rules! id_i64 { - ($(#[$attr:meta] $name:ident;)*) => { - $( - #[$attr] - #[derive(Copy, Clone, Debug, Eq, Hash, PartialOrd, Ord, Serialize)] - #[allow(derive_hash_xor_eq)] - pub struct $name(pub i64); - - impl $name { - /// Retrieves the time that the Id was created at. - pub fn created_at(&self) -> NaiveDateTime { - let offset = (self.0 >> 22) / 1000; - - NaiveDateTime::from_timestamp(1420070400 + offset, 0) - } - } - - impl From for $name { - fn from(v: i64) -> $name { - $name(v) - } - } - - impl PartialEq for $name { - fn eq(&self, other: &Self) -> bool { - self.0 == other.0 - } - } - - impl PartialEq for $name { - fn eq(&self, u: &i64) -> bool { - self.0 == *u - } - } - - impl<'de> Deserialize<'de> for $name { - fn deserialize>(deserializer: D) -> StdResult { - deserializer.deserialize_i64(I64Visitor).map($name) - } - } - )* - }; -} - macro_rules! id_u64 { ($(#[$attr:meta] $name:ident;)*) => { $( @@ -160,11 +116,6 @@ id_u64! { WebhookId; } -id_i64! { - /// An identifier for a general-purpose signed snowflake. - Snowflake; -} - /// A container for guilds. /// /// This is used to differentiate whether a guild itself can be used or whether diff --git a/src/model/utils.rs b/src/model/utils.rs index 6363fb61c9f..f88ecf5a84c 100644 --- a/src/model/utils.rs +++ b/src/model/utils.rs @@ -240,34 +240,3 @@ 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) - } -}