cleanups and typos
This commit is contained in:
parent
94c91e788e
commit
1836b3ebb9
12
README.md
12
README.md
@ -1,8 +1,8 @@
|
|||||||
# bipack_ru: Rust Bipack implementation
|
# bipack_ru: Rust Bipack implementation
|
||||||
|
|
||||||
Bipack serialization format is a compact binary format that was created as areplacement for a
|
Bipack serialization format is a compact binary format that was created as a replacement for a
|
||||||
BOSS format, mode compact, faster, and not revealing inner data structure as much as possible.
|
BOSS format, more compact, faster, and not revealing inner data structure as much as possible.
|
||||||
It is used in our next generation data protection systems, in DiWAN products as a base format and also in my newe communication package like [kiloparsec](https://gitea.sergeych.net/sergeych/kiloparsec)
|
It is used in our next generation data protection systems, in DiWAN products as a base format and also in my new communication package like [kiloparsec](https://gitea.sergeych.net/sergeych/kiloparsec)
|
||||||
(and in underlying [crypto2](https://gitea.sergeych.net/sergeych/crypto2)) which are intended
|
(and in underlying [crypto2](https://gitea.sergeych.net/sergeych/crypto2)) which are intended
|
||||||
to replace older `Parsec` protocols family to cope with modern crazy world challenges.
|
to replace older `Parsec` protocols family to cope with modern crazy world challenges.
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ fn test() -> Result<(),Error> {
|
|||||||
|
|
||||||
|
|
||||||
serde module supports all Rust formats except floats/doubles which are not yet used and
|
serde module supports all Rust formats except floats/doubles which are not yet used and
|
||||||
standardised by bipack format.
|
standardized by bipack format.
|
||||||
|
|
||||||
- All integers, signed and unsigned, are encoded with variable-length `smartint`.
|
- All integers, signed and unsigned, are encoded with variable-length `smartint`.
|
||||||
|
|
||||||
@ -46,9 +46,9 @@ pub struct DefinitelyBigEndian {
|
|||||||
|
|
||||||
## Fixed or variable length arrays?
|
## Fixed or variable length arrays?
|
||||||
|
|
||||||
When encoding arrays, the rule is simple:
|
When encoding arrays, the rule is simple:
|
||||||
|
|
||||||
- dynamic arrays (arrays, vectors, etc) are encoding with size, as variable-length
|
- dynamic arrays (arrays, vectors, etc.) are encoded with size, as variable-length
|
||||||
- fixed-size arrays, like tuples `(1,2,3,4,5)`, or something like `[u32; 5]` are encoded as fixed-size entities.
|
- fixed-size arrays, like tuples `(1,2,3,4,5)`, or something like `[u32; 5]` are encoded as fixed-size entities.
|
||||||
|
|
||||||
# Direct usage (no serde)
|
# Direct usage (no serde)
|
||||||
|
@ -112,7 +112,7 @@ pub trait BipackSink {
|
|||||||
let value = number.into_u64();
|
let value = number.into_u64();
|
||||||
let mut encode_seq = |ty: u8, bytes: &[u64]| -> Result<()> {
|
let mut encode_seq = |ty: u8, bytes: &[u64]| -> Result<()> {
|
||||||
if bytes.len() == 0 { self.put_u8(0)?; } else {
|
if bytes.len() == 0 { self.put_u8(0)?; } else {
|
||||||
if bytes[0] as u64 > V0LIMIT { panic!("first byte is too big (internal error)"); }
|
if bytes[0] > V0LIMIT { panic!("first byte is too big (internal error)"); }
|
||||||
self.put_u8((ty & 0x03) | ((bytes[0] as u8) << 2))?;
|
self.put_u8((ty & 0x03) | ((bytes[0] as u8) << 2))?;
|
||||||
for i in 1..bytes.len() {
|
for i in 1..bytes.len() {
|
||||||
self.put_u8(bytes[i] as u8)?;
|
self.put_u8(bytes[i] as u8)?;
|
||||||
|
@ -19,7 +19,7 @@ use crate::bipack_source::BipackError::NoDataError;
|
|||||||
/// Result of error-aware bipack function
|
/// Result of error-aware bipack function
|
||||||
pub type Result<T> = std::result::Result<T, BipackError>;
|
pub type Result<T> = std::result::Result<T, BipackError>;
|
||||||
|
|
||||||
/// There is not enought data to fulfill the request
|
/// There is not enough data to fulfill the request
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub enum BipackError {
|
pub enum BipackError {
|
||||||
@ -49,7 +49,7 @@ impl Display for BipackError {
|
|||||||
/// it is easy to implements your own.
|
/// it is easy to implements your own.
|
||||||
///
|
///
|
||||||
/// To implement source for other type, implement just [BipackSource::get_u8],
|
/// To implement source for other type, implement just [BipackSource::get_u8],
|
||||||
/// [BipackSource::eof] or mayve also
|
/// [BipackSource::eof] or maybe also
|
||||||
/// [BipackSource::get_fixed_bytes] for effectiveness.
|
/// [BipackSource::get_fixed_bytes] for effectiveness.
|
||||||
///
|
///
|
||||||
/// Unlike the [crate::bipack_sink::BipackSink] the source is returning errors. This is because
|
/// Unlike the [crate::bipack_sink::BipackSink] the source is returning errors. This is because
|
||||||
@ -86,7 +86,7 @@ pub trait BipackSource {
|
|||||||
Ok(self.get_u8()? as i8)
|
Ok(self.get_u8()? as i8)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unpack variable-length packed unsigned value, used aslo internally to store size
|
/// Unpack variable-length packed unsigned value, used also internally to store size
|
||||||
/// of arrays, binary data, strings, etc. To pack use
|
/// of arrays, binary data, strings, etc. To pack use
|
||||||
/// [crate::bipack_sink::BipackSink::put_unsigned()].
|
/// [crate::bipack_sink::BipackSink::put_unsigned()].
|
||||||
fn get_unsigned(self: &mut Self) -> Result<u64> {
|
fn get_unsigned(self: &mut Self) -> Result<u64> {
|
||||||
|
@ -16,12 +16,12 @@ impl Crc8 {
|
|||||||
t = (t << 1) ^ tmp;
|
t = (t << 1) ^ tmp;
|
||||||
for j in 0..i {
|
for j in 0..i {
|
||||||
idx = (i+j) as u8;
|
idx = (i+j) as u8;
|
||||||
table[(idx) as usize] = table[j as usize] ^ t;
|
table[idx as usize] = table[j as usize] ^ t;
|
||||||
}
|
}
|
||||||
i *= 2;
|
i *= 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Crc8{ table : table};
|
return Crc8{ table };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_lsb(polynomial :u8) -> Crc8 {
|
pub fn create_lsb(polynomial :u8) -> Crc8 {
|
||||||
@ -44,7 +44,7 @@ impl Crc8 {
|
|||||||
i >>= 1;
|
i >>= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Crc8{ table : table };
|
return Crc8{ table };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update CRC using previously calculated value and return new one.
|
/// Update CRC using previously calculated value and return new one.
|
||||||
@ -52,7 +52,7 @@ impl Crc8 {
|
|||||||
let mut crc_tmp = crc;
|
let mut crc_tmp = crc;
|
||||||
|
|
||||||
for i in 0..buffer.len() {
|
for i in 0..buffer.len() {
|
||||||
crc_tmp = self.table[((crc_tmp ^ buffer[i])) as usize];
|
crc_tmp = self.table[(crc_tmp ^ buffer[i]) as usize];
|
||||||
}
|
}
|
||||||
return crc_tmp;
|
return crc_tmp;
|
||||||
}
|
}
|
||||||
|
@ -161,7 +161,7 @@ impl<'de, 'a, T: BipackSource> de::Deserializer<'de> for &'a mut Deserializer<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de, 'a, T: BipackSource> serde::de::EnumAccess<'de> for &'a mut Deserializer<T> {
|
impl<'de, 'a, T: BipackSource> de::EnumAccess<'de> for &'a mut Deserializer<T> {
|
||||||
type Error = BipackError;
|
type Error = BipackError;
|
||||||
type Variant = Self;
|
type Variant = Self;
|
||||||
|
|
||||||
@ -175,7 +175,7 @@ impl<'de, 'a, T: BipackSource> serde::de::EnumAccess<'de> for &'a mut Deserializ
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de, 'a, T: BipackSource> serde::de::VariantAccess<'de> for &'a mut Deserializer<T> {
|
impl<'de, 'a, T: BipackSource> de::VariantAccess<'de> for &'a mut Deserializer<T> {
|
||||||
type Error = BipackError;
|
type Error = BipackError;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -213,7 +213,7 @@ impl<'a, T: BipackSource> SimpleSeq<'a, T> {
|
|||||||
fn new(de: &'a mut Deserializer<T>, size: usize) -> Self {
|
fn new(de: &'a mut Deserializer<T>, size: usize) -> Self {
|
||||||
SimpleSeq {
|
SimpleSeq {
|
||||||
de,
|
de,
|
||||||
size: size,
|
size,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -318,7 +318,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_map() -> Result<()> {
|
fn test_map() -> Result<()> {
|
||||||
let mut src = HashMap::new();
|
let mut src = HashMap::new();
|
||||||
src.insert("foo".to_string(), 1);
|
// src.insert("foo".to_string(), 1);
|
||||||
src.insert("foo".to_string(), 42);
|
src.insert("foo".to_string(), 42);
|
||||||
src.insert("bar".to_string(), 1);
|
src.insert("bar".to_string(), 1);
|
||||||
src.insert("baz".to_string(), 17);
|
src.insert("baz".to_string(), 17);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! # Fixed Size Integers
|
//! # Fixed Size Integers
|
||||||
//!
|
//!
|
||||||
//! In some cases, the use of variably length encoded data may not be
|
//! In some cases, the use of variably length encoded data may not be
|
||||||
//! preferrable. These modules, for use with `#[serde(with = ...)]`
|
//! preferable. These modules, for use with `#[serde(with = ...)]`
|
||||||
//! "opt out" of variable length encoding.
|
//! "opt out" of variable length encoding.
|
||||||
//!
|
//!
|
||||||
//! Support explicitly not provided for `usize` or `isize`, as
|
//! Support explicitly not provided for `usize` or `isize`, as
|
||||||
|
14
src/lib.rs
14
src/lib.rs
@ -28,24 +28,24 @@
|
|||||||
//!
|
//!
|
||||||
//! ## Utilities
|
//! ## Utilities
|
||||||
//!
|
//!
|
||||||
//! - to siplify encoding of unsigned ints the [bipack_sink::IntoU64] trait is used with
|
//! - to simplify encoding of unsigned ints the [bipack_sink::IntoU64] trait is used with
|
||||||
//! imlementation for usual u* types.
|
//! implementation for usual u* types.
|
||||||
//!
|
//!
|
||||||
//! - [tools::to_dump] utility function converts binary data into human-readable dump as in old goot
|
//! - [tools::to_dump] utility function converts binary data into human-readable dump as in old good
|
||||||
//! times (address, bytes, ASCII characters).
|
//! times (address, bytes, ASCII characters).
|
||||||
//!
|
//!
|
||||||
//! - [tools::StringBuilder] minimalistic growing strings builder.
|
//! - [tools::StringBuilder] minimalistic growing strings builder.
|
||||||
//!
|
//!
|
||||||
//! ## About Bipack format
|
//! ## About Bipack format
|
||||||
//!
|
//!
|
||||||
//! This is a binary format created wround the idea of bit-effectiveness and not disclosing
|
//! This is a binary format created around the idea of bit-effectiveness and not disclosing
|
||||||
//! inner data structure. Unlike many known binary and text formats, liek JSON, BSON, BOSS, and
|
//! inner data structure. Unlike many known binary and text formats, like JSON, BSON, BOSS, and
|
||||||
//! many others, it does not includes field names into packed binaries.
|
//! many others, it does not includes field names into packed binaries.
|
||||||
//!
|
//!
|
||||||
//! It also uses ratinally packed variable length format very effective for unsigned integers of
|
//! It also uses rationally-packed variable length format very effective for unsigned integers of
|
||||||
//! various sizes. This implementation supports sizes for u8, u16, u32 and u64. It is capable of
|
//! various sizes. This implementation supports sizes for u8, u16, u32 and u64. It is capable of
|
||||||
//! holding longer values too but for big numbers the fixed size encoding is mostly more effective.
|
//! holding longer values too but for big numbers the fixed size encoding is mostly more effective.
|
||||||
//! This rarional encoding format is called `smartint` and is internally used everywhere when one
|
//! This rational encoding format is called `smartint` and is internally used everywhere when one
|
||||||
//! need to pack unsigned number, unless the fixed size is important.
|
//! need to pack unsigned number, unless the fixed size is important.
|
||||||
//!
|
//!
|
||||||
//! ### Varint encoding
|
//! ### Varint encoding
|
||||||
|
@ -19,7 +19,7 @@ pub fn to_bytes<T>(value: &T) -> Result<Vec<u8>>
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_buffer<'a, T: Serialize>(value: &T, buffer: & 'a mut [u8]) -> Result<& 'a [u8]> {
|
pub fn to_buffer<'a, T: Serialize>(value: &T, buffer: & 'a mut [u8]) -> Result<& 'a [u8]> {
|
||||||
let mut serializer = Serializer { output: BufferSink { buffer: buffer, pos: 0}};
|
let mut serializer = Serializer { output: BufferSink { buffer, pos: 0}};
|
||||||
value.serialize(&mut serializer)?;
|
value.serialize(&mut serializer)?;
|
||||||
Ok(serializer.output.result_slice())
|
Ok(serializer.output.result_slice())
|
||||||
}
|
}
|
||||||
@ -360,7 +360,7 @@ mod test {
|
|||||||
use crate::tools::{to_dump, to_hex};
|
use crate::tools::{to_dump, to_hex};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_struct() -> std::result::Result<(), BipackError> {
|
fn test_struct() -> Result<(), BipackError> {
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Test {
|
struct Test {
|
||||||
int: u32,
|
int: u32,
|
||||||
@ -384,7 +384,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_enum() -> std::result::Result<(), FromUtf8Error> {
|
fn test_enum() -> Result<(), FromUtf8Error> {
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
enum E {
|
enum E {
|
||||||
Unit,
|
Unit,
|
||||||
@ -428,7 +428,7 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_map() -> error::Result<()> {
|
fn test_map() -> error::Result<()> {
|
||||||
let mut src = HashMap::new();
|
let mut src = HashMap::new();
|
||||||
src.insert("foo", 1);
|
// src.insert("foo", 1);
|
||||||
src.insert("foo", 42);
|
src.insert("foo", 42);
|
||||||
src.insert("bar", 1);
|
src.insert("bar", 1);
|
||||||
src.insert("baz", 17);
|
src.insert("baz", 17);
|
||||||
|
Loading…
Reference in New Issue
Block a user