diff --git a/brro-compressor/src/compressor/mod.rs b/brro-compressor/src/compressor/mod.rs index b080a15..0970e62 100644 --- a/brro-compressor/src/compressor/mod.rs +++ b/brro-compressor/src/compressor/mod.rs @@ -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; @@ -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, diff --git a/brro-compressor/src/data.rs b/brro-compressor/src/data.rs index 8b705ca..74835e8 100644 --- a/brro-compressor/src/data.rs +++ b/brro-compressor/src/data.rs @@ -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, @@ -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 { + 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}") + } + } } diff --git a/brro-compressor/src/frame/mod.rs b/brro-compressor/src/frame/mod.rs index 21e4665..6449108 100644 --- a/brro-compressor/src/frame/mod.rs +++ b/brro-compressor/src/frame/mod.rs @@ -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, diff --git a/brro-compressor/src/header.rs b/brro-compressor/src/header.rs index 2a84fcd..413fd86 100644 --- a/brro-compressor/src/header.rs +++ b/brro-compressor/src/header.rs @@ -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