include_dir/
dir_entry.rs

1use crate::{Dir, File};
2use std::path::Path;
3
4/// A directory entry, roughly analogous to [`std::fs::DirEntry`].
5#[derive(Debug, Clone, PartialEq)]
6pub enum DirEntry<'a> {
7    /// A directory.
8    Dir(Dir<'a>),
9    /// A file.
10    File(File<'a>),
11}
12
13impl<'a> DirEntry<'a> {
14    /// The [`DirEntry`]'s full path.
15    pub fn path(&self) -> &'a Path {
16        match self {
17            DirEntry::Dir(d) => d.path(),
18            DirEntry::File(f) => f.path(),
19        }
20    }
21
22    /// Try to get this as a [`Dir`], if it is one.
23    pub fn as_dir(&self) -> Option<&Dir<'a>> {
24        match self {
25            DirEntry::Dir(d) => Some(d),
26            DirEntry::File(_) => None,
27        }
28    }
29
30    /// Try to get this as a [`File`], if it is one.
31    pub fn as_file(&self) -> Option<&File<'a>> {
32        match self {
33            DirEntry::File(f) => Some(f),
34            DirEntry::Dir(_) => None,
35        }
36    }
37
38    /// Get this item's sub-items, if it has any.
39    pub fn children(&self) -> &'a [DirEntry<'a>] {
40        match self {
41            DirEntry::Dir(d) => d.entries(),
42            DirEntry::File(_) => &[],
43        }
44    }
45}