use std::ffi::OsStr;
use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LoadedEntry {
pub path: PathBuf,
pub is_dir: bool,
pub is_hidden: bool,
}
impl LoadedEntry {
pub fn file_name(&self) -> &OsStr {
self.path.file_name().unwrap_or(OsStr::new(""))
}
}
impl From<&swdir::DirEntry> for LoadedEntry {
fn from(entry: &swdir::DirEntry) -> Self {
Self {
path: entry.path().to_path_buf(),
is_dir: entry.is_dir(),
is_hidden: detect_hidden(entry),
}
}
}
fn detect_hidden(entry: &swdir::DirEntry) -> bool {
let dotfile = is_dotfile(entry.display_name());
#[cfg(windows)]
{
const FILE_ATTRIBUTE_HIDDEN: u32 = 0x2;
use std::os::windows::fs::MetadataExt;
let attr_hidden = entry
.metadata()
.map(|m| m.file_attributes() & FILE_ATTRIBUTE_HIDDEN != 0)
.unwrap_or(false);
attr_hidden || dotfile
}
#[cfg(not(windows))]
{
dotfile
}
}
pub(crate) fn is_dotfile(name: &OsStr) -> bool {
name.as_encoded_bytes().first() == Some(&b'.')
}