feat: prepare decoding traits and make new Flat trait
This commit is contained in:
5
crates/flat/src/decode.rs
Normal file
5
crates/flat/src/decode.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
use crate::decoder::Decoder;
|
||||
|
||||
pub trait Decode<'b>: Sized {
|
||||
fn decode(d: &mut Decoder<'b>) -> Result<Self, String>;
|
||||
}
|
||||
19
crates/flat/src/decoder.rs
Normal file
19
crates/flat/src/decoder.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use crate::decode::Decode;
|
||||
|
||||
pub struct Decoder<'b> {
|
||||
buffer: &'b [u8],
|
||||
pos: usize,
|
||||
}
|
||||
|
||||
impl<'b> Decoder<'b> {
|
||||
pub fn new(bytes: &'b [u8]) -> Decoder<'b> {
|
||||
Decoder {
|
||||
buffer: bytes,
|
||||
pos: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decode<T: Decode<'b>>(&mut self) -> Result<T, String> {
|
||||
T::decode(self)
|
||||
}
|
||||
}
|
||||
@@ -83,19 +83,6 @@ impl<T: Encode> Encode for Box<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, K> Encode for (T, K)
|
||||
where
|
||||
T: Encode,
|
||||
K: Encode,
|
||||
{
|
||||
fn encode(&self, e: &mut Encoder) -> Result<(), String> {
|
||||
self.0.encode(e)?;
|
||||
self.1.encode(e)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Encode for Filler {
|
||||
fn encode(&self, e: &mut Encoder) -> Result<(), String> {
|
||||
e.filler();
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
mod decode;
|
||||
mod decoder;
|
||||
mod encode;
|
||||
mod encoder;
|
||||
mod filler;
|
||||
@@ -8,33 +10,58 @@ pub mod en {
|
||||
pub use super::encoder::*;
|
||||
}
|
||||
|
||||
pub fn encode<T>(value: T) -> Result<Vec<u8>, String>
|
||||
pub mod de {
|
||||
pub use super::decode::*;
|
||||
pub use super::decoder::*;
|
||||
}
|
||||
|
||||
pub trait Flat<'b>: en::Encode + de::Decode<'b> {
|
||||
fn flat(&self) -> Result<Vec<u8>, String> {
|
||||
encode(self)
|
||||
}
|
||||
|
||||
fn unflat(bytes: &'b [u8]) -> Result<Self, String> {
|
||||
decode(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode<T>(value: &T) -> Result<Vec<u8>, String>
|
||||
where
|
||||
T: en::Encode,
|
||||
{
|
||||
let mut e = en::Encoder::new();
|
||||
|
||||
e.encode((value, filler::Filler::FillerEnd))?;
|
||||
value.encode(&mut e)?;
|
||||
e.encode(filler::Filler::FillerEnd)?;
|
||||
|
||||
Ok(e.buffer)
|
||||
}
|
||||
|
||||
pub fn decode<'b, T>(bytes: &'b [u8]) -> Result<T, String>
|
||||
where
|
||||
T: de::Decode<'b>,
|
||||
{
|
||||
let mut d = de::Decoder::new(bytes);
|
||||
|
||||
d.decode()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
#[test]
|
||||
fn encode_bool() {
|
||||
let bytes = super::encode(true).unwrap();
|
||||
let bytes = super::encode(&true).unwrap();
|
||||
|
||||
assert_eq!(bytes, vec![0b10000001]);
|
||||
|
||||
let bytes = super::encode(false).unwrap();
|
||||
let bytes = super::encode(&false).unwrap();
|
||||
|
||||
assert_eq!(bytes, vec![0b00000001]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_u8() {
|
||||
let bytes = super::encode(3_u8).unwrap();
|
||||
let bytes = super::encode(&3_u8).unwrap();
|
||||
|
||||
assert_eq!(bytes, vec![0b00000011, 0b00000001]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user