diff options
| author | Max Audron <audron@cocaine.farm> | 2022-10-06 13:25:55 +0200 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2022-10-06 13:27:16 +0200 |
| commit | 45a9f5c3965817536a8c14767de38a39a61ef498 (patch) | |
| tree | ac36bd9655b996f5a2e01d29fe2ee46a7f9ba56e /src/primitive/string.rs | |
| parent | fix IgnoreListManager (diff) | |
add variant impl for char
Diffstat (limited to 'src/primitive/string.rs')
| -rw-r--r-- | src/primitive/string.rs | 21 |
1 files changed, 19 insertions, 2 deletions
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<Vec<u8>, 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. /// |
