From 1bed4eb5a3727e84bac4d64995df212a971f4566 Mon Sep 17 00:00:00 2001 From: Max Audron Date: Wed, 26 Feb 2025 18:23:55 +0100 Subject: refactor deserialize and serializevariant trait move stuff around a bit to sepperate it out for deserializevariant --- src/serialize.rs | 50 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 18 deletions(-) (limited to 'src/serialize.rs') diff --git a/src/serialize.rs b/src/serialize.rs index 52fc2fe..553a239 100644 --- a/src/serialize.rs +++ b/src/serialize.rs @@ -46,36 +46,24 @@ pub trait UserType { /// const TYPE: u32 = primitive::QVARIANTLIST; /// } /// ``` -pub trait SerializeVariant { +pub trait VariantType { /// QT Type as defined by the [Primitive Constants] /// /// [Primitive Constants]: primitive#constants const TYPE: u32; +} +pub trait SerializeVariant: VariantType + Serialize { /// Default implementation that passes the serialization through to [SerializeVariantInner]. /// [SerializeVariantInner] is automaticly implemented for all types that implement [Serialize] /// /// ```rust ignore /// self.serialize_variant_inner(Self::TYPE) /// ``` - fn serialize_variant(&self) -> Result, ProtocolError> - where - Self: SerializeVariantInner, - { - self.serialize_variant_inner(Self::TYPE) - } -} - -/// Implemented automaticly for all types that implement [Serialize] refer to [SerializeVariant] -pub trait SerializeVariantInner { - fn serialize_variant_inner(&self, t: u32) -> Result, ProtocolError>; -} - -impl SerializeVariantInner for T { - fn serialize_variant_inner(&self, t: u32) -> Result, ProtocolError> { + fn serialize_variant(&self) -> Result, ProtocolError> { let mut res: Vec = Vec::new(); - res.extend(t.to_be_bytes().iter()); + res.extend(Self::TYPE.to_be_bytes().iter()); res.extend(0x00u8.to_be_bytes().iter()); res.extend(self.serialize()?); @@ -83,6 +71,32 @@ impl SerializeVariantInner for T { } } -impl SerializeVariant for T { +impl SerializeVariant for T {} + +impl VariantType for T { const TYPE: u32 = primitive::USERTYPE; } + +// ============================================= +// Deserialization +// + +/// Deserialization of types and structs to the quassel byteprotocol +pub trait Deserialize { + fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> + where + Self: std::marker::Sized; +} + +/// Deserialization of UTF-8 based Strings to the quassel byteprotocol +pub trait DeserializeUTF8 { + fn parse_utf8(b: &[u8]) -> Result<(usize, Self), ProtocolError> + where + Self: std::marker::Sized; +} + +pub trait DeserializeVariant: VariantType { + fn parse_variant(b: &[u8]) -> Result<(usize, Self), ProtocolError> + where + Self: std::marker::Sized; +} -- cgit v1.2.3