1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
///! An implementation of [`Compressor`] for the `BGZF` format.
use crate::Compressor;

/// A BGZF compressor.
pub struct BgzfCompressor {
    inner: bgzf::Compressor,
}

impl Compressor for BgzfCompressor {
    type CompressionLevel = bgzf::CompressionLevel;
    type Error = bgzf::BgzfError;

    const BLOCK_SIZE: usize = bgzf::BGZF_BLOCK_SIZE;

    fn new(compression_level: Self::CompressionLevel) -> Self {
        Self { inner: bgzf::Compressor::new(compression_level) }
    }

    fn compress(
        &mut self,
        input: &[u8],
        output: &mut Vec<u8>,
        is_last: bool,
    ) -> Result<(), Self::Error> {
        self.inner.compress(input, output)?;
        if is_last {
            bgzf::Compressor::append_eof(output);
        }
        Ok(())
    }

    fn new_compression_level(compression_level: u8) -> Result<Self::CompressionLevel, Self::Error> {
        bgzf::CompressionLevel::new(compression_level)
    }
}