1 //! Possible ZIP compression methods.
2 
3 use std::fmt;
4 
5 /// Compression methods for the contents of a ZIP file.
6 #[derive(Copy, Clone, PartialEq, Debug)]
7 pub enum CompressionMethod
8 {
9     /// The file is stored (no compression)
10     Stored,
11     /// Deflate in pure rust
12     #[cfg(feature = "deflate")]
13     Deflated,
14     /// File is compressed using BZIP2 algorithm
15     #[cfg(feature = "bzip2")]
16     Bzip2,
17     /// Unsupported compression method
18     Unsupported(u16),
19 }
20 
21 impl CompressionMethod {
22     /// Converts an u16 to its corresponding CompressionMethod
from_u16(val: u16) -> CompressionMethod23     pub fn from_u16(val: u16) -> CompressionMethod {
24         match val {
25             0 => CompressionMethod::Stored,
26             #[cfg(feature = "deflate")]
27             8 => CompressionMethod::Deflated,
28             #[cfg(feature = "bzip2")]
29             12 => CompressionMethod::Bzip2,
30             v => CompressionMethod::Unsupported(v),
31         }
32     }
33 
34     /// Converts a CompressionMethod to a u16
to_u16(self) -> u1635     pub fn to_u16(self) -> u16 {
36         match self {
37             CompressionMethod::Stored => 0,
38             #[cfg(feature = "deflate")]
39             CompressionMethod::Deflated => 8,
40             #[cfg(feature = "bzip2")]
41             CompressionMethod::Bzip2 => 12,
42             CompressionMethod::Unsupported(v) => v,
43         }
44     }
45 }
46 
47 impl fmt::Display for CompressionMethod {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result48     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49         // Just duplicate what the Debug format looks like, i.e, the enum key:
50         write!(f, "{:?}", self)
51     }
52 }
53 
54 #[cfg(test)]
55 mod test {
56     use super::CompressionMethod;
57 
58     #[test]
from_eq_to()59     fn from_eq_to() {
60         for v in 0..(::std::u16::MAX as u32 + 1)
61         {
62             let from = CompressionMethod::from_u16(v as u16);
63             let to = from.to_u16() as u32;
64             assert_eq!(v, to);
65         }
66     }
67 
methods() -> Vec<CompressionMethod>68     fn methods() -> Vec<CompressionMethod> {
69         let mut methods = Vec::new();
70         methods.push(CompressionMethod::Stored);
71         #[cfg(feature="deflate")] methods.push(CompressionMethod::Deflated);
72         #[cfg(feature="bzip2")] methods.push(CompressionMethod::Bzip2);
73         methods
74     }
75 
76 
77     #[test]
to_eq_from()78     fn to_eq_from() {
79         fn check_match(method: CompressionMethod) {
80             let to = method.to_u16();
81             let from = CompressionMethod::from_u16(to);
82             let back = from.to_u16();
83             assert_eq!(to, back);
84         }
85 
86         for method in methods() {
87             check_match(method);
88         }
89     }
90 
91     #[test]
to_display_fmt()92     fn to_display_fmt() {
93         fn check_match(method: CompressionMethod) {
94             let debug_str = format!("{:?}", method);
95             let display_str = format!("{}", method);
96             assert_eq!(debug_str, display_str);
97         }
98 
99         for method in methods() {
100             check_match(method);
101         }
102     }
103 }
104