2
2
3
3
use brotli:: enc:: backward_references:: { BrotliEncoderMode , BrotliEncoderParams } ;
4
4
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.
7
7
///
8
8
/// See the [Brotli documentation](https://www.brotli.org/encode.html#a9a8) for more information on
9
9
/// 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 ) ]
12
21
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 > ,
17
26
}
18
27
19
28
impl EncoderParams {
@@ -23,7 +32,7 @@ impl EncoderParams {
23
32
///
24
33
/// `window_size` is clamped to `0 <= window_size <= 24`.
25
34
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 ) ) ;
27
36
self
28
37
}
29
38
@@ -33,13 +42,13 @@ impl EncoderParams {
33
42
///
34
43
/// `block_size` is clamped to `16 <= block_size <= 24`.
35
44
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 ) ) ;
37
46
self
38
47
}
39
48
40
49
/// Sets hint for size of data to be compressed.
41
50
pub fn size_hint ( mut self , size_hint : usize ) -> Self {
42
- self . size_hint = size_hint;
51
+ self . size_hint = Some ( size_hint) ;
43
52
self
44
53
}
45
54
@@ -50,17 +59,36 @@ impl EncoderParams {
50
59
///
51
60
/// Used as Brotli's `mode` parameter.
52
61
pub fn text_mode ( mut self ) -> Self {
53
- self . mode = BrotliEncoderMode :: BROTLI_MODE_TEXT ;
62
+ self . mode = Some ( BrotliEncoderMode :: BROTLI_MODE_TEXT ) ;
54
63
self
55
64
}
56
65
57
66
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;
64
78
}
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
65
93
}
66
94
}
0 commit comments