use num_enum to clean up the System type

This commit is contained in:
Danny McClanahan 2023-07-12 00:01:23 -04:00
parent 01a8ff41b1
commit 3cfbdfca83
No known key found for this signature in database
GPG key ID: 6105C10F1A199CC7
3 changed files with 15 additions and 19 deletions

View file

@ -30,6 +30,7 @@ constant_time_eq = { version = "0.3.0", optional = true }
crc32fast = "1.4.0"
flate2 = { version = "1.0.28", default-features = false, optional = true }
hmac = { version = "0.12.1", optional = true, features = ["reset"] }
num_enum = "0.6.1"
pbkdf2 = { version = "0.12.2", optional = true }
sha1 = { version = "0.10.6", optional = true }
time = { workspace = true, optional = true, features = [

View file

@ -812,7 +812,7 @@ fn central_header_to_zip_file_inner<R: Read>(
// Construct the result
let mut result = ZipFileData {
system: System::from_u8((version_made_by >> 8) as u8),
system: System::from((version_made_by >> 8) as u8),
version_made_by: version_made_by as u8,
encrypted,
using_data_descriptor,
@ -1178,7 +1178,7 @@ pub fn read_zipfile_from_stream<'a, R: Read>(reader: &'a mut R) -> ZipResult<Opt
};
let mut result = ZipFileData {
system: System::from_u8((version_made_by >> 8) as u8),
system: System::from((version_made_by >> 8) as u8),
version_made_by: version_made_by as u8,
encrypted,
using_data_descriptor,

View file

@ -1,5 +1,6 @@
//! Types that specify what is contained in a ZIP.
use path::{Component, Path, PathBuf};
use num_enum::{FromPrimitive, IntoPrimitive};
use std::path;
use std::sync::{Arc, OnceLock};
@ -49,25 +50,15 @@ use crate::result::DateTimeRangeError;
#[cfg(feature = "time")]
use time::{error::ComponentRange, Date, Month, OffsetDateTime, PrimitiveDateTime, Time};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum System {
Dos = 0,
Unix = 3,
#[num_enum(default)]
Unknown,
}
impl System {
pub const fn from_u8(system: u8) -> System {
use self::System::*;
match system {
0 => Dos,
3 => Unix,
_ => Unknown,
}
}
}
/// Representation of a moment in time.
///
/// Zip files use an old format from DOS to store timestamps,
@ -512,10 +503,14 @@ mod test {
#[test]
fn system() {
use super::System;
assert_eq!(System::Dos as u16, 0u16);
assert_eq!(System::Unix as u16, 3u16);
assert_eq!(System::from_u8(0), System::Dos);
assert_eq!(System::from_u8(3), System::Unix);
assert_eq!(u8::from(System::Dos), 0u8);
assert_eq!(System::Dos as u8, 0u8);
assert_eq!(System::Unix as u8, 3u8);
assert_eq!(u8::from(System::Unix), 3u8);
assert_eq!(System::from(0), System::Dos);
assert_eq!(System::from(3), System::Unix);
assert_eq!(u8::from(System::Unknown), 4u8);
assert_eq!(System::Unknown as u8, 4u8);
}
#[test]