Skip to main content

basalt_tui/explorer/
item.rs

1use std::path::{Path, PathBuf};
2
3use basalt_core::obsidian::{Note, VaultEntry};
4
5#[derive(Debug, Clone, PartialEq)]
6pub enum Item {
7    File {
8        note: Note,
9        depth: usize,
10    },
11    Directory {
12        name: String,
13        path: PathBuf,
14        expanded: bool,
15        items: Vec<Item>,
16        depth: usize,
17    },
18}
19
20impl Item {
21    pub(crate) fn path(&self) -> &Path {
22        match self {
23            Self::File { note, .. } => note.path(),
24            Self::Directory { path, .. } => path,
25        }
26    }
27
28    pub(crate) fn depth(&self) -> usize {
29        match self {
30            Self::Directory { depth, .. } | Self::File { depth, .. } => *depth,
31        }
32    }
33
34    pub(crate) fn name(&self) -> &str {
35        match self {
36            Self::Directory { name, .. } => name.as_str(),
37            Self::File { note, .. } => note.name(),
38        }
39    }
40
41    pub(crate) fn is_dir(&self) -> bool {
42        matches!(self, Self::Directory { .. })
43    }
44}
45
46impl From<VaultEntry> for Item {
47    fn from(value: VaultEntry) -> Self {
48        fn to_items(depth: usize, entry: VaultEntry) -> Item {
49            match entry {
50                VaultEntry::File(note) => Item::File { note, depth },
51                VaultEntry::Directory {
52                    name,
53                    entries,
54                    path,
55                } => Item::Directory {
56                    name,
57                    path,
58                    depth,
59                    expanded: false,
60                    items: entries
61                        .into_iter()
62                        .map(|entry| to_items(depth + 1, entry))
63                        .collect(),
64                },
65            }
66        }
67
68        to_items(0, value)
69    }
70}