use std::io::{self, Read, Write};
#[derive(Debug, Clone, Copy)]
pub struct CompressionLevel(pub i32);
impl CompressionLevel {
pub const FAST: Self = Self(1);
pub const DEFAULT: Self = Self(3);
pub const BETTER: Self = Self(9);
pub const BEST: Self = Self(19);
pub const MAX: Self = Self(22);
}
impl Default for CompressionLevel {
fn default() -> Self {
Self::MAX }
}
#[derive(Clone)]
pub struct CompressionDict {
raw_dict: Vec<u8>,
}
impl CompressionDict {
pub fn train(samples: &[&[u8]], dict_size: usize) -> io::Result<Self> {
let raw_dict = zstd::dict::from_samples(samples, dict_size).map_err(io::Error::other)?;
Ok(Self { raw_dict })
}
pub fn from_bytes(bytes: Vec<u8>) -> Self {
Self { raw_dict: bytes }
}
pub fn as_bytes(&self) -> &[u8] {
&self.raw_dict
}
pub fn len(&self) -> usize {
self.raw_dict.len()
}
pub fn is_empty(&self) -> bool {
self.raw_dict.is_empty()
}
}
pub fn compress(data: &[u8], level: CompressionLevel) -> io::Result<Vec<u8>> {
zstd::encode_all(data, level.0).map_err(io::Error::other)
}
pub fn compress_with_dict(
data: &[u8],
level: CompressionLevel,
dict: &CompressionDict,
) -> io::Result<Vec<u8>> {
let mut encoder = zstd::Encoder::with_dictionary(Vec::new(), level.0, &dict.raw_dict)
.map_err(io::Error::other)?;
encoder.write_all(data)?;
encoder.finish().map_err(io::Error::other)
}
pub fn decompress(data: &[u8]) -> io::Result<Vec<u8>> {
zstd::decode_all(data).map_err(io::Error::other)
}
pub fn decompress_with_dict(data: &[u8], dict: &CompressionDict) -> io::Result<Vec<u8>> {
let mut decoder =
zstd::Decoder::with_dictionary(data, &dict.raw_dict).map_err(io::Error::other)?;
let mut result = Vec::new();
decoder.read_to_end(&mut result)?;
Ok(result)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_roundtrip() {
let data = b"Hello, World! This is a test of compression.".repeat(100);
let compressed = compress(&data, CompressionLevel::default()).unwrap();
let decompressed = decompress(&compressed).unwrap();
assert_eq!(data, decompressed.as_slice());
assert!(compressed.len() < data.len());
}
#[test]
fn test_empty_data() {
let data: &[u8] = &[];
let compressed = compress(data, CompressionLevel::default()).unwrap();
let decompressed = decompress(&compressed).unwrap();
assert!(decompressed.is_empty());
}
#[test]
fn test_compression_levels() {
let data = b"Test data for compression levels".repeat(100);
for level in [1, 3, 9, 19] {
let compressed = compress(&data, CompressionLevel(level)).unwrap();
let decompressed = decompress(&compressed).unwrap();
assert_eq!(data.as_slice(), decompressed.as_slice());
}
}
}