use core::fmt;
use alloc::vec::Vec;
use crate::compress::CompressorRef;
use lz4rip_core::CompressError;
pub struct Compressor {
inner: CompressorRef<'static>,
#[allow(dead_code)] dict: Vec<u8>,
}
impl fmt::Debug for Compressor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Compressor")
.field("dict_len", &self.dict.len())
.finish()
}
}
impl Compressor {
pub fn new() -> Self {
Compressor {
inner: CompressorRef::new(),
dict: Vec::new(),
}
}
pub fn with_dict(dict: &[u8]) -> Self {
let dict = dict.to_vec();
let dict_ref: &'static [u8] =
unsafe { core::slice::from_raw_parts(dict.as_ptr(), dict.len()) };
Compressor {
inner: CompressorRef::with_dict(dict_ref),
dict,
}
}
pub fn compress_into(
&mut self,
input: &[u8],
output: &mut [u8],
) -> Result<usize, CompressError> {
self.inner.compress_into(input, output)
}
pub fn compress(&mut self, input: &[u8]) -> Vec<u8> {
self.inner.compress(input)
}
}
impl Default for Compressor {
fn default() -> Self {
Self::new()
}
}