From 45a9f5c3965817536a8c14767de38a39a61ef498 Mon Sep 17 00:00:00 2001 From: Max Audron Date: Thu, 6 Oct 2022 13:25:55 +0200 Subject: add variant impl for char --- src/primitive/string.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src/primitive/string.rs') diff --git a/src/primitive/string.rs b/src/primitive/string.rs index 478bc0a..5addff2 100644 --- a/src/primitive/string.rs +++ b/src/primitive/string.rs @@ -7,8 +7,25 @@ use failure::Error; use log::trace; -use crate::util; -use crate::{deserialize::*, serialize::*}; +use crate::{deserialize::*, error::ProtocolError, serialize::*, util}; + +impl Deserialize for char { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (slen, qchar): (usize, u16) = u16::parse(&b[0..2])?; + let qchar = char::from_u32(qchar as u32).ok_or(ProtocolError::CharError)?; + + return Ok((slen, qchar)); + } +} + +impl Serialize for char { + fn serialize(&self) -> Result, Error> { + let mut b = [0, 0]; + self.encode_utf16(&mut b); + + return Ok(b[0].to_be_bytes().to_vec()); + } +} /// We Shadow the String type here as we can only use impl on types in our own scope. /// -- cgit v1.2.3