A simple and efficient data compression library with support for multiple compression algorithms.
- Multiple compression algorithm support:
- Snappy compression (default)
- Gzip compression (TO-BE)
- Compression algorithm factory pattern
- Serialization/deserialization integration (using serde_json)
- Extensible design (easily add new compression algorithms)
- Feature flags for conditional compilation (TO-BE)
[dependencies]
bering-compression = { git = "https://github.com/BeringLab/bering-compression.git", branch = "main" }
# (TO-BE)
# Optional: Enable specific compression algorithms
# bering-compression = { git = "https://github.com/BeringLab/bering-compression.git", branch = "main", features = ["myalgo"] }
# Optional: Enable all compression algorithms
# bering-compression = { git = "https://github.com/BeringLab/bering-compression.git", branch = "main", features = ["all"] }use bering_compression::TCompressible;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct MyData {
field1: String,
field2: i32,
}
fn main() {
let data = MyData {
field1: "Hello Beringlab".to_string(),
field2: 42,
};
// Compress data using default algorithm (Snappy)
let compressed = data.compress().unwrap();
// Decompress data
let decompressed: MyData = TCompressible::decompress(&compressed).unwrap();
assert_eq!(data, decompressed);
}use bering_compression::{TCompressible, SnappyCompressor};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct MyData {
field1: String,
field2: i32,
}
fn main() {
let data = MyData {
field1: "Hello Beringlab".to_string(),
field2: 42,
};
// Create a compressor instance
let compressor = SnappyCompressor;
// Compress data with specific compressor
let compressed = data.compress_with(&compressor).unwrap();
// Decompress data with specific compressor
let decompressed: MyData = TCompressible::decompress_with(&compressed, &compressor).unwrap();
assert_eq!(data, decompressed);
}use bering_compression::{TCompressible, CompressionAlgorithm};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct MyData {
field1: String,
field2: i32,
}
fn main() {
let data = MyData {
field1: "Hello Beringlab".to_string(),
field2: 42,
};
// Compress data with specific algorithm
let compressed = data.compress_with_algorithm(CompressionAlgorithm::Snappy).unwrap();
// Decompress data with specific algorithm
let decompressed: MyData = TCompressible::decompress_with_algorithm(&compressed, CompressionAlgorithm::Snappy).unwrap();
assert_eq!(data, decompressed);
// Below Code is not implemented yet (TO-BE)
/*
// When zstd feature is enabled
#[cfg(feature = "bzip2")]
{
//
}
*/
}use bering_compression::{TCompressor, CompressorError};
use std::io;
// Define your custom compressor
struct MyCustomCompressor;
impl TCompressor for MyCustomCompressor {
fn compress(&self, value: &[u8]) -> Result<Vec<u8>, CompressorError> {
// Implement compression logic
// ...
Ok(value.to_vec()) // Example (no actual compression)
}
fn decompress(&self, compressed: &[u8]) -> Result<Vec<u8>, CompressorError> {
// Implement decompression logic
// ...
Ok(compressed.to_vec()) // Example (no actual decompression)
}
}
// To use your custom compressor:
// let compressor = MyCustomCompressor;
// let compressed = my_data.compress_with(&compressor).unwrap();To add a new compression algorithm to the library:
- Create a new module in
src/compressors/(e.g.,my_algo.rs) - Implement the
TCompressortrait for your algorithm - Add your algorithm to the
CompressionAlgorithmenum insrc/compressors/mod.rs - Update the
CompressorFactory::get_compressormethod to handle your algorithm - Add appropriate feature flags in
Cargo.toml
Example (TO-BE):
// src/compressors/my_algo.rs
use super::TCompressor;
use crate::error::CompressorError;
use std::io;
pub struct MyAlgoCompressor;
impl TCompressor for MyAlgoCompressor {
fn compress(&self, value: &[u8]) -> Result<Vec<u8>, CompressorError> {
// Implement compression using your algorithm
// ...
}
fn decompress(&self, compressed: &[u8]) -> Result<Vec<u8>, CompressorError> {
// Implement decompression using your algorithm
// ...
}
}// Update src/compressors/mod.rs (TO-BE)
pub mod my_algo;
pub use my_algo::MyAlgoCompressor;
pub enum CompressionAlgorithm {
Snappy,
MyAlgo, // Add your algorithm
}
impl CompressorFactory {
pub fn get_compressor(algorithm: CompressionAlgorithm) -> Box<dyn TCompressor> {
match algorithm {
CompressionAlgorithm::Snappy => Box::new(SnappyCompressor),
CompressionAlgorithm::MyAlgo => Box::new(my_algo::MyAlgoCompressor), // Add algorithm
}
}
}use bering_compression::{TCompressible, CompressorError};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct MyData {
field1: String,
field2: i32,
}
fn main() -> Result<(), CompressorError> {
let data = MyData {
field1: "Hello Beringlab".to_string(),
field2: 42,
};
// Compress data
let compressed = data.compress()?;
// Decompress data
let decompressed: MyData = TCompressible::decompress(&compressed)?;
assert_eq!(data, decompressed);
Ok(())
}MIT