From 4cd7ea75ca8104e5762777dfab44e6a360d2654b Mon Sep 17 00:00:00 2001 From: sergeych Date: Sat, 2 Dec 2023 23:47:41 +0300 Subject: [PATCH] ToDump as trait, fine tuning de/ser --- src/de.rs | 46 ++++++++++++++++++++++++---------------------- src/lib.rs | 2 ++ src/tools.rs | 19 +++++++++++++++++++ 3 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/de.rs b/src/de.rs index 5861962..c0ee031 100644 --- a/src/de.rs +++ b/src/de.rs @@ -11,12 +11,12 @@ pub struct Deserializer { input: T, } -pub fn from_bytes<'de, T: Deserialize<'de>>(source: &[u8]) -> Result { +pub fn from_bytes<'b, T: Deserialize<'b>>(source: &[u8]) -> Result { let mut des = Deserializer { input: SliceSource::from(&source) }; T::deserialize(&mut des) } -impl<'de, 'a, T: BipackSource> de::Deserializer<'de> for &'a mut Deserializer { +impl<'de, 'a, T: BipackSource> de::Deserializer<'de> for &'a mut Deserializer { type Error = BipackError; fn deserialize_any(self, visitor: V) -> std::result::Result 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, 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, 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; fn next_key_seed(&mut self, seed: K) -> std::result::Result, Self::Error> where K: DeserializeSeed<'de> { @@ -346,21 +346,23 @@ mod tests { } - 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()); + fn testeq + PartialEq + Debug>(x: &T) { + { + let packed = to_bytes(x).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 packed2 = to_buffer(x, & mut buffer).unwrap(); + let packed2 = to_buffer(x, &mut buffer).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] @@ -383,24 +385,24 @@ mod tests { testeq(&E::Unit2); testeq(&E::Newtype(101)); testeq(&E::Tuple(17, 42)); - testeq(&E::Struct {a: 19} ); + testeq(&E::Struct { a: 19 }); Ok(()) } #[test] fn test_arrays() { - let x = (1,2,3,4,5); + let x = (1, 2, 3, 4, 5); // println!("{:?}",x); // 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); - 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())); assert_eq!(5, to_bytes(&y).unwrap().len()); assert_eq!(y, to_bytes(&y).unwrap()[..]); 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!(z, to_bytes(&z).unwrap()[1..]); } diff --git a/src/lib.rs b/src/lib.rs index 72981f0..9af98a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -126,6 +126,8 @@ pub mod contrail; pub mod fixint; pub mod buffer_sink; +pub use serde::{Deserialize,Serialize}; + #[cfg(test)] mod tests { use base64::Engine; diff --git a/src/tools.rs b/src/tools.rs index d51ce1d..57dea4e 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -93,3 +93,22 @@ pub fn to_hex>(source: T) -> Result { } 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 { + fn to_dump(&self) -> String { + to_dump(&self) + } +} +