ser/de support including enums

This commit is contained in:
Sergey Chernov 2023-11-24 21:59:18 +03:00
parent 53e3c568a8
commit 3a8b622ae7
2 changed files with 129 additions and 43 deletions

134
src/de.rs
View File

@ -1,7 +1,5 @@
use serde::de::{
self, DeserializeSeed, MapAccess, SeqAccess,
Visitor,
};
use serde::de::{self, DeserializeSeed, IntoDeserializer, MapAccess, SeqAccess, Visitor};
use serde::de::value::U32Deserializer;
use serde::Deserialize;
use crate::bipack_source::{BipackSource, VecSource};
@ -13,11 +11,13 @@ pub struct Deserializer {
input: VecSource,
}
pub fn from_bytes<'de,T: Deserialize<'de>>(source: Vec<u8>) -> Result<T> {
let mut des = Deserializer { input: VecSource::from(source)};
pub fn from_bytes<'de, T: Deserialize<'de>>(source: &[u8]) -> Result<T> {
let mut des = Deserializer { input: VecSource::from(source.to_vec()) };
T::deserialize(&mut des)
}
impl Deserializer {}
impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer {
type Error = Error;
@ -73,8 +73,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer {
let ch = self.input.get_str()?;
if ch.len() != 1 {
Err(Error::BadFormat(format!("Char length is {}, should be 1 {:?}", ch.len(), ch)))
}
else {
} else {
visitor.visit_char(ch.chars().next().unwrap())
}
}
@ -138,8 +137,17 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer {
visitor.visit_seq(SimpleSeq::new(self, fields.len()))
}
fn deserialize_enum<V>(self, name: &'static str, variants: &'static [&'static str], visitor: V) -> std::result::Result<V::Value, Self::Error> where V: Visitor<'de> {
todo!()
#[inline]
fn deserialize_enum<V>(
self,
_name: &'static str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
where
V: Visitor<'de>,
{
visitor.visit_enum(self)
}
fn deserialize_identifier<V>(self, visitor: V) -> std::result::Result<V::Value, Self::Error> where V: Visitor<'de> {
@ -155,6 +163,49 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer {
}
}
impl<'de, 'a> serde::de::EnumAccess<'de> for &'a mut Deserializer {
type Error = Error;
type Variant = Self;
#[inline]
fn variant_seed<V: DeserializeSeed<'de>>(self, seed: V) -> Result<(V::Value, Self)> {
let varint: u32 = self.input.get_unsigned()? as u32;
let v = DeserializeSeed::deserialize::<U32Deserializer<Error>>(
seed,
varint.into_deserializer())?;
Ok((v, self))
}
}
impl<'de, 'a> serde::de::VariantAccess<'de> for &'a mut Deserializer {
type Error = Error;
#[inline]
fn unit_variant(self) -> Result<()> {
Ok(())
}
#[inline]
fn newtype_variant_seed<V: DeserializeSeed<'de>>(self, seed: V) -> Result<V::Value> {
DeserializeSeed::deserialize(seed, self)
}
#[inline]
fn tuple_variant<V: Visitor<'de>>(self, len: usize, visitor: V) -> Result<V::Value> {
serde::de::Deserializer::deserialize_tuple(self, len, visitor)
}
#[inline]
fn struct_variant<V: Visitor<'de>>(
self,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value> {
serde::de::Deserializer::deserialize_tuple(self, fields.len(), visitor)
}
}
struct SimpleSeq<'a> {
de: &'a mut Deserializer,
size: usize,
@ -198,8 +249,7 @@ impl<'de, 'a> MapAccess<'de> for SimpleMap<'a> {
fn next_key_seed<K>(&mut self, seed: K) -> std::result::Result<Option<K::Value>, Self::Error> where K: DeserializeSeed<'de> {
if self.size < 1 {
Ok(None)
}
else {
} else {
self.size -= 1;
seed.deserialize(&mut *self.de).map(Some)
}
@ -210,6 +260,16 @@ impl<'de, 'a> MapAccess<'de> for SimpleMap<'a> {
}
}
mod tests {
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use serde::{Deserialize, Serialize};
use crate::de::from_bytes;
use crate::error::Result;
use crate::ser::to_bytes;
use crate::tools::to_dump;
#[test]
fn test_ints() -> Result<()> {
@ -220,7 +280,7 @@ fn test_ints() -> Result<()> {
// }
let b = vec![7];
assert_eq!( 7u8, from_bytes(vec![7u8])?);
assert_eq!(7u8, from_bytes(&vec![7u8])?);
// let j = r#"{"int":1,"seq":["a","b"]}"#;
@ -232,16 +292,6 @@ fn test_ints() -> Result<()> {
Ok(())
}
mod tests {
use std::collections::{HashMap, HashSet};
use serde::{Deserialize, Serialize};
use crate::de::from_bytes;
use crate::error::Result;
use crate::ser::to_bytes;
use crate::tools::to_dump;
#[test]
fn test_struct() -> Result<()> {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
@ -256,7 +306,7 @@ mod tests {
let packed = to_bytes(&expected)?;
println!("::{}", to_dump(&packed));
let unpacked: Test = from_bytes(packed)?;
let unpacked: Test = from_bytes(&packed)?;
println!("::{:?}", unpacked);
assert_eq!(&expected, &unpacked);
Ok(())
@ -275,7 +325,7 @@ mod tests {
let packed = to_bytes(&src)?;
println!("{}", to_dump(&packed));
let restored: HashMap<String, i32> = from_bytes(packed)?;
let restored: HashMap<String, i32> = from_bytes(&packed)?;
println!("{:?}", restored);
assert_eq!(src, restored);
@ -288,10 +338,42 @@ mod tests {
let packed = to_bytes(&src)?;
println!("{}", to_dump(&packed));
let restored: HashSet<String> = from_bytes(packed)?;
let restored: HashSet<String> = from_bytes(&packed)?;
println!("{:?}", restored);
assert_eq!(src, restored);
Ok(())
}
fn testeq<'a, T: Serialize + Deserialize<'a> + PartialEq + Debug>(x: & 'a T) {
let packed = to_bytes(x).unwrap();
println!("packed {:?}:\n{}", x, to_dump(&packed) );
assert_eq!(*x, from_bytes(&packed).unwrap());
}
#[test]
fn test_enum() -> Result<()> {
#[derive(Serialize, Deserialize, Debug, PartialEq)]
enum E {
Unit,
Unit2,
Newtype(u32),
Tuple(u32, u32),
Struct { a: u32 },
}
let packed = to_bytes(&E::Newtype(7))?;
println!("{}", to_dump(&packed));
let r: E = from_bytes(&packed)?;
println!("{:?}", r);
testeq(&E::Unit);
testeq(&E::Unit2);
testeq(&E::Newtype(101));
testeq(&E::Tuple(17, 42));
testeq(&E::Struct {a: 19} );
Ok(())
}
}

View File

@ -134,6 +134,7 @@ impl<'a> ser::Serializer for &'a mut Serializer {
where
T: ?Sized + Serialize,
{
value.serialize(self)
}
@ -200,7 +201,7 @@ impl<'a> ser::Serializer for &'a mut Serializer {
variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant> {
variant.serialize(&mut *self)?;
self.output.put_unsigned(_variant_index);
Ok(self)
}
}
@ -387,6 +388,9 @@ fn test_enum() -> std::result::Result<(), FromUtf8Error> {
let t = E::Tuple(7,17);
println!("u:{}",to_dump(to_bytes(&t).unwrap().as_slice()));
assert_eq!("0c 1c 44",to_hex(to_bytes(&t).unwrap())?);
let t = E::Struct { a: 17 };
println!("u:{}",to_dump(to_bytes(&t).unwrap().as_slice()));
assert_eq!("10 44",to_hex(to_bytes(&t).unwrap())?);
// let expected = r#""Unit""#;
// assert_eq!(to_string(&u).unwrap(), expected);
//