Crates.io | hydraulic |
lib.rs | hydraulic |
version | 0.1.0 |
source | src |
created_at | 2024-08-27 13:10:39.616465 |
updated_at | 2024-08-27 13:10:39.616465 |
description | An interface for generic compression library usage with a collection (unimplemented so far) of pre-existing compression libraries |
homepage | |
repository | |
max_upload_size | |
id | 1353375 |
size | 59,048 |
An interface and implementations of various compression algorithms.
As it is currently, this library is simply a plain interface without any implementations. This will be changed in future versions
:warning: This feature has not yet been added. See the CLI branch for the current status
hydraulic may be installed with the command:
cargo install hydraulic
Or by adding the following to your Cargo.toml
[dependencies]
hydraulic = { version = "0.1.0", features = ["full"]}
A simple compression function using already implemented Gzip compression.
Ensure that the "gzip" feature is enabled
[dependencies]
hydraulic = { version = "0.1.0", features = ["gzip"]}
Then, in main.rs:
use std::fs::File;
use std::io::Read;
use hydraulic::Gzip;
use hydraulic::prelude::*;
fn compress(data: &[u8]) -> File {
let alg = Gzip::new();
// Create a new compressor which writes data to "foo.txt"
let mut compressor = WriteEncoder::new(&alg, File::create("foo.txt").unwrap(), CompressionLevel::High);
// Send "data" to the buffer
compressor.queue(&data);
// Compress and write the buffer to "foo.txt"
compressor.write().unwrap();
// Finalises the compression and returns the file where data is written
let finalised = compressor.finish().unwrap();
finalised
}
Alternatively, you may implement your own compression algorithm through the provided traits:
use crate::algorithm_meta::AlgorithmMeta;
use crate::algorithms::Algorithm;
use crate::errors::compression_error::CompressionError;
use crate::errors::decompression_error::DecompressionError;
#[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Gzip {}
impl Gzip {
pub fn new() -> Self {
Self {}
}
}
impl Algorithm for Gzip {
fn finalise_encode(&self, meta: &AlgorithmMeta) -> Result<Vec<u8>, CompressionError> {
// Final encoding steps go here
}
fn finalise_decode(&self, meta: &AlgorithmMeta) -> Result<Vec<u8>, DecompressionError> {
// Final decoding steps go here
}
fn partial_encode(
&self,
data: &[u8],
meta: &AlgorithmMeta,
) -> Result<Vec<u8>, CompressionError> {
// Implement your decompression of a single data block here
}
fn partial_decode(
&self,
data: &[u8],
meta: &AlgorithmMeta,
) -> Result<Vec<u8>, DecompressionError> {
// Implement your compression of a single data block here
}
}
More examples of usage can be found here
This project is very small and only maintained by @quexeky. Contributions are greatly appreciated. A contribution guide will be added later on