Skip to content
Myr

entry.rs

This file

Path

src/entry.rs

62 lines · 1.5 KiB · Rust

Last changed by

send DirEntries to output instead of just pathJonathan Gorenbefore adoption813a802

History of this file

Take it

1use std::{2    fs::{FileType, Metadata},3    path::{Path, PathBuf},4};56use once_cell::unsync::OnceCell;78enum DirEntryInner {9    Normal(ignore::DirEntry),10    BrokenSymlink(PathBuf),11}1213pub struct DirEntry {14    inner: DirEntryInner,15    metadata: OnceCell<Option<Metadata>>,16}1718impl DirEntry {19    pub fn normal(e: ignore::DirEntry) -> Self {20        Self {21            inner: DirEntryInner::Normal(e),22            metadata: OnceCell::new(),23        }24    }2526    pub fn broken_symlink(path: PathBuf) -> Self {27        Self {28            inner: DirEntryInner::BrokenSymlink(path),29            metadata: OnceCell::new(),30        }31    }3233    pub fn path(&self) -> &Path {34        match &self.inner {35            DirEntryInner::Normal(e) => e.path(),36            DirEntryInner::BrokenSymlink(pathbuf) => pathbuf.as_path(),37        }38    }3940    pub fn file_type(&self) -> Option<FileType> {41        match &self.inner {42            DirEntryInner::Normal(e) => e.file_type(),43            DirEntryInner::BrokenSymlink(_) => self.metadata().map(|m| m.file_type()),44        }45    }4647    pub fn metadata(&self) -> Option<&Metadata> {48        self.metadata49            .get_or_init(|| match &self.inner {50                DirEntryInner::Normal(e) => e.metadata().ok(),51                DirEntryInner::BrokenSymlink(path) => path.symlink_metadata().ok(),52            })53            .as_ref()54    }5556    pub fn depth(&self) -> Option<usize> {57        match &self.inner {58            DirEntryInner::Normal(e) => Some(e.depth()),59            DirEntryInner::BrokenSymlink(_) => None,60        }61    }62}