1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::{Dir, File};
use std::path::Path;

/// A directory entry, roughly analogous to [`std::fs::DirEntry`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DirEntry {
    /// A directory.
    Dir(Dir),
    /// A file.
    File(File),
}

impl DirEntry {
    /// The [`DirEntry`]'s full path.
    pub fn path(&self) -> &'static Path {
        match self {
            DirEntry::Dir(d) => d.path(),
            DirEntry::File(f) => f.path(),
        }
    }

    /// Try to get this as a [`Dir`], if it is one.
    pub fn as_dir(&self) -> Option<&Dir> {
        match self {
            DirEntry::Dir(d) => Some(d),
            DirEntry::File(_) => None,
        }
    }

    /// Try to get this as a [`File`], if it is one.
    pub fn as_file(&self) -> Option<&File> {
        match self {
            DirEntry::File(f) => Some(f),
            DirEntry::Dir(_) => None,
        }
    }

    /// Get this item's sub-items, if it has any.
    pub fn children(&self) -> &'static [DirEntry] {
        match self {
            DirEntry::Dir(d) => d.entries(),
            DirEntry::File(_) => &[],
        }
    }

    /// returns true if this entry is a file
    pub fn is_file(&self) -> bool {
        matches!(self, Self::File(_))
    }

    /// returns true if this entry is a dir
    pub fn is_dir(&self) -> bool {
        matches!(self, Self::Dir(_))
    }
}