From c7df8157a9ba7f7837cfa290d5ab8dacdde5fff1 Mon Sep 17 00:00:00 2001 From: Mathijs van de Nes Date: Mon, 16 Mar 2015 10:34:48 +0100 Subject: [PATCH] Removed unsupported compression methods from enum - Simplified enum to only show supported values - Removed use of FromPrimitive --- Cargo.toml | 4 +-- src/compression.rs | 73 ++++++++++++++++++++++++++++------------------ src/read.rs | 3 +- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4fadc85a..b986b8ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "zip" -version = "0.1.0" +version = "0.1.1" authors = ["Mathijs van de Nes "] license = "MIT" repository = "https://github.com/mvdnes/zip-rs.git" @@ -12,5 +12,5 @@ Library to support the reading and writing of zip files. [dependencies] flate2 = "^0.2" bzip2 = "^0.2" -time = "*" +time = "^0.1" podio = "^0.0" diff --git a/src/compression.rs b/src/compression.rs index df7cc644..17fd6750 100644 --- a/src/compression.rs +++ b/src/compression.rs @@ -1,41 +1,56 @@ //! Possible ZIP compression methods. /// Compression methods for the contents of a ZIP file. -#[derive(FromPrimitive, Clone, Copy)] +#[derive(Clone, Copy)] pub enum CompressionMethod { /// The file is stored (no compression) Stored = 0, - /// The file is Shrunk - Shrunk = 1, - /// The file is Reduced with compression factor 1 - Reduced1 = 2, - /// The file is Reduced with compression factor 2 - Reduced2 = 3, - /// The file is Reduced with compression factor 3 - Reduced3 = 4, - /// The file is Reduced with compression factor 4 - Reduced4 = 5, - /// The file is Imploded - Imploded = 6, /// The file is Deflated Deflated = 8, - /// Enhanced Deflating using Deflate64(tm) - Deflate64 = 9, - /// PKWARE Data Compression Library Imploding (old IBM TERSE) - PkwareImploding = 10, /// File is compressed using BZIP2 algorithm Bzip2 = 12, - /// LZMA (EFS) - LZMA = 14, - /// File is compressed using IBM TERSE (new) - IBMTerse = 18, - /// IBM LZ77 z Architecture (PFS) - LZ77 = 19, - /// WavPack compressed data - WavPack = 97, - /// PPMd version I, Rev 1 - PPMdI1 = 98, - /// Unknown (invalid) compression - Unknown = 10000, + /// Unsupported compression method + Unsupported = ::std::u16::MAX as isize, +} + +impl CompressionMethod { + /// Converts an u16 to its corresponding CompressionMethod + pub fn from_u16(val: u16) -> CompressionMethod { + match val { + 0 => CompressionMethod::Stored, + 8 => CompressionMethod::Deflated, + 12 => CompressionMethod::Bzip2, + _ => CompressionMethod::Unsupported, + } + } +} + +#[cfg(test)] +mod test { + use super::CompressionMethod; + + #[test] + fn from_u16() { + for v in (0..::std::u16::MAX as u32 + 1) + { + let method = CompressionMethod::from_u16(v as u16); + match method { + CompressionMethod::Unsupported => {}, + supported => assert_eq!(v, supported as u32), + } + } + } + + #[test] + fn to_u16() { + fn check_match(method: CompressionMethod) { + assert!(method as u32 == CompressionMethod::from_u16(method as u16) as u32); + } + + check_match(CompressionMethod::Stored); + check_match(CompressionMethod::Deflated); + check_match(CompressionMethod::Bzip2); + check_match(CompressionMethod::Unsupported); + } } diff --git a/src/read.rs b/src/read.rs index 9fa476cb..43d88dd0 100644 --- a/src/read.rs +++ b/src/read.rs @@ -7,7 +7,6 @@ use result::{ZipResult, ZipError}; use std::io; use std::io::prelude::*; use std::collections::HashMap; -use std::num::FromPrimitive; use flate2; use flate2::FlateReadExt; use bzip2::reader::BzDecompressor; @@ -231,7 +230,7 @@ fn central_header_to_zip_file(reader: &mut R) -> ZipResult