ToDump as trait, fine tuning de/ser
This commit is contained in:
parent
352fe36b14
commit
4cd7ea75ca
32
src/de.rs
32
src/de.rs
@ -11,7 +11,7 @@ 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)
|
||||||
}
|
}
|
||||||
@ -241,7 +241,7 @@ struct SimpleMap<'a, T: BipackSource> {
|
|||||||
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,23 +346,25 @@ 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();
|
let packed = to_bytes(x).unwrap();
|
||||||
println!("packed {:?}:\n{}", x, to_dump(&packed) );
|
println!("packed {:?}:\n{}", x, to_dump(&packed));
|
||||||
|
let z: T = from_bytes(&packed).unwrap();
|
||||||
assert_eq!(*x, from_bytes(&packed).unwrap());
|
assert_eq!(*x, from_bytes(&packed).unwrap());
|
||||||
|
|
||||||
let mut buffer = [0u8; 128];
|
|
||||||
let packed2 = to_buffer(x, & mut buffer).unwrap();
|
|
||||||
assert_eq!(*x, from_bytes(&packed2).unwrap());
|
|
||||||
|
|
||||||
if packed.len() > 1 {
|
if packed.len() > 1 {
|
||||||
let mut small_buffer = [0u8; 1];
|
let mut small_buffer = [0u8; 1];
|
||||||
let result = to_buffer(x, & mut small_buffer);
|
let result = to_buffer(x, &mut small_buffer);
|
||||||
assert_eq!(true, result.is_err());
|
assert_eq!(true, result.is_err());
|
||||||
assert_eq!(BipackError::BufferOverflow, result.err().unwrap());
|
assert_eq!(BipackError::BufferOverflow, result.err().unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut buffer = [0u8; 128];
|
||||||
|
let packed2 = to_buffer(x, &mut buffer).unwrap();
|
||||||
|
assert_eq!(*x, from_bytes(&packed2).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_enum() -> Result<()> {
|
fn test_enum() -> Result<()> {
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||||
@ -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..]);
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
19
src/tools.rs
19
src/tools.rs
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user