use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum Representation {
#[default]
NameOnly,
Structure,
Snippet,
Full,
}
impl Representation {
pub fn upgrade(&self) -> Option<Representation> {
match self {
Representation::NameOnly => Some(Representation::Structure),
Representation::Structure => Some(Representation::Snippet),
Representation::Snippet => Some(Representation::Full),
Representation::Full => None,
}
}
}
#[derive(Debug, Clone)]
pub struct FileEntry {
pub path: PathBuf,
pub relative_path: PathBuf,
pub is_dir: bool,
pub size: u64,
pub extension: String,
pub depth: usize,
pub representation: Representation,
}
impl FileEntry {
pub fn new(path: &Path, root: &Path, is_dir: bool, size: u64) -> Self {
let relative_path = path.strip_prefix(root).unwrap_or(path).to_path_buf();
let depth = relative_path.components().count().saturating_sub(1);
let extension = if is_dir {
String::new()
} else {
path.extension()
.and_then(|e| e.to_str())
.unwrap_or("")
.to_string()
};
Self {
path: path.to_path_buf(),
relative_path,
is_dir,
size,
extension,
depth,
representation: Representation::NameOnly,
}
}
pub fn file_name(&self) -> &str {
self.path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("")
}
pub fn has_extension(&self, ext: &str) -> bool {
self.extension.eq_ignore_ascii_case(ext)
}
}