#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Kind {
Absent = b'a',
File = b'f',
Directory = b'd',
Relocated = b'r',
Symlink = b'l',
TreeReference = b't',
}
impl Kind {
#[inline]
pub fn to_minikind(self) -> u8 {
self as u8
}
#[inline]
pub fn from_minikind(byte: u8) -> Result<Self, u8> {
match byte {
b'a' => Ok(Kind::Absent),
b'f' => Ok(Kind::File),
b'd' => Ok(Kind::Directory),
b'r' => Ok(Kind::Relocated),
b'l' => Ok(Kind::Symlink),
b't' => Ok(Kind::TreeReference),
other => Err(other),
}
}
pub fn to_char(self) -> char {
self.to_minikind() as char
}
pub fn as_str(&self) -> &'static str {
match self {
Kind::Absent => "absent",
Kind::File => "file",
Kind::Directory => "directory",
Kind::Relocated => "relocated",
Kind::Symlink => "symlink",
Kind::TreeReference => "tree-reference",
}
}
#[inline]
pub fn is_fdlt(self) -> bool {
matches!(
self,
Kind::File | Kind::Directory | Kind::Symlink | Kind::TreeReference
)
}
#[inline]
pub fn is_fdltr(self) -> bool {
!matches!(self, Kind::Absent)
}
#[inline]
pub fn is_absent_or_relocated(self) -> bool {
matches!(self, Kind::Absent | Kind::Relocated)
}
pub fn to_osutils_kind(self) -> Option<crate::osutils::Kind> {
match self {
Kind::File => Some(crate::osutils::Kind::File),
Kind::Directory => Some(crate::osutils::Kind::Directory),
Kind::Symlink => Some(crate::osutils::Kind::Symlink),
Kind::TreeReference => Some(crate::osutils::Kind::TreeReference),
Kind::Absent | Kind::Relocated => None,
}
}
}
impl From<crate::osutils::Kind> for Kind {
fn from(k: crate::osutils::Kind) -> Self {
match k {
crate::osutils::Kind::File => Kind::File,
crate::osutils::Kind::Directory => Kind::Directory,
crate::osutils::Kind::Symlink => Kind::Symlink,
crate::osutils::Kind::TreeReference => Kind::TreeReference,
}
}
}
impl std::fmt::Display for Kind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[allow(clippy::wrong_self_convention)]
pub trait OptionKindExt {
fn is_not_live(self) -> bool;
fn is_live(self) -> bool;
}
impl OptionKindExt for Option<Kind> {
#[inline]
fn is_not_live(self) -> bool {
match self {
None | Some(Kind::Absent) | Some(Kind::Relocated) => true,
Some(_) => false,
}
}
#[inline]
fn is_live(self) -> bool {
!self.is_not_live()
}
}