use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use crate::entry::LoadedEntry;
use crate::error::ScanIssue;
#[derive(Debug, Clone, PartialEq)]
pub struct TreeNode {
pub path: PathBuf,
pub is_dir: bool,
pub is_expanded: bool,
pub is_loaded: bool,
pub is_selected: bool,
pub children: Vec<TreeNode>,
pub error: Option<ScanIssue>,
}
impl TreeNode {
pub(crate) fn new_root(path: PathBuf) -> Self {
Self {
path,
is_dir: true,
is_expanded: false,
is_loaded: false,
is_selected: false,
children: Vec::new(),
error: None,
}
}
pub(crate) fn from_entry(entry: &LoadedEntry) -> Self {
Self {
path: entry.path.clone(),
is_dir: entry.is_dir,
is_expanded: false,
is_loaded: false,
is_selected: false,
children: Vec::new(),
error: None,
}
}
pub fn file_name(&self) -> &OsStr {
self.path.file_name().unwrap_or(OsStr::new(""))
}
pub fn find(&self, path: &Path) -> Option<&TreeNode> {
if self.path == path {
return Some(self);
}
self.children
.iter()
.find(|c| path.starts_with(&c.path))
.and_then(|c| c.find(path))
}
pub(crate) fn find_mut(&mut self, path: &Path) -> Option<&mut TreeNode> {
if self.path == path {
return Some(self);
}
self.children
.iter_mut()
.find(|c| path.starts_with(&c.path))
.and_then(|c| c.find_mut(path))
}
}