compression_codecs/lzma/
encoder.rs

1use crate::{Encode, Xz2Encoder, Xz2FileFormat};
2use compression_core::{util::PartialBuffer, Level};
3use std::io::Result;
4
5/// Lzma encoding stream
6#[derive(Debug)]
7pub struct LzmaEncoder {
8    inner: Xz2Encoder,
9}
10
11impl LzmaEncoder {
12    pub fn new(level: Level) -> Self {
13        Self {
14            inner: Xz2Encoder::new(Xz2FileFormat::Lzma, level),
15        }
16    }
17}
18
19impl From<Xz2Encoder> for LzmaEncoder {
20    fn from(inner: Xz2Encoder) -> Self {
21        Self { inner }
22    }
23}
24
25impl Encode for LzmaEncoder {
26    fn encode(
27        &mut self,
28        input: &mut PartialBuffer<impl AsRef<[u8]>>,
29        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
30    ) -> Result<()> {
31        self.inner.encode(input, output)
32    }
33
34    fn flush(
35        &mut self,
36        _output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
37    ) -> Result<bool> {
38        // Flush on LZMA 1 is not supported
39        Ok(true)
40    }
41
42    fn finish(
43        &mut self,
44        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
45    ) -> Result<bool> {
46        self.inner.finish(output)
47    }
48}