pub use self::default::DefaultLz77Encoder;
mod default;
pub const MAX_LENGTH: u16 = 258;
pub const MAX_DISTANCE: u16 = 32_768;
pub const MAX_WINDOW_SIZE: u16 = MAX_DISTANCE;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Code {
Literal(u8),
Pointer {
length: u16,
backward_distance: u16,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CompressionLevel {
None,
Fast,
Balance,
Best,
}
pub trait Sink {
fn consume(&mut self, code: Code);
}
impl<'a, T> Sink for &'a mut T
where
T: Sink,
{
fn consume(&mut self, code: Code) {
(*self).consume(code);
}
}
pub trait Lz77Encode {
fn encode<S>(&mut self, buf: &[u8], sink: S)
where
S: Sink;
fn flush<S>(&mut self, sink: S)
where
S: Sink;
fn compression_level(&self) -> CompressionLevel {
CompressionLevel::Balance
}
fn window_size(&self) -> u16 {
MAX_WINDOW_SIZE
}
}
#[derive(Debug, Default)]
pub struct NoCompressionLz77Encoder;
impl NoCompressionLz77Encoder {
pub fn new() -> Self {
NoCompressionLz77Encoder
}
}
impl Lz77Encode for NoCompressionLz77Encoder {
fn encode<S>(&mut self, buf: &[u8], mut sink: S)
where
S: Sink,
{
for c in buf.iter().cloned().map(Code::Literal) {
sink.consume(c);
}
}
#[allow(unused_variables)]
fn flush<S>(&mut self, sink: S)
where
S: Sink,
{
}
fn compression_level(&self) -> CompressionLevel {
CompressionLevel::None
}
}