From b7c34f8bdbcb92beba58b07d52d1d1e0bd370113 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Wed, 1 Feb 2023 16:53:56 +0400 Subject: [PATCH 1/6] Fix & test rgb deserialization --- crates/teloxide-core/src/types.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/teloxide-core/src/types.rs b/crates/teloxide-core/src/types.rs index 420e8fee..fc7a47b2 100644 --- a/crates/teloxide-core/src/types.rs +++ b/crates/teloxide-core/src/types.rs @@ -461,6 +461,13 @@ pub(crate) mod serde_rgb { { Ok(from_u32(v)) } + + fn visit_u64(self, v: u64) -> Result + where + E: serde::de::Error, + { + self.visit_u32(v.try_into().map_err(|_| E::custom("rgb value doesn't fit u32"))?) + } } d.deserialize_u32(V) } @@ -481,5 +488,16 @@ pub(crate) mod serde_rgb { } #[test] - fn json() {} + fn json() { + #[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] + struct Struct { + #[serde(with = "self")] + color: [u8; 3], + } + + let json = format!("{}", 0x00AABBCC); + let Struct { color } = serde_json::from_str(&json).unwrap(); + + assert_eq!(color, [0xAA, 0xBB, 0xCC]) + } } From 6a2f5d7f2fde73f8c6e16790f9c75f9070273175 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Wed, 1 Feb 2023 16:57:10 +0400 Subject: [PATCH 2/6] Correctly deserialize `Message::thread_id` --- crates/teloxide-core/src/types/message.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/teloxide-core/src/types/message.rs b/crates/teloxide-core/src/types/message.rs index 6b8922cb..64afdea1 100644 --- a/crates/teloxide-core/src/types/message.rs +++ b/crates/teloxide-core/src/types/message.rs @@ -26,6 +26,7 @@ pub struct Message { /// Unique identifier of a message thread to which the message belongs; for /// supergroups only. // FIXME: MessageThreadId or such + #[serde(rename = "message_thread_id")] pub thread_id: Option, /// Date the message was sent in Unix time. From 0b83007954c49b5d206f32359f6d89e9f9337a23 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Wed, 1 Feb 2023 16:57:42 +0400 Subject: [PATCH 3/6] Add some deserialization tests for topic-related types/values --- crates/teloxide-core/src/types.rs | 2 +- .../src/types/forum_topic_created.rs | 17 ++++++++++ crates/teloxide-core/src/types/message.rs | 34 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/crates/teloxide-core/src/types.rs b/crates/teloxide-core/src/types.rs index fc7a47b2..8737244a 100644 --- a/crates/teloxide-core/src/types.rs +++ b/crates/teloxide-core/src/types.rs @@ -496,7 +496,7 @@ pub(crate) mod serde_rgb { } let json = format!("{}", 0x00AABBCC); - let Struct { color } = serde_json::from_str(&json).unwrap(); + let Struct { color } = serde_json::from_str(&json).unwrap(); assert_eq!(color, [0xAA, 0xBB, 0xCC]) } diff --git a/crates/teloxide-core/src/types/forum_topic_created.rs b/crates/teloxide-core/src/types/forum_topic_created.rs index cf4a91b6..fe209f62 100644 --- a/crates/teloxide-core/src/types/forum_topic_created.rs +++ b/crates/teloxide-core/src/types/forum_topic_created.rs @@ -19,3 +19,20 @@ pub struct ForumTopicCreated { // FIXME: CustomEmojiId pub icon_custom_emoji_id: Option, } + +#[cfg(test)] +mod tests { + use crate::types::ForumTopicCreated; + + #[test] + fn deserialization() { + let json = + r#"{"icon_color":9367192,"icon_custom_emoji_id":"5312536423851630001","name":"???"}"#; + + let event = serde_json::from_str::(json).unwrap(); + + assert_eq!(event.name, "???"); + assert_eq!(event.icon_color, [0x8E, 0xEE, 0x98]); + assert_eq!(event.icon_custom_emoji_id.as_deref(), Some("5312536423851630001")); + } +} diff --git a/crates/teloxide-core/src/types/message.rs b/crates/teloxide-core/src/types/message.rs index 64afdea1..0fcbc1a8 100644 --- a/crates/teloxide-core/src/types/message.rs +++ b/crates/teloxide-core/src/types/message.rs @@ -117,6 +117,8 @@ pub struct MessageCommon { pub reply_markup: Option, /// `true`, if the message is sent to a forum topic. + // FIXME: `is_topic_message` is included even in service messages, like ForumTopicCreated. + // more this to `Message` #[serde(default)] pub is_topic_message: bool, @@ -1838,4 +1840,36 @@ mod tests { assert!(!entities.is_empty()); assert_eq!(entities[0].kind().clone(), MessageEntityKind::Url); } + + #[test] + fn topic_created() { + let json = r#"{ + "chat":{"id":-1001847508954,"is_forum":true,"title":"twest","type":"supergroup"}, + "date":1675229139, + "forum_topic_created":{ + "icon_color":9367192, + "icon_custom_emoji_id":"5312536423851630001", + "name":"???" + }, + "from":{ + "first_name":"вафель'", + "id":1253681278, + "is_bot":false, + "language_code":"en", + "username":"wafflelapkin" + }, + "is_topic_message":true, + "message_id":4, + "message_thread_id":4 + }"#; + + let message: Message = serde_json::from_str(json).unwrap(); + } + + #[test] + fn topic_message() { + let json = r#"{"chat":{"id":-1001847508954,"is_forum":true,"title":"twest","type":"supergroup"},"date":1675229140,"from":{"first_name":"вафель'","id":1253681278,"is_bot":false,"language_code":"en","username":"wafflelapkin"},"is_topic_message":true,"message_id":5,"message_thread_id":4,"reply_to_message":{"chat":{"id":-1001847508954,"is_forum":true,"title":"twest","type":"supergroup"},"date":1675229139,"forum_topic_created":{"icon_color":9367192,"icon_custom_emoji_id":"5312536423851630001","name":"???"},"from":{"first_name":"вафель'","id":1253681278,"is_bot":false,"language_code":"en","username":"wafflelapkin"},"is_topic_message":true,"message_id":4,"message_thread_id":4},"text":"blah"}"#; + + let message: Message = serde_json::from_str(json).unwrap(); + } } From c3a3fc4ee2c4ad8d9d2da5043b6fd9aa6d3dceff Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Wed, 1 Feb 2023 17:17:30 +0400 Subject: [PATCH 4/6] Remove unused variables in tests --- crates/teloxide-core/src/types/message.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/teloxide-core/src/types/message.rs b/crates/teloxide-core/src/types/message.rs index 0fcbc1a8..706e371b 100644 --- a/crates/teloxide-core/src/types/message.rs +++ b/crates/teloxide-core/src/types/message.rs @@ -1863,13 +1863,13 @@ mod tests { "message_thread_id":4 }"#; - let message: Message = serde_json::from_str(json).unwrap(); + let _: Message = serde_json::from_str(json).unwrap(); } #[test] fn topic_message() { let json = r#"{"chat":{"id":-1001847508954,"is_forum":true,"title":"twest","type":"supergroup"},"date":1675229140,"from":{"first_name":"вафель'","id":1253681278,"is_bot":false,"language_code":"en","username":"wafflelapkin"},"is_topic_message":true,"message_id":5,"message_thread_id":4,"reply_to_message":{"chat":{"id":-1001847508954,"is_forum":true,"title":"twest","type":"supergroup"},"date":1675229139,"forum_topic_created":{"icon_color":9367192,"icon_custom_emoji_id":"5312536423851630001","name":"???"},"from":{"first_name":"вафель'","id":1253681278,"is_bot":false,"language_code":"en","username":"wafflelapkin"},"is_topic_message":true,"message_id":4,"message_thread_id":4},"text":"blah"}"#; - let message: Message = serde_json::from_str(json).unwrap(); + let _: Message = serde_json::from_str(json).unwrap(); } } From 3c5370548aaf293460bb03c35f04237524212f6a Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 2 Feb 2023 12:12:37 +0400 Subject: [PATCH 5/6] fix a test --- crates/teloxide-core/src/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/teloxide-core/src/types.rs b/crates/teloxide-core/src/types.rs index 8737244a..fd2a428c 100644 --- a/crates/teloxide-core/src/types.rs +++ b/crates/teloxide-core/src/types.rs @@ -495,7 +495,7 @@ pub(crate) mod serde_rgb { color: [u8; 3], } - let json = format!("{}", 0x00AABBCC); + let json = format!(r#"{{"color":{}}}"#, 0x00AABBCC); let Struct { color } = serde_json::from_str(&json).unwrap(); assert_eq!(color, [0xAA, 0xBB, 0xCC]) From 6a9183a53b288422af5882fc235f562c0ffb211a Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Wed, 15 Feb 2023 18:14:28 +0400 Subject: [PATCH 6/6] Update changelog --- crates/teloxide-core/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/teloxide-core/CHANGELOG.md b/crates/teloxide-core/CHANGELOG.md index dadb1ec3..d42b4067 100644 --- a/crates/teloxide-core/CHANGELOG.md +++ b/crates/teloxide-core/CHANGELOG.md @@ -11,10 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Update::user` now handles channel posts, chat member changes and chat join request updates correctly ([#835][pr835]) - In cases when `teloxide` can't deserialize an update, error now includes the full json value ([#826][pr826]) - +- Deserialization of topic messages ([#830][pr830]) [pr835]: https://github.com/teloxide/teloxide/pull/835 [pr826]: https://github.com/teloxide/teloxide/pull/826 +[pr830]: https://github.com/teloxide/teloxide/pull/830 ## 0.9.0 - 2023-01-17