use std::{
ffi::OsStr,
io::Error,
path::{Path, PathBuf},
};
use walkdir::DirEntryExt;
use crate::metadata::{FileType, Metadata};
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct DirEntry
{
pub path: PathBuf,
pub metadata: Metadata,
pub follow_link: bool,
pub depth: usize,
pub ino: u64,
}
impl DirEntry
{
#[must_use]
pub fn path(&self) -> &Path
{
&self.path
}
#[allow(clippy::missing_const_for_fn)]
#[must_use]
pub fn into_path(self) -> PathBuf
{
self.path
}
#[must_use]
pub fn path_is_symlink(&self) -> bool
{
self.metadata.file_type.is_symlink() || self.follow_link
}
#[must_use]
pub const fn metadata(&self) -> Metadata
{
self.metadata
}
#[must_use]
pub const fn file_type(&self) -> FileType
{
self.metadata.file_type
}
#[must_use]
pub fn file_name(&self) -> &OsStr
{
self.path.file_name().unwrap_or(self.path.as_os_str())
}
#[must_use]
pub const fn depth(&self) -> usize
{
self.depth
}
}
pub(crate) struct WorkaroundResult<T, E>
{
pub inner: Result<T, E>,
}
impl From<::walkdir::DirEntry> for WorkaroundResult<DirEntry, Error>
{
fn from(walkdir: ::walkdir::DirEntry) -> Self
{
let metadata = match walkdir.metadata()
{
Ok(metadata) => metadata.into(),
Err(err) =>
{
return Self {
inner: Err(err.into()),
}
}
};
Ok(DirEntry {
path: walkdir.path().to_owned(),
metadata,
follow_link: walkdir.path_is_symlink(),
depth: walkdir.depth(),
ino: walkdir.ino(),
})
.into()
}
}
impl<T, E> From<Result<T, E>> for WorkaroundResult<T, E>
{
fn from(inner: Result<T, E>) -> Self
{
Self { inner }
}
}
impl<T, E> From<WorkaroundResult<T, E>> for Result<T, E>
{
fn from(val: WorkaroundResult<T, E>) -> Self
{
val.inner
}
}
impl<T, E> WorkaroundResult<T, E>
{
#[allow(clippy::missing_const_for_fn)]
pub fn deworkaround(self) -> Result<T, E>
{
self.inner
}
}