1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use std::io::{Read, Write};
use flate2::Compression as GzCompression;
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use serde::{
Deserialize,
Serialize,
};
use super::{
Compression,
};
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GzipCompression {
#[serde(default = "default_gzip_level")]
level: i32,
}
impl GzipCompression {
fn get_effective_level(&self) -> GzCompression {
if self.level < 0 || self.level > 9 {
GzCompression::default()
} else {
GzCompression::new(self.level as u32)
}
}
}
fn default_gzip_level() -> i32 {-1}
impl Default for GzipCompression {
fn default() -> GzipCompression {
GzipCompression {
level: default_gzip_level(),
}
}
}
impl Compression for GzipCompression {
fn decoder<'a, R: Read + 'a>(&self, r: R) -> Box<dyn Read + 'a> {
Box::new(GzDecoder::new(r))
}
fn encoder<'a, W: Write + 'a>(&self, w: W) -> Box<dyn Write + 'a> {
Box::new(GzEncoder::new(w, self.get_effective_level()))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::compression::CompressionType;
const TEST_BLOCK_I16_GZIP: [u8; 48] = [
0x00, 0x00,
0x00, 0x03,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x03,
0x1f, 0x8b, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x63, 0x60,
0x64, 0x60, 0x62, 0x60,
0x66, 0x60, 0x61, 0x60,
0x65, 0x60, 0x03, 0x00,
0xaa, 0xea, 0x6d, 0xbf,
0x0c, 0x00, 0x00, 0x00,
];
#[test]
fn test_read_doc_spec_block() {
crate::tests::test_read_doc_spec_block(
TEST_BLOCK_I16_GZIP.as_ref(),
CompressionType::Gzip(GzipCompression::default()));
}
#[test]
fn test_write_doc_spec_block() {
let mut fudge_test_block = TEST_BLOCK_I16_GZIP.clone();
fudge_test_block[25] = 255;
crate::tests::test_write_doc_spec_block(
&fudge_test_block,
CompressionType::Gzip(GzipCompression::default()));
}
#[test]
fn test_rw() {
crate::tests::test_block_compression_rw(CompressionType::Gzip(GzipCompression::default()));
}
}