Skip to content

Commit

Permalink
Capacity to binary Encode/Decode the several parts
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrolo committed Oct 9, 2023
1 parent 1ee4b6e commit 5750e59
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion brro-compressor/src/compressor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bincode::config::{self, Configuration};
use bincode::{Decode, Encode};
use self::constant::constant;
use self::fft::fft;
use self::noop::noop;
Expand All @@ -7,7 +8,7 @@ pub mod noop;
pub mod constant;
pub mod fft;

#[derive(Default)]
#[derive(Encode, Decode, Default, Debug, Clone)]
pub enum Compressor {
#[default]
Noop,
Expand Down
19 changes: 18 additions & 1 deletion brro-compressor/src/data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::compressor::Compressor;
use bincode::{Decode, Encode};
use crate::compressor::{Compressor, BinConfig};
use crate::frame::CompressorFrame;
use crate::header::CompressorHeader;

#[derive(Encode, Decode, Debug, Clone)]
pub struct CompressedStream {
header: CompressorHeader,
data_frames: Vec<CompressorFrame>,
Expand Down Expand Up @@ -32,4 +34,19 @@ impl CompressedStream {
self.data_frames.push(compressor_frame);

}

/// Transforms the whole CompressedStream into bytes to be written to a file
pub fn to_bytes(self) -> Vec<u8> {
let config = BinConfig::get();
bincode::encode_to_vec(self, config).unwrap()
}

/// Gets a binary stream and generates a Compressed Stream
pub fn from_bytes(data: &[u8]) -> Self {
let config = BinConfig::get();
match bincode::decode_from_slice(data, config) {
Ok((compressed_stream, _)) => compressed_stream,
Err(e) => panic!("{e}")
}
}
}
2 changes: 2 additions & 0 deletions brro-compressor/src/frame/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::mem::size_of_val;
use bincode::{Decode, Encode};
use crate::compressor::Compressor;

/// This is the structure of a compressor frame
#[derive(Encode, Decode, Debug, Clone)]
pub struct CompressorFrame{
/// The frame size in bytes,
frame_size: usize,
Expand Down
4 changes: 3 additions & 1 deletion brro-compressor/src/header.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/// This will write the file headers
use bincode::{Decode, Encode};

/// This will write the file headers
#[derive(Encode, Decode, Debug, Clone)]
pub struct CompressorHeader {
initial_segment: [u8; 4],
// We should go unsigned
Expand Down

0 comments on commit 5750e59

Please sign in to comment.