lazippy 0.0.1

Pure-Rust LZMA (Lempel-Ziv-Markov chain Algorithm), part of the 8z umbrella
Documentation
//! LZMA sliding-window dictionary stub.

use crate::error::{LazippyError, LazippyResult};

/// The LZMA sliding-window dictionary used during decompression.
pub struct Dict {
    buf: Vec<u8>,
    pos: usize,
    size: usize,
}

impl Dict {
    /// Create a new dictionary with the given capacity.
    pub fn new(size: usize) -> Self {
        Dict {
            buf: vec![0u8; size],
            pos: 0,
            size,
        }
    }

    /// Copy `length` bytes from `distance` bytes back in the dictionary to the output.
    ///
    /// # Errors
    /// Always returns [`LazippyError::NotYetImplemented`] until the dictionary is implemented.
    pub fn copy_match(
        &mut self,
        _length: u32,
        _distance: u32,
        _output: &mut Vec<u8>,
    ) -> LazippyResult<()> {
        Err(LazippyError::NotYetImplemented)
    }

    /// Append a literal byte to the dictionary and output.
    ///
    /// # Errors
    /// Always returns [`LazippyError::NotYetImplemented`] until the dictionary is implemented.
    pub fn push_literal(&mut self, _byte: u8, _output: &mut Vec<u8>) -> LazippyResult<()> {
        let _ = (self.buf.as_slice(), self.pos, self.size); // silence unused warnings
        Err(LazippyError::NotYetImplemented)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn dict_copy_match_is_stub() {
        let mut dict = Dict::new(4096);
        let mut out = Vec::new();
        let result = dict.copy_match(3, 1, &mut out);
        assert!(
            matches!(result, Err(LazippyError::NotYetImplemented)),
            "expected NotYetImplemented, got {result:?}"
        );
    }

    #[test]
    fn dict_push_literal_is_stub() {
        let mut dict = Dict::new(4096);
        let mut out = Vec::new();
        let result = dict.push_literal(b'A', &mut out);
        assert!(
            matches!(result, Err(LazippyError::NotYetImplemented)),
            "expected NotYetImplemented, got {result:?}"
        );
    }
}