Skip to main content

vfs_kit/vfs/
dir_entry.rs

1use std::path::{Component, Path, PathBuf};
2
3#[derive(Debug, Copy, Clone, PartialEq)]
4pub enum DirEntryType {
5    File,
6    Directory,
7}
8
9#[derive(Debug, Clone, PartialEq)]
10pub struct DirEntry {
11    path: PathBuf,
12    kind: DirEntryType,
13}
14
15impl DirEntry {
16    pub fn new<P: AsRef<Path>>(path: P, kind: DirEntryType) -> DirEntry {
17        DirEntry { path: path.as_ref().to_path_buf(), kind }
18    }
19    
20    pub fn path(&self) -> &Path {
21        &self.path
22    }
23    
24    pub fn kind(&self) -> DirEntryType {
25        self.kind
26    }
27    
28    pub fn is_file(&self) -> bool {
29        self.kind == DirEntryType::File
30    }
31    
32    pub fn is_dir(&self) -> bool {
33        self.kind == DirEntryType::Directory
34    }
35    
36    pub fn is_root(&self) -> bool {
37        let components: Vec<_> = self.path.components().collect();
38        self.kind == DirEntryType::Directory
39            && components.len() == 1
40            && components[0] == Component::RootDir
41    }
42}