Skip to content

Commit f8f5840

Browse files
nyurikdanielrh
authored andcommitted
Refactor encode::set_parameter
* return `bool` * use match statement
1 parent f1dad22 commit f8f5840

File tree

3 files changed

+83
-137
lines changed

3 files changed

+83
-137
lines changed

src/enc/encode.rs

Lines changed: 76 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -203,152 +203,94 @@ pub fn set_parameter(
203203
params: &mut BrotliEncoderParams,
204204
p: BrotliEncoderParameter,
205205
value: u32,
206-
) -> i32 {
207-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_MODE as i32 {
208-
params.mode = match value {
209-
0 => BrotliEncoderMode::BROTLI_MODE_GENERIC,
210-
1 => BrotliEncoderMode::BROTLI_MODE_TEXT,
211-
2 => BrotliEncoderMode::BROTLI_MODE_FONT,
212-
3 => BrotliEncoderMode::BROTLI_FORCE_LSB_PRIOR,
213-
4 => BrotliEncoderMode::BROTLI_FORCE_MSB_PRIOR,
214-
5 => BrotliEncoderMode::BROTLI_FORCE_UTF8_PRIOR,
215-
6 => BrotliEncoderMode::BROTLI_FORCE_SIGNED_PRIOR,
216-
_ => BrotliEncoderMode::BROTLI_MODE_GENERIC,
217-
};
218-
return 1i32;
219-
}
220-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_QUALITY as i32 {
221-
params.quality = value as i32;
222-
return 1i32;
223-
}
224-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_STRIDE_DETECTION_QUALITY as i32 {
225-
params.stride_detection_quality = value as u8;
226-
return 1i32;
227-
}
228-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_HIGH_ENTROPY_DETECTION_QUALITY as i32 {
229-
params.high_entropy_detection_quality = value as u8;
230-
return 1i32;
231-
}
232-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_CDF_ADAPTATION_DETECTION as i32 {
233-
params.cdf_adaptation_detection = value as u8;
234-
return 1i32;
235-
}
236-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_Q9_5 as i32 {
237-
params.q9_5 = (value != 0);
238-
return 1i32;
239-
}
240-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_PRIOR_BITMASK_DETECTION as i32 {
241-
params.prior_bitmask_detection = value as u8;
242-
return 1i32;
243-
}
244-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_SPEED as i32 {
245-
params.literal_adaptation[1].0 = value as u16;
246-
if params.literal_adaptation[0] == (0, 0) {
247-
params.literal_adaptation[0].0 = value as u16;
206+
) -> bool {
207+
use crate::enc::parameters::BrotliEncoderParameter::*;
208+
match p {
209+
BROTLI_PARAM_MODE => {
210+
params.mode = match value {
211+
0 => BrotliEncoderMode::BROTLI_MODE_GENERIC,
212+
1 => BrotliEncoderMode::BROTLI_MODE_TEXT,
213+
2 => BrotliEncoderMode::BROTLI_MODE_FONT,
214+
3 => BrotliEncoderMode::BROTLI_FORCE_LSB_PRIOR,
215+
4 => BrotliEncoderMode::BROTLI_FORCE_MSB_PRIOR,
216+
5 => BrotliEncoderMode::BROTLI_FORCE_UTF8_PRIOR,
217+
6 => BrotliEncoderMode::BROTLI_FORCE_SIGNED_PRIOR,
218+
_ => BrotliEncoderMode::BROTLI_MODE_GENERIC,
219+
};
248220
}
249-
return 1i32;
250-
}
251-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_SPEED_MAX as i32 {
252-
params.literal_adaptation[1].1 = value as u16;
253-
if params.literal_adaptation[0].1 == 0 {
254-
params.literal_adaptation[0].1 = value as u16;
221+
BROTLI_PARAM_QUALITY => params.quality = value as i32,
222+
BROTLI_PARAM_STRIDE_DETECTION_QUALITY => params.stride_detection_quality = value as u8,
223+
BROTLI_PARAM_HIGH_ENTROPY_DETECTION_QUALITY => {
224+
params.high_entropy_detection_quality = value as u8
225+
}
226+
BROTLI_PARAM_CDF_ADAPTATION_DETECTION => params.cdf_adaptation_detection = value as u8,
227+
BROTLI_PARAM_Q9_5 => params.q9_5 = (value != 0),
228+
BROTLI_PARAM_PRIOR_BITMASK_DETECTION => params.prior_bitmask_detection = value as u8,
229+
BROTLI_PARAM_SPEED => {
230+
params.literal_adaptation[1].0 = value as u16;
231+
if params.literal_adaptation[0] == (0, 0) {
232+
params.literal_adaptation[0].0 = value as u16;
233+
}
255234
}
256-
return 1i32;
257-
}
258-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_CM_SPEED as i32 {
259-
params.literal_adaptation[3].0 = value as u16;
260-
if params.literal_adaptation[2] == (0, 0) {
261-
params.literal_adaptation[2].0 = value as u16;
235+
BROTLI_PARAM_SPEED_MAX => {
236+
params.literal_adaptation[1].1 = value as u16;
237+
if params.literal_adaptation[0].1 == 0 {
238+
params.literal_adaptation[0].1 = value as u16;
239+
}
262240
}
263-
return 1i32;
264-
}
265-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_CM_SPEED_MAX as i32 {
266-
params.literal_adaptation[3].1 = value as u16;
267-
if params.literal_adaptation[2].1 == 0 {
268-
params.literal_adaptation[2].1 = value as u16;
241+
BROTLI_PARAM_CM_SPEED => {
242+
params.literal_adaptation[3].0 = value as u16;
243+
if params.literal_adaptation[2] == (0, 0) {
244+
params.literal_adaptation[2].0 = value as u16;
245+
}
269246
}
270-
return 1i32;
271-
}
272-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_SPEED_LOW as i32 {
273-
params.literal_adaptation[0].0 = value as u16;
274-
return 1i32;
275-
}
276-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_SPEED_LOW_MAX as i32 {
277-
params.literal_adaptation[0].1 = value as u16;
278-
return 1i32;
279-
}
280-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_CM_SPEED_LOW as i32 {
281-
params.literal_adaptation[2].0 = value as u16;
282-
return 1i32;
283-
}
284-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_CM_SPEED_LOW_MAX as i32 {
285-
params.literal_adaptation[2].1 = value as u16;
286-
return 1i32;
287-
}
288-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_LITERAL_BYTE_SCORE as i32 {
289-
params.hasher.literal_byte_score = value as i32;
290-
return 1i32;
291-
}
292-
if p as i32 == BrotliEncoderParameter::BROTLI_METABLOCK_CALLBACK as i32 {
293-
params.log_meta_block = value != 0;
294-
return 1i32;
295-
}
296-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_LGWIN as i32 {
297-
params.lgwin = value as i32;
298-
return 1i32;
299-
}
300-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_LGBLOCK as i32 {
301-
params.lgblock = value as i32;
302-
return 1i32;
303-
}
304-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING as i32 {
305-
if value != 0u32 && (value != 1u32) {
306-
return 0i32;
247+
BROTLI_PARAM_CM_SPEED_MAX => {
248+
params.literal_adaptation[3].1 = value as u16;
249+
if params.literal_adaptation[2].1 == 0 {
250+
params.literal_adaptation[2].1 = value as u16;
251+
}
307252
}
308-
params.disable_literal_context_modeling = if value != 0 { 1i32 } else { 0i32 };
309-
return 1i32;
310-
}
311-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_SIZE_HINT as i32 {
312-
params.size_hint = value as usize;
313-
return 1i32;
314-
}
315-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_LARGE_WINDOW as i32 {
316-
params.large_window = value != 0;
317-
return 1i32;
318-
}
319-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_AVOID_DISTANCE_PREFIX_SEARCH as i32 {
320-
params.avoid_distance_prefix_search = value != 0;
321-
return 1i32;
322-
}
323-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_CATABLE as i32 {
324-
params.catable = value != 0;
325-
if !params.appendable {
326-
params.appendable = value != 0;
253+
BROTLI_PARAM_SPEED_LOW => params.literal_adaptation[0].0 = value as u16,
254+
BROTLI_PARAM_SPEED_LOW_MAX => params.literal_adaptation[0].1 = value as u16,
255+
BROTLI_PARAM_CM_SPEED_LOW => params.literal_adaptation[2].0 = value as u16,
256+
BROTLI_PARAM_CM_SPEED_LOW_MAX => params.literal_adaptation[2].1 = value as u16,
257+
BROTLI_PARAM_LITERAL_BYTE_SCORE => params.hasher.literal_byte_score = value as i32,
258+
BROTLI_METABLOCK_CALLBACK => params.log_meta_block = value != 0,
259+
BROTLI_PARAM_LGWIN => params.lgwin = value as i32,
260+
BROTLI_PARAM_LGBLOCK => params.lgblock = value as i32,
261+
BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING => {
262+
if value != 0 && value != 1 {
263+
return false;
264+
}
265+
params.disable_literal_context_modeling = if value != 0 { 1 } else { 0 };
327266
}
328-
params.use_dictionary = (value == 0);
329-
return 1i32;
330-
}
331-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_APPENDABLE as i32 {
332-
params.appendable = value != 0;
333-
return 1i32;
334-
}
335-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_MAGIC_NUMBER as i32 {
336-
params.magic_number = value != 0;
337-
return 1i32;
338-
}
339-
if p as i32 == BrotliEncoderParameter::BROTLI_PARAM_FAVOR_EFFICIENCY as i32 {
340-
params.favor_cpu_efficiency = value != 0;
341-
return 1i32;
267+
BROTLI_PARAM_SIZE_HINT => params.size_hint = value as usize,
268+
BROTLI_PARAM_LARGE_WINDOW => params.large_window = value != 0,
269+
BROTLI_PARAM_AVOID_DISTANCE_PREFIX_SEARCH => {
270+
params.avoid_distance_prefix_search = value != 0
271+
}
272+
BROTLI_PARAM_CATABLE => {
273+
params.catable = value != 0;
274+
if !params.appendable {
275+
params.appendable = value != 0;
276+
}
277+
params.use_dictionary = (value == 0);
278+
}
279+
BROTLI_PARAM_APPENDABLE => params.appendable = value != 0,
280+
BROTLI_PARAM_MAGIC_NUMBER => params.magic_number = value != 0,
281+
BROTLI_PARAM_FAVOR_EFFICIENCY => params.favor_cpu_efficiency = value != 0,
282+
_ => return false,
342283
}
343-
0i32
284+
true
344285
}
345286

346287
impl<Alloc: BrotliAlloc> BrotliEncoderStateStruct<Alloc> {
347-
pub fn set_parameter(&mut self, p: BrotliEncoderParameter, value: u32) -> i32 {
288+
pub fn set_parameter(&mut self, p: BrotliEncoderParameter, value: u32) -> bool {
348289
if self.is_initialized_ {
349-
return 0i32;
290+
false
291+
} else {
292+
set_parameter(&mut self.params, p, value)
350293
}
351-
set_parameter(&mut self.params, p, value)
352294
}
353295
}
354296

src/ffi/compressor.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ pub unsafe extern "C" fn BrotliEncoderSetParameter(
111111
param: ::enc::encode::BrotliEncoderParameter,
112112
value: u32,
113113
) -> i32 {
114-
(*state_ptr).compressor.set_parameter(param, value)
114+
if (*state_ptr).compressor.set_parameter(param, value) {
115+
1
116+
} else {
117+
0
118+
}
115119
}
116120

117121
#[no_mangle]

src/ffi/multicompress/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub unsafe extern "C" fn BrotliEncoderCompressMulti(
143143
};
144144
let mut params = BrotliEncoderParams::default();
145145
for (k, v) in param_keys_slice.iter().zip(param_values_slice.iter()) {
146-
if set_parameter(&mut params, *k, *v) == 0 {
146+
if !set_parameter(&mut params, *k, *v) {
147147
return 0;
148148
}
149149
}
@@ -357,7 +357,7 @@ pub unsafe extern "C" fn BrotliEncoderCompressWorkPool(
357357
let param_values_slice = slice_from_raw_parts_or_nil(param_values, num_params);
358358
let mut params = BrotliEncoderParams::default();
359359
for (k, v) in param_keys_slice.iter().zip(param_values_slice.iter()) {
360-
if set_parameter(&mut params, *k, *v) == 0 {
360+
if !set_parameter(&mut params, *k, *v) {
361361
return 0;
362362
}
363363
}

0 commit comments

Comments
 (0)