added fixint support

This commit is contained in:
Sergey Chernov 2023-11-25 02:37:48 +03:00
parent 3a8b622ae7
commit a48de88630
7 changed files with 291 additions and 84 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "bipack_ru" name = "bipack_ru"
version = "0.3.0" version = "0.3.1"
edition = "2021" edition = "2021"
license = "Apache-2.0" license = "Apache-2.0"
description = "binary size-effective format used in Divan smart contracts, wasm bindings, network protocols, etc." description = "binary size-effective format used in Divan smart contracts, wasm bindings, network protocols, etc."

View File

@ -44,6 +44,10 @@ The autodoc documentation is good enough already, so we do not repeat it here no
- just ad this package to your dependencies, it is on crates.io. - just ad this package to your dependencies, it is on crates.io.
## Big thanks
- to https://github.com/jamesmunns for the brilliant postcard format which was used as a model.
# License # License
For compliance with other modules this work is provided under APACHE 2.0 license a copy of which is included in the file `LICENSE`. For compliance with other modules this work is provided under APACHE 2.0 license a copy of which is included in the file `LICENSE`.

View File

@ -18,6 +18,7 @@ pub fn create_contrail(data: &[u8]) -> Vec<u8> {
result result
} }
#[cfg(test)]
mod tests { mod tests {
use crate::contrail::{create_contrail, is_valid_contrail}; use crate::contrail::{create_contrail, is_valid_contrail};

View File

@ -260,7 +260,11 @@ impl<'de, 'a> MapAccess<'de> for SimpleMap<'a> {
} }
} }
#[cfg(test)]
mod tests { mod tests {
// use std::collections::{HashMap, HashSet};
// use std::fmt::Debug;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::fmt::Debug; use std::fmt::Debug;

189
src/fixint.rs Normal file
View File

@ -0,0 +1,189 @@
//! # Fixed Size Integers
//!
//! In some cases, the use of variably length encoded data may not be
//! preferrable. These modules, for use with `#[serde(with = ...)]`
//! "opt out" of variable length encoding.
//!
//! Support explicitly not provided for `usize` or `isize`, as
//! these types would not be portable between systems of different
//! pointer widths.
//!
//! Although all data in Postcard is typically encoded in little-endian
//! order, these modules provide a choice to the user to encode the data
//! in either little or big endian form, which may be useful for zero-copy
//! applications.
use serde::{Deserialize, Serialize, Serializer};
/// Use with the `#[serde(with = "bipack_ru::fixint::le")]` field attribute.
/// Disables smartint serialization/deserialization for the specified integer
/// field. The integer will always be serialized in the same way as a fixed
/// size array, in **Little Endian** order on the wire.
///
/// ```rust
/// # use serde::Serialize;
/// #[derive(Serialize)]
/// pub struct DefinitelyLittleEndian {
/// #[serde(with = "bipack_ru::fixint::le")]
/// x: u16,
/// }
/// ```
pub mod le {
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use super::LE;
/// Serialize the integer value as a little-endian fixed-size array.
pub fn serialize<S, T>(val: &T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: Copy,
LE<T>: Serialize,
{
LE(*val).serialize(serializer)
}
/// Deserialize the integer value from a little-endian fixed-size array.
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
LE<T>: Deserialize<'de>,
{
LE::<T>::deserialize(deserializer).map(|x| x.0)
}
}
/// Use with the `#[serde(with = "bipack_ru::fixint::be")]` field attribute.
/// Disables smartint serialization/deserialization for the specified integer
/// field. The integer will always be serialized in the same way as a fixed
/// size array, in **Big Endian** order on the wire.
///
/// ```rust
/// # use serde::Serialize;
/// #[derive(Serialize)]
/// pub struct DefinitelyBigEndian {
/// #[serde(with = "bipack_ru::fixint::be")]
/// x: u16,
/// }
/// ```
pub mod be {
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use super::BE;
/// Serialize the integer value as a big-endian fixed-size array.
pub fn serialize<S, T>(val: &T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: Copy,
BE<T>: Serialize,
{
BE(*val).serialize(serializer)
}
/// Deserialize the integer value from a big-endian fixed-size array.
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
BE<T>: Deserialize<'de>,
{
BE::<T>::deserialize(deserializer).map(|x| x.0)
}
}
#[doc(hidden)]
pub struct LE<T>(T);
#[doc(hidden)]
pub struct BE<T>(T);
macro_rules! impl_fixint {
($( $int:ty ),*) => {
$(
impl Serialize for LE<$int> {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.to_le_bytes().serialize(serializer)
}
}
impl<'de> Deserialize<'de> for LE<$int> {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
<_ as Deserialize>::deserialize(deserializer)
.map(<$int>::from_le_bytes)
.map(Self)
}
}
impl Serialize for BE<$int> {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.to_be_bytes().serialize(serializer)
}
}
impl<'de> Deserialize<'de> for BE<$int> {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
<_ as Deserialize>::deserialize(deserializer)
.map(<$int>::from_be_bytes)
.map(Self)
}
}
)*
};
}
impl_fixint![i16, i32, i64, i128, u16, u32, u64, u128];
#[cfg(test)]
mod tests {
use serde::{Deserialize, Serialize};
use crate::de::from_bytes;
use crate::ser::to_bytes;
#[test]
fn test_little_endian() {
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct DefinitelyLE {
#[serde(with = "crate::fixint::le")]
x: u16,
}
let input = DefinitelyLE { x: 0xABCD };
let serialized = to_bytes(&input).unwrap();
assert_eq!(serialized, &[0xCD, 0xAB]);
let deserialized: DefinitelyLE = from_bytes(&serialized).unwrap();
assert_eq!(deserialized, input);
}
#[test]
fn test_big_endian() {
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct DefinitelyBE {
#[serde(with = "crate::fixint::be")]
x: u16,
}
let input = DefinitelyBE { x: 0xABCD };
let serialized = to_bytes(&input).unwrap();
assert_eq!(serialized, &[0xAB, 0xCD]);
let deserialized: DefinitelyBE = from_bytes(&serialized).unwrap();
assert_eq!(deserialized, input);
}
}

