wae_https/middleware/
compression.rs1use tower_http::compression::CompressionLayer as TowerCompressionLayer;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum CompressionAlgorithm {
11 Gzip,
13
14 Brotli,
16
17 Deflate,
19}
20
21#[derive(Debug, Clone)]
23pub struct CompressionConfig {
24 pub enable_gzip: bool,
26
27 pub enable_brotli: bool,
29
30 pub enable_deflate: bool,
32}
33
34impl Default for CompressionConfig {
35 fn default() -> Self {
36 Self { enable_gzip: true, enable_brotli: true, enable_deflate: true }
37 }
38}
39
40impl CompressionConfig {
41 pub fn new() -> Self {
43 Self::default()
44 }
45
46 pub fn with_gzip(mut self, enable: bool) -> Self {
48 self.enable_gzip = enable;
49 self
50 }
51
52 pub fn with_brotli(mut self, enable: bool) -> Self {
54 self.enable_brotli = enable;
55 self
56 }
57
58 pub fn with_deflate(mut self, enable: bool) -> Self {
60 self.enable_deflate = enable;
61 self
62 }
63}
64
65pub struct Compression {
70 config: CompressionConfig,
71}
72
73impl Compression {
74 pub fn new() -> Self {
76 Self { config: CompressionConfig::new() }
77 }
78
79 pub fn with_config(config: CompressionConfig) -> Self {
81 Self { config }
82 }
83
84 pub fn config(&self) -> &CompressionConfig {
86 &self.config
87 }
88
89 pub fn build(&self) -> TowerCompressionLayer {
91 let mut layer = TowerCompressionLayer::new();
92
93 if self.config.enable_gzip {
94 layer = layer.gzip(true);
95 }
96
97 if self.config.enable_brotli {
98 layer = layer.br(true);
99 }
100
101 if self.config.enable_deflate {
102 layer = layer.deflate(true);
103 }
104
105 layer
106 }
107}
108
109impl Default for Compression {
110 fn default() -> Self {
111 Self::new()
112 }
113}
114
115pub struct CompressionBuilder {
119 config: CompressionConfig,
120}
121
122impl CompressionBuilder {
123 pub fn new() -> Self {
125 Self { config: CompressionConfig::new() }
126 }
127
128 pub fn gzip(mut self) -> Self {
130 self.config.enable_gzip = true;
131 self
132 }
133
134 pub fn no_gzip(mut self) -> Self {
136 self.config.enable_gzip = false;
137 self
138 }
139
140 pub fn brotli(mut self) -> Self {
142 self.config.enable_brotli = true;
143 self
144 }
145
146 pub fn no_brotli(mut self) -> Self {
148 self.config.enable_brotli = false;
149 self
150 }
151
152 pub fn deflate(mut self) -> Self {
154 self.config.enable_deflate = true;
155 self
156 }
157
158 pub fn no_deflate(mut self) -> Self {
160 self.config.enable_deflate = false;
161 self
162 }
163
164 pub fn build(self) -> Compression {
166 Compression::with_config(self.config)
167 }
168}
169
170impl Default for CompressionBuilder {
171 fn default() -> Self {
172 Self::new()
173 }
174}