diff --git a/Cargo.lock b/Cargo.lock index d6989f932..9a822e0e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -159,6 +159,9 @@ dependencies = [ [[package]] name = "cpio-decoder" version = "0.1.0" +dependencies = [ + "int-to-c-enum", +] [[package]] name = "critical-section" diff --git a/services/libs/cpio-decoder/Cargo.toml b/services/libs/cpio-decoder/Cargo.toml index bf5c1c80c..f8dd8aaa3 100644 --- a/services/libs/cpio-decoder/Cargo.toml +++ b/services/libs/cpio-decoder/Cargo.toml @@ -5,4 +5,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] \ No newline at end of file +[dependencies] +int-to-c-enum = { path = "../../libs/int-to-c-enum" } \ No newline at end of file diff --git a/services/libs/cpio-decoder/src/lib.rs b/services/libs/cpio-decoder/src/lib.rs index ecca3b91b..6cc23b963 100644 --- a/services/libs/cpio-decoder/src/lib.rs +++ b/services/libs/cpio-decoder/src/lib.rs @@ -15,6 +15,7 @@ #![forbid(unsafe_code)] use crate::error::{Error, Result}; +use int_to_c_enum::TryFromInt; pub mod error; @@ -192,7 +193,7 @@ impl FileMetadata { let raw_mode = read_hex_bytes_to_u32(&header.mode)?; let metadata = Self { ino: read_hex_bytes_to_u32(&header.ino)?, - type_: FileType::from_u32(raw_mode)?, + type_: FileType::try_from(raw_mode).map_err(|_| Error::FileTypeError)?, mode: (raw_mode & MODE_MASK) as u16, uid: read_hex_bytes_to_u32(&header.uid)?, gid: read_hex_bytes_to_u32(&header.gid)?, @@ -270,7 +271,7 @@ impl FileMetadata { /// The type of the file. #[repr(u32)] -#[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, TryFromInt)] pub enum FileType { /// FIFO special file FiFo = 0o010000, @@ -288,31 +289,6 @@ pub enum FileType { Socket = 0o140000, } -impl FileType { - pub fn from_u32(bits: u32) -> Result { - const TYPE_MASK: u32 = 0o170000; - let bits = bits & TYPE_MASK; - let type_ = if bits == Self::FiFo as u32 { - Self::FiFo - } else if bits == Self::Char as u32 { - Self::Char - } else if bits == Self::Dir as u32 { - Self::Dir - } else if bits == Self::Block as u32 { - Self::Block - } else if bits == Self::File as u32 { - Self::File - } else if bits == Self::Link as u32 { - Self::Link - } else if bits == Self::Socket as u32 { - Self::Socket - } else { - return Err(Error::FileTypeError); - }; - Ok(type_) - } -} - impl Default for FileType { fn default() -> Self { Self::File