From 83884f7be8f3e5c6149d40c443603322793fa017 Mon Sep 17 00:00:00 2001 From: Max Audron Date: Wed, 26 Feb 2025 19:03:33 +0100 Subject: refactor variant deserialization --- src/serialize.rs | 52 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) (limited to 'src/serialize.rs') diff --git a/src/serialize.rs b/src/serialize.rs index 553a239..925b668 100644 --- a/src/serialize.rs +++ b/src/serialize.rs @@ -1,14 +1,7 @@ -use crate::{error::ProtocolError, primitive}; - -/// Serialization of types and structs to the quassel byteprotocol -pub trait Serialize { - fn serialize(&self) -> Result, ProtocolError>; -} - -/// Serialization of UTF-8 based Strings to the quassel byteprotocol -pub trait SerializeUTF8 { - fn serialize_utf8(&self) -> Result, ProtocolError>; -} +use crate::{ + error::ProtocolError, + primitive::{self, Variant}, +}; /// Sets the usertype name to be used in serialization and deserialization of the types variants. /// Automaticly implements the [SerializeVariant] trait. @@ -37,6 +30,24 @@ pub trait UserType { const NAME: &str; } +impl VariantType for T { + const TYPE: u32 = primitive::USERTYPE; +} + +// ============================================= +// Serialization +// + +/// Serialization of types and structs to the quassel byteprotocol +pub trait Serialize { + fn serialize(&self) -> Result, ProtocolError>; +} + +/// Serialization of UTF-8 based Strings to the quassel byteprotocol +pub trait SerializeUTF8 { + fn serialize_utf8(&self) -> Result, ProtocolError>; +} + /// Provides a easy default implementation for serializing a type to it's [Variant] given it's QT type id. /// /// If the type is a UserType implement only [UserType], SerializeVariant is implemented automaticly. @@ -73,10 +84,6 @@ pub trait SerializeVariant: VariantType + Serialize { impl SerializeVariant for T {} -impl VariantType for T { - const TYPE: u32 = primitive::USERTYPE; -} - // ============================================= // Deserialization // @@ -95,8 +102,15 @@ pub trait DeserializeUTF8 { Self: std::marker::Sized; } -pub trait DeserializeVariant: VariantType { - fn parse_variant(b: &[u8]) -> Result<(usize, Self), ProtocolError> - where - Self: std::marker::Sized; +pub trait DeserializeVariant: VariantType + Deserialize +where + Variant: From, + Self: Sized, +{ + fn parse_variant(b: &[u8], len: usize) -> Result<(usize, Variant), ProtocolError> { + let (vlen, value) = Self::parse(&b[len..])?; + return Ok((len + vlen, value.into())); + } } + +impl DeserializeVariant for T where Variant: From {} -- cgit v1.2.3