Skip to content

Commit

Permalink
Merge pull request #245 from Nullus157/construct-brotli-encoder
Browse files Browse the repository at this point in the history
feat: impl Default for brotli::EncoderParams
  • Loading branch information
robjtede committed Sep 12, 2023
2 parents c2c7a24 + 4e8abd4 commit ece5584
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0),

## Unreleased

## 0.4.3

- Implement `Default` for `brotli::EncoderParams`.

## 0.4.2

- Add top-level `brotli` module containing stable `brotli` crate wrapper types.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "async-compression"
version = "0.4.2"
version = "0.4.3"
authors = ["Wim Looman <wim@nemo157.com>", "Allen Bui <fairingrey@gmail.com>"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ additional terms or conditions.
[7]: https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square
[8]: https://docs.rs/async-compression
[9]: https://img.shields.io/crates/l/async-compression.svg?style=flat-square
[10]: https://deps.rs/crate/async-compression/0.4.2/status.svg?style=flat-square
[10]: https://deps.rs/crate/async-compression/0.4.3/status.svg?style=flat-square
[11]: https://deps.rs/crate/async-compression/0.4.2
[releases]: https://github.com/Nullus157/async-compression/releases
64 changes: 46 additions & 18 deletions src/brotli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@

use brotli::enc::backward_references::{BrotliEncoderMode, BrotliEncoderParams};

/// A compression parameter for Brotli. This is a stable wrapper around Brotli's own encoder params
/// type, to abstract over different versions of the Brotli library.
/// Brotli compression parameters builder. This is a stable wrapper around Brotli's own encoder
/// params type, to abstract over different versions of the Brotli library.
///
/// See the [Brotli documentation](https://www.brotli.org/encode.html#a9a8) for more information on
/// these parameters.

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
///
/// # Examples
///
/// ```
/// use async_compression::brotli;
///
/// let params = brotli::EncoderParams::default()
/// .window_size(12)
/// .text_mode();
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct EncoderParams {
window_size: i32,
block_size: i32,
size_hint: usize,
mode: BrotliEncoderMode,
window_size: Option<i32>,
block_size: Option<i32>,
size_hint: Option<usize>,
mode: Option<BrotliEncoderMode>,
}

impl EncoderParams {
Expand All @@ -23,7 +32,7 @@ impl EncoderParams {
///
/// `window_size` is clamped to `0 <= window_size <= 24`.
pub fn window_size(mut self, window_size: i32) -> Self {
self.window_size = window_size.clamp(0, 24);
self.window_size = Some(window_size.clamp(0, 24));
self
}

Expand All @@ -33,13 +42,13 @@ impl EncoderParams {
///
/// `block_size` is clamped to `16 <= block_size <= 24`.
pub fn block_size(mut self, block_size: i32) -> Self {
self.block_size = block_size.clamp(16, 24);
self.block_size = Some(block_size.clamp(16, 24));
self
}

/// Sets hint for size of data to be compressed.
pub fn size_hint(mut self, size_hint: usize) -> Self {
self.size_hint = size_hint;
self.size_hint = Some(size_hint);
self
}

Expand All @@ -50,17 +59,36 @@ impl EncoderParams {
///
/// Used as Brotli's `mode` parameter.
pub fn text_mode(mut self) -> Self {
self.mode = BrotliEncoderMode::BROTLI_MODE_TEXT;
self.mode = Some(BrotliEncoderMode::BROTLI_MODE_TEXT);
self
}

pub(crate) fn as_brotli(&self) -> BrotliEncoderParams {
BrotliEncoderParams {
lgwin: self.window_size,
lgblock: self.block_size,
size_hint: self.size_hint,
mode: self.mode,
..Default::default()
let mut params = BrotliEncoderParams::default();

let Self {
window_size,
block_size,
size_hint,
mode,
} = self;

if let Some(window_size) = window_size {
params.lgwin = *window_size;
}

if let Some(block_size) = block_size {
params.lgblock = *block_size;
}

if let Some(size_hint) = size_hint {
params.size_hint = *size_hint;
}

if let Some(mode) = mode {
params.mode = *mode;
}

params
}
}

0 comments on commit ece5584

Please sign in to comment.