use crate::fs::FileType;
use rustix::fs::RawMode;
use std::{fs, io};
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub(crate) enum ImplFileTypeExt {
Symlink,
BlockDevice,
CharDevice,
Fifo,
Socket,
}
impl ImplFileTypeExt {
#[inline]
#[allow(clippy::unnecessary_wraps)]
pub(crate) fn from(_file: &fs::File, metadata: &fs::Metadata) -> io::Result<FileType> {
Ok(Self::from_just_metadata(metadata))
}
#[inline]
pub(crate) fn from_just_metadata(metadata: &fs::Metadata) -> FileType {
let std = metadata.file_type();
Self::from_std(std)
}
#[inline]
pub(crate) fn from_std(std: fs::FileType) -> FileType {
use rustix::fs::FileTypeExt;
if std.is_file() {
FileType::file()
} else if std.is_dir() {
FileType::dir()
} else if std.is_symlink() {
FileType::ext(Self::Symlink)
} else if std.is_block_device() {
FileType::ext(Self::BlockDevice)
} else if std.is_char_device() {
FileType::ext(Self::CharDevice)
} else {
#[cfg(not(target_os = "wasi"))]
if std.is_fifo() {
return FileType::ext(Self::Fifo);
}
#[cfg(not(target_os = "wasi"))]
if std.is_socket() {
return FileType::ext(Self::Socket);
}
FileType::unknown()
}
}
#[inline]
pub(crate) const fn from_raw_mode(mode: RawMode) -> FileType {
match rustix::fs::FileType::from_raw_mode(mode) {
rustix::fs::FileType::RegularFile => FileType::file(),
rustix::fs::FileType::Directory => FileType::dir(),
rustix::fs::FileType::Symlink => FileType::ext(Self::symlink()),
#[cfg(not(target_os = "wasi"))]
rustix::fs::FileType::Fifo => FileType::ext(Self::fifo()),
rustix::fs::FileType::CharacterDevice => FileType::ext(Self::char_device()),
rustix::fs::FileType::BlockDevice => FileType::ext(Self::block_device()),
#[cfg(not(target_os = "wasi"))]
rustix::fs::FileType::Socket => FileType::ext(Self::socket()),
_ => FileType::unknown(),
}
}
#[inline]
pub(crate) const fn symlink() -> Self {
Self::Symlink
}
#[inline]
pub(crate) const fn block_device() -> Self {
Self::BlockDevice
}
#[inline]
pub(crate) const fn char_device() -> Self {
Self::CharDevice
}
#[inline]
pub(crate) const fn fifo() -> Self {
Self::Fifo
}
#[inline]
pub(crate) const fn socket() -> Self {
Self::Socket
}
#[inline]
pub(crate) fn is_symlink(&self) -> bool {
*self == Self::Symlink
}
}