From 6f9c0d0f2906d05e27f9f11af6cefdf006c2cf4b Mon Sep 17 00:00:00 2001 From: Max Audron Date: Wed, 26 Feb 2025 17:53:08 +0100 Subject: refactor variant serialization code Factored out a lot of the serialization of variants into trait's that have auto impl so code duplication is much reduced --- src/primitive/msgid.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/primitive/msgid.rs') diff --git a/src/primitive/msgid.rs b/src/primitive/msgid.rs index 27608fe..7f3e32f 100644 --- a/src/primitive/msgid.rs +++ b/src/primitive/msgid.rs @@ -8,9 +8,16 @@ pub struct MsgId( use crate::error::ProtocolError; use crate::{deserialize::*, serialize::*}; +use crate::serialize::UserType; + impl Serialize for MsgId { fn serialize(&self) -> Result, ProtocolError> { - self.0.serialize() + let mut res = Vec::new(); + + res.append(&mut Self::NAME.serialize_utf8()?); + res.extend(self.0.serialize()?); + + Ok(res) } } @@ -49,6 +56,10 @@ impl std::ops::Deref for MsgId { } } +impl UserType for MsgId { + const NAME: &str = "MsgId"; +} + #[cfg(test)] mod tests { use super::*; @@ -69,9 +80,9 @@ mod tests { pub fn msgid_serialize_test() { let res = MsgId(1).serialize().unwrap(); let expected_bytes: &[u8] = if cfg!(feature = "long-message-id") { - &[0, 0, 0, 0, 0, 0, 0, 1] + &[0, 0, 0, 5, 77, 115, 103, 73, 100, 0, 0, 0, 0, 0, 0, 0, 1] } else { - &[0, 0, 0, 1] + &[0, 0, 0, 5, 77, 115, 103, 73, 100, 0, 0, 0, 1] }; assert_eq!(res, expected_bytes); } -- cgit v1.2.3