From b7c34f8bdbcb92beba58b07d52d1d1e0bd370113 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Wed, 1 Feb 2023 16:53:56 +0400 Subject: [PATCH] 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]) + } }