Merge pull request #77 from cosmicexplorer/oldpr395

refactor: use num_enum to clean up the System type
This commit is contained in:
Chris Hennick 2024-05-02 20:48:04 +00:00 committed by GitHub
commit c8655d9eda
Signed by: DevComp
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 19 deletions

View file

@ -31,6 +31,7 @@ crc32fast = "1.4.0"
displaydoc = { version = "0.2.4", default-features = false } displaydoc = { version = "0.2.4", default-features = false }
flate2 = { version = "1.0.28", default-features = false, optional = true } flate2 = { version = "1.0.28", default-features = false, optional = true }
hmac = { version = "0.12.1", optional = true, features = ["reset"] } hmac = { version = "0.12.1", optional = true, features = ["reset"] }
num_enum = "0.6.1"
pbkdf2 = { version = "0.12.2", optional = true } pbkdf2 = { version = "0.12.2", optional = true }
sha1 = { version = "0.10.6", optional = true } sha1 = { version = "0.10.6", optional = true }
thiserror = "1.0.48" thiserror = "1.0.48"

View file

@ -813,7 +813,7 @@ fn central_header_to_zip_file_inner<R: Read>(
// Construct the result // Construct the result
let mut result = ZipFileData { 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, version_made_by: version_made_by as u8,
encrypted, encrypted,
using_data_descriptor, using_data_descriptor,
@ -1197,7 +1197,7 @@ pub fn read_zipfile_from_stream<'a, R: Read>(reader: &'a mut R) -> ZipResult<Opt
}; };
let mut result = ZipFileData { 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, version_made_by: version_made_by as u8,
encrypted, encrypted,
using_data_descriptor, using_data_descriptor,

View file

@ -1,4 +1,5 @@
//! Types that specify what is contained in a ZIP. //! Types that specify what is contained in a ZIP.
use num_enum::{FromPrimitive, IntoPrimitive};
use path::{Component, Path, PathBuf}; use path::{Component, Path, PathBuf};
use std::path; use std::path;
use std::sync::{Arc, OnceLock}; use std::sync::{Arc, OnceLock};
@ -50,25 +51,15 @@ use crate::result::DateTimeRangeError;
#[cfg(feature = "time")] #[cfg(feature = "time")]
use time::{error::ComponentRange, Date, Month, OffsetDateTime, PrimitiveDateTime, 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 { pub enum System {
Dos = 0, Dos = 0,
Unix = 3, Unix = 3,
#[num_enum(default)]
Unknown, 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. /// Representation of a moment in time.
/// ///
/// Zip files use an old format from DOS to store timestamps, /// Zip files use an old format from DOS to store timestamps,
@ -516,10 +507,14 @@ mod test {
#[test] #[test]
fn system() { fn system() {
use super::System; use super::System;
assert_eq!(System::Dos as u16, 0u16); assert_eq!(u8::from(System::Dos), 0u8);
assert_eq!(System::Unix as u16, 3u16); assert_eq!(System::Dos as u8, 0u8);
assert_eq!(System::from_u8(0), System::Dos); assert_eq!(System::Unix as u8, 3u8);
assert_eq!(System::from_u8(3), System::Unix); 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] #[test]