use crate::FileEntry;
use std::path::{Path, PathBuf};
pub const VIRTUAL_ROOT_SENTINEL: &str = "<fast-fs:virtual-root>";
pub fn virtual_root_path() -> PathBuf {
PathBuf::from(VIRTUAL_ROOT_SENTINEL)
}
pub fn is_virtual_root_path(path: &Path) -> bool {
path.as_os_str() == VIRTUAL_ROOT_SENTINEL
}
pub fn build_virtual_root_entries(roots: &[PathBuf]) -> Vec<FileEntry> {
roots
.iter()
.map(|root| {
let name = root
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| root.to_string_lossy().into_owned());
FileEntry {
path: root.clone(),
name,
is_dir: true,
is_hidden: false,
size: 0,
modified: None,
is_symlink: false,
is_readonly: false,
}
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn virtual_root_sentinel_is_detected() {
assert!(is_virtual_root_path(&virtual_root_path()));
assert!(!is_virtual_root_path(Path::new("/tmp")));
}
#[test]
fn build_entries_from_named_roots() {
let roots = vec![PathBuf::from("/tmp/a"), PathBuf::from("/tmp/b")];
let entries = build_virtual_root_entries(&roots);
assert_eq!(entries.len(), 2);
assert_eq!(entries[0].name, "a");
assert_eq!(entries[1].name, "b");
assert!(entries.iter().all(|e| e.is_dir));
}
#[test]
fn build_entries_from_bare_root() {
let roots = vec![PathBuf::from("/")];
let entries = build_virtual_root_entries(&roots);
assert_eq!(entries.len(), 1);
assert!(entries[0].is_dir);
assert!(!entries[0].name.is_empty());
}
}