chore: switch to a mono repo
This commit is contained in:
8
crates/flat/Cargo.toml
Normal file
8
crates/flat/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "flat"
|
||||
version = "0.0.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
34
crates/flat/src/encode.rs
Normal file
34
crates/flat/src/encode.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use crate::{encoder::Encoder, filler::Filler};
|
||||
|
||||
pub trait Encode {
|
||||
fn encode(&self, e: &mut Encoder) -> Result<(), String>;
|
||||
}
|
||||
|
||||
impl Encode for bool {
|
||||
fn encode(&self, e: &mut Encoder) -> Result<(), String> {
|
||||
e.bool(*self);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
78
crates/flat/src/encoder.rs
Normal file
78
crates/flat/src/encoder.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use crate::encode::Encode;
|
||||
|
||||
pub struct Encoder {
|
||||
pub buffer: Vec<u8>,
|
||||
// Int
|
||||
used_bits: usize,
|
||||
// Int
|
||||
current_byte: u8,
|
||||
}
|
||||
|
||||
impl Default for Encoder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Encoder {
|
||||
pub fn new() -> Encoder {
|
||||
Encoder {
|
||||
buffer: Vec::new(),
|
||||
used_bits: 0,
|
||||
current_byte: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Encode any type that implements [`Encode`].
|
||||
pub fn encode<T: Encode>(&mut self, x: T) -> Result<&mut Self, String> {
|
||||
x.encode(self)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn u8(&mut self, x: u8) -> Result<&mut Self, String> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Encode a `bool` value.
|
||||
pub fn bool(&mut self, x: bool) -> &mut Self {
|
||||
if x {
|
||||
self.one()
|
||||
} else {
|
||||
self.zero()
|
||||
}
|
||||
}
|
||||
|
||||
fn zero(&mut self) -> &mut Self {
|
||||
if self.used_bits == 7 {
|
||||
self.next_word();
|
||||
} else {
|
||||
self.used_bits += 1;
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
fn one(&mut self) -> &mut Self {
|
||||
if self.used_bits == 7 {
|
||||
self.current_byte |= 1;
|
||||
self.next_word();
|
||||
} else {
|
||||
self.current_byte |= 128 >> self.used_bits;
|
||||
self.used_bits += 1;
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
fn next_word(&mut self) {
|
||||
self.buffer.push(self.current_byte);
|
||||
|
||||
self.current_byte = 0;
|
||||
self.used_bits = 0;
|
||||
}
|
||||
|
||||
pub(crate) fn filler(&mut self) {
|
||||
self.current_byte |= 1;
|
||||
self.next_word();
|
||||
}
|
||||
}
|
||||
13
crates/flat/src/filler.rs
Normal file
13
crates/flat/src/filler.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
pub enum Filler {
|
||||
FillerStart(Box<Filler>),
|
||||
FillerEnd,
|
||||
}
|
||||
|
||||
impl Filler {
|
||||
pub fn len(&self) -> usize {
|
||||
match self {
|
||||
Filler::FillerStart(f) => f.len() + 1,
|
||||
Filler::FillerEnd => 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
33
crates/flat/src/lib.rs
Normal file
33
crates/flat/src/lib.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
mod encode;
|
||||
mod encoder;
|
||||
mod filler;
|
||||
|
||||
pub mod en {
|
||||
pub use super::encode::*;
|
||||
pub use super::encoder::*;
|
||||
}
|
||||
|
||||
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))?;
|
||||
|
||||
Ok(e.buffer)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
#[test]
|
||||
fn encode_bool() {
|
||||
let bytes = super::encode(true).unwrap();
|
||||
|
||||
assert_eq!(bytes, vec![0b10000001]);
|
||||
|
||||
let bytes = super::encode(false).unwrap();
|
||||
|
||||
assert_eq!(bytes, vec![0b00000001]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user