Removed unsupported compression methods from enum

- Simplified enum to only show supported values
- Removed use of FromPrimitive
This commit is contained in:
Mathijs van de Nes 2015-03-16 10:34:48 +01:00
parent 132530cb41
commit c7df8157a9
3 changed files with 47 additions and 33 deletions

View file

@ -1,7 +1,7 @@
[package]
name = "zip"
version = "0.1.0"
version = "0.1.1"
authors = ["Mathijs van de Nes <git@mathijs.vd-nes.nl>"]
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"

View file

@ -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);
}
}

View file

@ -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<R: Read+io::Seek>(reader: &mut R) -> ZipResult<Zip
let mut result = ZipFileData
{
encrypted: encrypted,
compression_method: FromPrimitive::from_u16(compression_method).unwrap_or(CompressionMethod::Unknown),
compression_method: CompressionMethod::from_u16(compression_method),
last_modified_time: util::msdos_datetime_to_tm(last_mod_time, last_mod_date),
crc32: crc32,
compressed_size: compressed_size as u64,