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
use std::fmt;
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum CompressionMethod {
Stored,
#[cfg(feature = "deflate")]
Deflated,
#[cfg(feature = "bzip2")]
Bzip2,
Unsupported(u16),
}
impl CompressionMethod {
pub fn from_u16(val: u16) -> CompressionMethod {
match val {
0 => CompressionMethod::Stored,
#[cfg(feature = "deflate")]
8 => CompressionMethod::Deflated,
#[cfg(feature = "bzip2")]
12 => CompressionMethod::Bzip2,
v => CompressionMethod::Unsupported(v),
}
}
pub fn to_u16(self) -> u16 {
match self {
CompressionMethod::Stored => 0,
#[cfg(feature = "deflate")]
CompressionMethod::Deflated => 8,
#[cfg(feature = "bzip2")]
CompressionMethod::Bzip2 => 12,
CompressionMethod::Unsupported(v) => v,
}
}
}
impl fmt::Display for CompressionMethod {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
#[cfg(test)]
mod test {
use super::CompressionMethod;
#[test]
fn from_eq_to() {
for v in 0..(::std::u16::MAX as u32 + 1) {
let from = CompressionMethod::from_u16(v as u16);
let to = from.to_u16() as u32;
assert_eq!(v, to);
}
}
fn methods() -> Vec<CompressionMethod> {
let mut methods = Vec::new();
methods.push(CompressionMethod::Stored);
#[cfg(feature = "deflate")]
methods.push(CompressionMethod::Deflated);
#[cfg(feature = "bzip2")]
methods.push(CompressionMethod::Bzip2);
methods
}
#[test]
fn to_eq_from() {
fn check_match(method: CompressionMethod) {
let to = method.to_u16();
let from = CompressionMethod::from_u16(to);
let back = from.to_u16();
assert_eq!(to, back);
}
for method in methods() {
check_match(method);
}
}
#[test]
fn to_display_fmt() {
fn check_match(method: CompressionMethod) {
let debug_str = format!("{:?}", method);
let display_str = format!("{}", method);
assert_eq!(debug_str, display_str);
}
for method in methods() {
check_match(method);
}
}
}