From 1e935672bdab306ff54f5fb99d94c444c856d2f5 Mon Sep 17 00:00:00 2001 From: Alexander Koval Date: Sat, 23 Apr 2016 13:12:28 +0300 Subject: [PATCH] Replace system attribute by enum --- src/read.rs | 10 +++++----- src/types.rs | 25 ++++++++++++++++++++++--- src/write.rs | 4 ++-- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/read.rs b/src/read.rs index 6a30bceb..3ff2881f 100644 --- a/src/read.rs +++ b/src/read.rs @@ -10,7 +10,7 @@ use std::collections::HashMap; use flate2; use flate2::FlateReadExt; use podio::{ReadPodExt, LittleEndian}; -use types::{ZipFileData, SYSTEM_MSDOS, SYSTEM_UNIX}; +use types::{ZipFileData, System}; use cp437::FromCp437; use msdos_time::{TmMsDosExt, MsDosDateTime}; @@ -239,7 +239,7 @@ fn central_header_to_zip_file(reader: &mut R) -> ZipResult> 8) as u8, + system: System::from_u8((version_made_by >> 8) as u8), version: version_made_by as u8, encrypted: encrypted, compression_method: CompressionMethod::from_u16(compression_method), @@ -289,7 +289,7 @@ impl<'a> ZipFile<'a> { } } /// Get compatibility of the file attribute information - pub fn system(&self) -> u8 { + pub fn system(&self) -> System { self.data.system } /// Get the version of the file @@ -323,10 +323,10 @@ impl<'a> ZipFile<'a> { /// Get mode for the file pub fn unix_mode(&self) -> Option { match self.data.system { - s if s == SYSTEM_UNIX => { + System::Unix => { Some(self.data.external_attributes >> 16) }, - s if s == SYSTEM_MSDOS => { + System::Dos => { // Interpret MSDOS directory bit let mut mode = if 0x10 == (self.data.external_attributes & 0x10) { ffi::S_IFDIR | 0o0775 diff --git a/src/types.rs b/src/types.rs index 6677637b..43aa1008 100644 --- a/src/types.rs +++ b/src/types.rs @@ -2,8 +2,27 @@ use time; -pub const SYSTEM_MSDOS: u8 = 0; -pub const SYSTEM_UNIX: u8 = 3; + +#[derive(Clone, Copy)] +pub enum System +{ + Dos, + Unix, + Unknown, +} + +impl System { + pub fn from_u8(system: u8) -> System + { + use self::System::*; + + match system { + 0 => Dos, + 3 => Unix, + _ => Unknown, + } + } +} pub const DEFAULT_VERSION: u8 = 20; @@ -11,7 +30,7 @@ pub const DEFAULT_VERSION: u8 = 20; pub struct ZipFileData { /// Compatibility of the file attribute information - pub system: u8, + pub system: System, /// Specification version pub version: u8, /// True if the file is encrypted. diff --git a/src/write.rs b/src/write.rs index 5ab6d443..6db41261 100644 --- a/src/write.rs +++ b/src/write.rs @@ -1,7 +1,7 @@ //! Structs for creating a new zip archive use compression::CompressionMethod; -use types::{ZipFileData, SYSTEM_MSDOS, DEFAULT_VERSION}; +use types::{ZipFileData, System, DEFAULT_VERSION}; use spec; use crc32; use result::{ZipResult, ZipError}; @@ -134,7 +134,7 @@ impl ZipWriter let mut file = ZipFileData { - system: SYSTEM_MSDOS, + system: System::Dos, version: DEFAULT_VERSION, encrypted: false, compression_method: compression,