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 /// The file is Deflated 12 #[cfg(feature = "flate2")] 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 = "flate2")] 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 = "flate2")] 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 68 #[cfg(all(not(feature = "bzip2"), feature = "flate2"))] methods() -> Vec<CompressionMethod>69 fn methods() -> Vec<CompressionMethod> { 70 vec![CompressionMethod::Stored, CompressionMethod::Deflated] 71 } 72 73 #[cfg(all(not(feature = "flate2"), feature = "bzip2"))] methods() -> Vec<CompressionMethod>74 fn methods() -> Vec<CompressionMethod> { 75 vec![CompressionMethod::Stored, CompressionMethod::Bzip2] 76 } 77 78 #[cfg(all(feature = "bzip2", feature = "flate2"))] methods() -> Vec<CompressionMethod>79 fn methods() -> Vec<CompressionMethod> { 80 vec![CompressionMethod::Stored, CompressionMethod::Deflated, CompressionMethod::Bzip2] 81 } 82 83 #[cfg(all(not(feature = "bzip2"), not(feature = "flate2")))] methods() -> Vec<CompressionMethod>84 fn methods() -> Vec<CompressionMethod> { 85 vec![CompressionMethod::Stored] 86 } 87 88 #[test] to_eq_from()89 fn to_eq_from() { 90 fn check_match(method: CompressionMethod) { 91 let to = method.to_u16(); 92 let from = CompressionMethod::from_u16(to); 93 let back = from.to_u16(); 94 assert_eq!(to, back); 95 } 96 97 for method in methods() { 98 check_match(method); 99 } 100 } 101 102 #[test] to_display_fmt()103 fn to_display_fmt() { 104 fn check_match(method: CompressionMethod) { 105 let debug_str = format!("{:?}", method); 106 let display_str = format!("{}", method); 107 assert_eq!(debug_str, display_str); 108 } 109 110 for method in methods() { 111 check_match(method); 112 } 113 } 114 } 115