View File

@ -118,11 +118,12 @@ pub mod bipack_source;
pub mod bipack_sink; pub mod bipack_sink;
pub mod tools; pub mod tools;
pub mod bipack; pub mod bipack;
mod error; pub mod error;
mod ser; pub mod ser;
mod de; pub mod de;
mod crc; pub mod crc;
mod contrail; pub mod contrail;
pub mod fixint;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@ -1,12 +1,7 @@
use std::collections::HashMap;
use std::string::FromUtf8Error;
use serde::{ser, Serialize}; use serde::{ser, Serialize};
use crate::bipack_sink::{BipackSink, IntoU64}; use crate::bipack_sink::{BipackSink, IntoU64};
use crate::bipack_source::{BipackError, BipackSource, SliceSource};
use crate::error::{Error, Result}; use crate::error::{Error, Result};
use crate::tools::{to_dump, to_hex};
pub struct Serializer { pub struct Serializer {
// This string starts empty and JSON is appended as values are serialized. // This string starts empty and JSON is appended as values are serialized.
@ -159,7 +154,8 @@ impl<'a> ser::Serializer for &'a mut Serializer {
} }
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> { fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
self.serialize_seq(Some(len)) Ok(self)
// self.serialize_seq(Some(len))
} }
fn serialize_tuple_struct( fn serialize_tuple_struct(
@ -342,81 +338,93 @@ impl<'a> ser::SerializeStructVariant for &'a mut Serializer {
} }
} }
#[test] #[cfg(test)]
fn test_struct() -> std::result::Result<(), BipackError> { mod test {
#[derive(Serialize)] use std::collections::HashMap;
struct Test { use std::string::FromUtf8Error;
int: u32,
seq: Vec<&'static str>, use serde::Serialize;
use crate::bipack_source::{BipackError, BipackSource, SliceSource};
use crate::error;
use crate::ser::to_bytes;
use crate::tools::{to_dump, to_hex};
#[test]
fn test_struct() -> std::result::Result<(), BipackError> {
#[derive(Serialize)]
struct Test {
int: u32,
seq: Vec<&'static str>,
}
let test = Test {
int: 17,
seq: vec!["a", "b"],
};
let x = to_bytes(&test).unwrap();
println!("!:\n{}", to_dump(&x));
// let y = x.clone();
// let z = x.clone();
let mut src = SliceSource::from(&x);
assert_eq!(test.int, src.get_unsigned()? as u32);
assert_eq!(test.seq.len(), src.get_unsigned()? as usize);
assert_eq!(test.seq[0], src.get_str()?);
assert_eq!(test.seq[1], src.get_str()?);
Ok(())
} }
let test = Test { #[test]
int: 17, fn test_enum() -> std::result::Result<(), FromUtf8Error> {
seq: vec!["a", "b"], #[derive(Serialize)]
}; enum E {
let x = to_bytes(&test).unwrap(); Unit,
println!("!:\n{}", to_dump(&x)); Unit2,
// let y = x.clone(); Newtype(u32),
// let z = x.clone(); Tuple(u32, u32),
let mut src = SliceSource::from(&x); Struct { a: u32 },
assert_eq!(test.int, src.get_unsigned()? as u32); }
assert_eq!(test.seq.len(), src.get_unsigned()? as usize);
assert_eq!(test.seq[0], src.get_str()?);
assert_eq!(test.seq[1], src.get_str()?);
Ok(())
}
#[test] let u = E::Unit;
fn test_enum() -> std::result::Result<(), FromUtf8Error> { println!("u:{}", to_dump(to_bytes(&u).unwrap().as_slice()));
#[derive(Serialize)] assert_eq!("00", to_hex(to_bytes(&u).unwrap())?);
enum E { let u2 = E::Unit2;
Unit, println!("u:{}", to_dump(to_bytes(&u2).unwrap().as_slice()));
Unit2, let nt = E::Newtype(17);
Newtype(u32), println!("u:{}", to_dump(to_bytes(&nt).unwrap().as_slice()));
Tuple(u32, u32), assert_eq!("08 44", to_hex(to_bytes(&nt).unwrap())?);
Struct { a: u32 }, 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);
//
// let n = E::Newtype(1);
// let expected = r#"{"Newtype":1}"#;
// assert_eq!(to_string(&n).unwrap(), expected);
//
// let t = E::Tuple(1, 2);
// let expected = r#"{"Tuple":[1,2]}"#;
// assert_eq!(to_string(&t).unwrap(), expected);
//
// let s = E::Struct { a: 1 };
// let expected = r#"{"Struct":{"a":1}}"#;
// assert_eq!(to_string(&s).unwrap(), expected);
Ok(())
} }
let u = E::Unit; #[test]
println!("u:{}",to_dump(to_bytes(&u).unwrap().as_slice())); fn test_map() -> error::Result<()> {
assert_eq!("00",to_hex(to_bytes(&u).unwrap())?); let mut src = HashMap::new();
let u2 = E::Unit2; src.insert("foo", 1);
println!("u:{}",to_dump(to_bytes(&u2).unwrap().as_slice())); src.insert("foo", 42);
let nt = E::Newtype(17); src.insert("bar", 1);
println!("u:{}",to_dump(to_bytes(&nt).unwrap().as_slice())); src.insert("baz", 17);
assert_eq!("08 44",to_hex(to_bytes(&nt).unwrap())?); let packed = to_bytes(&src)?;
let t = E::Tuple(7,17); println!("{}", to_dump(&packed));
println!("u:{}",to_dump(to_bytes(&t).unwrap().as_slice())); Ok(())
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);
//
// let n = E::Newtype(1);
// let expected = r#"{"Newtype":1}"#;
// assert_eq!(to_string(&n).unwrap(), expected);
//
// let t = E::Tuple(1, 2);
// let expected = r#"{"Tuple":[1,2]}"#;
// assert_eq!(to_string(&t).unwrap(), expected);
//
// let s = E::Struct { a: 1 };
// let expected = r#"{"Struct":{"a":1}}"#;
// assert_eq!(to_string(&s).unwrap(), expected);
Ok(())
}
#[test]
fn test_map() -> Result<()> {
let mut src = HashMap::new();
src.insert("foo", 1);
src.insert("foo", 42);
src.insert("bar", 1);
src.insert("baz", 17);
let packed = to_bytes(&src)?;
println!("{}", to_dump(&packed));
Ok(())
} }