[cpio-decoder] Use TryFromInt for enum type
This commit is contained in:
parent
4c4366ccb0
commit
f301c70708
|
|
@ -159,6 +159,9 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "cpio-decoder"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"int-to-c-enum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "critical-section"
|
||||
|
|
|
|||
|
|
@ -5,4 +5,5 @@ edition = "2021"
|
|||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
[dependencies]
|
||||
int-to-c-enum = { path = "../../libs/int-to-c-enum" }
|
||||
|
|
@ -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<Self> {
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in New Issue