async_compression/
lz4.rs

1//! This module contains lz4-specific types for async-compression.
2
3pub use lz4::liblz4::BlockSize;
4use lz4::{
5    liblz4::{BlockChecksum, FrameType, LZ4FFrameInfo, LZ4FPreferences},
6    BlockMode, ContentChecksum,
7};
8
9/// lz4 compression parameters builder. This is a stable wrapper around lz4's own encoder
10/// params type, to abstract over different versions of the lz4 library.
11///
12/// See the [lz4 documentation](https://github.com/lz4/lz4/blob/dev/doc/lz4frame_manual.html)
13/// for more information on these parameters.
14///
15/// # Examples
16///
17/// ```
18/// use async_compression::lz4;
19///
20/// let params = lz4::EncoderParams::default()
21///     .block_size(lz4::BlockSize::Max1MB)
22///     .content_checksum(true);
23/// ```
24#[derive(Clone, Debug, Default)]
25pub struct EncoderParams {
26    block_size: Option<BlockSize>,
27    block_checksum: Option<BlockChecksum>,
28    content_checksum: Option<ContentChecksum>,
29}
30
31impl EncoderParams {
32    /// Sets input block size.
33    pub fn block_size(mut self, block_size: BlockSize) -> Self {
34        self.block_size = Some(block_size);
35        self
36    }
37
38    /// Add a 32-bit checksum of frame's decompressed data.
39    pub fn content_checksum(mut self, enable: bool) -> Self {
40        self.content_checksum = Some(if enable {
41            ContentChecksum::ChecksumEnabled
42        } else {
43            ContentChecksum::NoChecksum
44        });
45        self
46    }
47
48    /// Each block followed by a checksum of block's compressed data.
49    pub fn block_checksum(mut self, enable: bool) -> Self {
50        self.block_checksum = Some(if enable {
51            BlockChecksum::BlockChecksumEnabled
52        } else {
53            BlockChecksum::NoBlockChecksum
54        });
55        self
56    }
57
58    pub(crate) fn as_lz4(&self) -> LZ4FPreferences {
59        let block_size_id = self.block_size.clone().unwrap_or(BlockSize::Default);
60        let content_checksum_flag = self
61            .content_checksum
62            .clone()
63            .unwrap_or(ContentChecksum::NoChecksum);
64        let block_checksum_flag = self
65            .block_checksum
66            .clone()
67            .unwrap_or(BlockChecksum::NoBlockChecksum);
68
69        LZ4FPreferences {
70            frame_info: LZ4FFrameInfo {
71                block_size_id,
72                block_mode: BlockMode::Linked,
73                content_checksum_flag,
74                frame_type: FrameType::Frame,
75                content_size: 0,
76                dict_id: 0,
77                block_checksum_flag,
78            },
79            compression_level: 0,
80            auto_flush: 0,
81            favor_dec_speed: 0,
82            reserved: [0; 3],
83        }
84    }
85}