Skip to content

Commit d0d8cdd

Browse files
committed
feat: impl Default for brotli::EncoderParams
1 parent c2c7a24 commit d0d8cdd

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0),
66

77
## Unreleased
88

9+
- Implement `Default` for `brotli::EncoderParams`.
10+
911
## 0.4.2
1012

1113
- Add top-level `brotli` module containing stable `brotli` crate wrapper types.

src/brotli.rs

+46-18
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,27 @@
22
33
use brotli::enc::backward_references::{BrotliEncoderMode, BrotliEncoderParams};
44

5-
/// A compression parameter for Brotli. This is a stable wrapper around Brotli's own encoder params
6-
/// type, to abstract over different versions of the Brotli library.
5+
/// Brotli compression parameters builder. This is a stable wrapper around Brotli's own encoder
6+
/// params type, to abstract over different versions of the Brotli library.
77
///
88
/// See the [Brotli documentation](https://www.brotli.org/encode.html#a9a8) for more information on
99
/// these parameters.
10-
11-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10+
///
11+
/// # Examples
12+
///
13+
/// ```
14+
/// use async_compression::brotli;
15+
///
16+
/// let params = brotli::EncoderParams::default()
17+
/// .window_size(12)
18+
/// .text_mode();
19+
/// ```
20+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
1221
pub struct EncoderParams {
13-
window_size: i32,
14-
block_size: i32,
15-
size_hint: usize,
16-
mode: BrotliEncoderMode,
22+
window_size: Option<i32>,
23+
block_size: Option<i32>,
24+
size_hint: Option<usize>,
25+
mode: Option<BrotliEncoderMode>,
1726
}
1827

1928
impl EncoderParams {
@@ -23,7 +32,7 @@ impl EncoderParams {
2332
///
2433
/// `window_size` is clamped to `0 <= window_size <= 24`.
2534
pub fn window_size(mut self, window_size: i32) -> Self {
26-
self.window_size = window_size.clamp(0, 24);
35+
self.window_size = Some(window_size.clamp(0, 24));
2736
self
2837
}
2938

@@ -33,13 +42,13 @@ impl EncoderParams {
3342
///
3443
/// `block_size` is clamped to `16 <= block_size <= 24`.
3544
pub fn block_size(mut self, block_size: i32) -> Self {
36-
self.block_size = block_size.clamp(16, 24);
45+
self.block_size = Some(block_size.clamp(16, 24));
3746
self
3847
}
3948

4049
/// Sets hint for size of data to be compressed.
4150
pub fn size_hint(mut self, size_hint: usize) -> Self {
42-
self.size_hint = size_hint;
51+
self.size_hint = Some(size_hint);
4352
self
4453
}
4554

@@ -50,17 +59,36 @@ impl EncoderParams {
5059
///
5160
/// Used as Brotli's `mode` parameter.
5261
pub fn text_mode(mut self) -> Self {
53-
self.mode = BrotliEncoderMode::BROTLI_MODE_TEXT;
62+
self.mode = Some(BrotliEncoderMode::BROTLI_MODE_TEXT);
5463
self
5564
}
5665

5766
pub(crate) fn as_brotli(&self) -> BrotliEncoderParams {
58-
BrotliEncoderParams {
59-
lgwin: self.window_size,
60-
lgblock: self.block_size,
61-
size_hint: self.size_hint,
62-
mode: self.mode,
63-
..Default::default()
67+
let mut params = BrotliEncoderParams::default();
68+
69+
let Self {
70+
window_size,
71+
block_size,
72+
size_hint,
73+
mode,
74+
} = self;
75+
76+
if let Some(window_size) = window_size {
77+
params.lgwin = *window_size;
6478
}
79+
80+
if let Some(block_size) = block_size {
81+
params.lgblock = *block_size;
82+
}
83+
84+
if let Some(size_hint) = size_hint {
85+
params.size_hint = *size_hint;
86+
}
87+
88+
if let Some(mode) = mode {
89+
params.mode = *mode;
90+
}
91+
92+
params
6593
}
6694
}

0 commit comments

Comments
 (0)