ToDump as trait, fine tuning de/ser

This commit is contained in:
Sergey Chernov 2023-12-02 23:47:41 +03:00
parent 352fe36b14
commit 4cd7ea75ca
3 changed files with 45 additions and 22 deletions

View File

@ -11,12 +11,12 @@ pub struct Deserializer<T: BipackSource> {
input: T, input: T,
} }
pub fn from_bytes<'de, T: Deserialize<'de>>(source: &[u8]) -> Result<T> { pub fn from_bytes<'b, T: Deserialize<'b>>(source: &[u8]) -> Result<T> {
let mut des = Deserializer { input: SliceSource::from(&source) }; let mut des = Deserializer { input: SliceSource::from(&source) };
T::deserialize(&mut des) T::deserialize(&mut des)
} }
impl<'de, 'a, T: BipackSource> de::Deserializer<'de> for &'a mut Deserializer<T> { impl<'de, 'a, T: BipackSource> de::Deserializer<'de> for &'a mut Deserializer<T> {
type Error = BipackError; type Error = BipackError;
fn deserialize_any<V>(self, visitor: V) -> std::result::Result<V::Value, Self::Error> where V: Visitor<'de> { fn deserialize_any<V>(self, visitor: V) -> std::result::Result<V::Value, Self::Error> where V: Visitor<'de> {
@ -204,7 +204,7 @@ impl<'de, 'a, T: BipackSource> serde::de::VariantAccess<'de> for &'a mut Deseria
} }
struct SimpleSeq<'a, T: BipackSource> { struct SimpleSeq<'a, T: BipackSource> {
de: &'a mut Deserializer<T>, de: &'a mut Deserializer<T>,
size: usize, size: usize,
} }
@ -236,12 +236,12 @@ impl<'de, 'a, T: BipackSource> SeqAccess<'de> for SimpleSeq<'a, T> {
} }
struct SimpleMap<'a, T: BipackSource> { struct SimpleMap<'a, T: BipackSource> {
de: &'a mut Deserializer<T>, de: &'a mut Deserializer<T>,
size: usize, size: usize,
} }
impl<'de, 'a, T: BipackSource> MapAccess<'de> for SimpleMap<'a,T> { impl<'de, 'a, T: BipackSource> MapAccess<'de> for SimpleMap<'a, T> {
type Error = BipackError; type Error = BipackError;
fn next_key_seed<K>(&mut self, seed: K) -> std::result::Result<Option<K::Value>, Self::Error> where K: DeserializeSeed<'de> { fn next_key_seed<K>(&mut self, seed: K) -> std::result::Result<Option<K::Value>, Self::Error> where K: DeserializeSeed<'de> {
@ -346,21 +346,23 @@ mod tests {
} }
fn testeq<'a, T: Serialize + Deserialize<'a> + PartialEq + Debug>(x: & 'a T) { fn testeq<T: Serialize + Deserialize<'static> + PartialEq + Debug>(x: &T) {
let packed = to_bytes(x).unwrap(); {
println!("packed {:?}:\n{}", x, to_dump(&packed) ); let packed = to_bytes(x).unwrap();
assert_eq!(*x, from_bytes(&packed).unwrap()); println!("packed {:?}:\n{}", x, to_dump(&packed));
let z: T = from_bytes(&packed).unwrap();
assert_eq!(*x, from_bytes(&packed).unwrap());
if packed.len() > 1 {
let mut small_buffer = [0u8; 1];
let result = to_buffer(x, &mut small_buffer);
assert_eq!(true, result.is_err());
assert_eq!(BipackError::BufferOverflow, result.err().unwrap());
}
}
let mut buffer = [0u8; 128]; let mut buffer = [0u8; 128];
let packed2 = to_buffer(x, & mut buffer).unwrap(); let packed2 = to_buffer(x, &mut buffer).unwrap();
assert_eq!(*x, from_bytes(&packed2).unwrap()); assert_eq!(*x, from_bytes(&packed2).unwrap());
if packed.len() > 1 {
let mut small_buffer = [0u8; 1];
let result = to_buffer(x, & mut small_buffer);
assert_eq!(true, result.is_err());
assert_eq!(BipackError::BufferOverflow, result.err().unwrap());
}
} }
#[test] #[test]
@ -383,24 +385,24 @@ mod tests {
testeq(&E::Unit2); testeq(&E::Unit2);
testeq(&E::Newtype(101)); testeq(&E::Newtype(101));
testeq(&E::Tuple(17, 42)); testeq(&E::Tuple(17, 42));
testeq(&E::Struct {a: 19} ); testeq(&E::Struct { a: 19 });
Ok(()) Ok(())
} }
#[test] #[test]
fn test_arrays() { fn test_arrays() {
let x = (1,2,3,4,5); let x = (1, 2, 3, 4, 5);
// println!("{:?}",x); // println!("{:?}",x);
// println!("{}", to_dump(to_bytes(&x).unwrap().as_slice())); // println!("{}", to_dump(to_bytes(&x).unwrap().as_slice()));
assert_eq!(5,to_bytes(&x).unwrap().len()); assert_eq!(5, to_bytes(&x).unwrap().len());
testeq(&x); testeq(&x);
let y: [u8; 5] = [1,2,3,4,5]; let y: [u8; 5] = [1, 2, 3, 4, 5];
println!("{}", to_dump(to_bytes(&y).unwrap().as_slice())); println!("{}", to_dump(to_bytes(&y).unwrap().as_slice()));
assert_eq!(5, to_bytes(&y).unwrap().len()); assert_eq!(5, to_bytes(&y).unwrap().len());
assert_eq!(y, to_bytes(&y).unwrap()[..]); assert_eq!(y, to_bytes(&y).unwrap()[..]);
testeq(&y); testeq(&y);
let z = vec![1,2,3,4,5]; let z = vec![1, 2, 3, 4, 5];
assert_eq!(6, to_bytes(&z).unwrap().len()); assert_eq!(6, to_bytes(&z).unwrap().len());
assert_eq!(z, to_bytes(&z).unwrap()[1..]); assert_eq!(z, to_bytes(&z).unwrap()[1..]);
} }

View File

@ -126,6 +126,8 @@ pub mod contrail;
pub mod fixint; pub mod fixint;
pub mod buffer_sink; pub mod buffer_sink;
pub use serde::{Deserialize,Serialize};
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use base64::Engine; use base64::Engine;

View File

@ -93,3 +93,22 @@ pub fn to_hex<T: AsRef<[u8]>>(source: T) -> Result<String,FromUtf8Error> {
} }
result.string() result.string()
} }
pub trait ToDump {
/// Same as [to_dump] function, but specified as a trait for
/// different dump-able formats.
fn to_dump(&self) -> String;
}
impl ToDump for &[u8] {
fn to_dump(&self) -> String{
to_dump(self)
}
}
impl ToDump for Vec<u8> {
fn to_dump(&self) -> String {
to_dump(&self)
}
}