1use std::{fs, ops::Deref, path::{Path, PathBuf}};
2use strum::IntoEnumIterator;
3
4use crate::*;
5
6pub struct NotesDir(PathBuf);
7
8impl Deref for NotesDir {
9 type Target = Path;
10
11 fn deref(&self) -> &Self::Target {
12 &self.0
13 }
14}
15
16impl NotesDir {
17 pub fn new(dir: PathBuf) -> Self {
18 Self(dir)
19 }
20
21 pub fn init(&self) -> Result<()> {
22 let notes_dir = &self.0;
23 for kind in NoteKind::iter() {
24 let dir_name: &'static str = kind.into();
25 let kind_dir = notes_dir.join(dir_name);
26 if !kind_dir.is_dir() {
27 fs::create_dir_all(&kind_dir)
28 .map_err(|e| Error::Io(format!("Unable to create note dir: {}", kind_dir.display()), e))?;
29 }
30 }
31
32 Ok(())
33 }
34
35 pub fn kind_dir(&self, kind: NoteKind) -> PathBuf {
36 let dir_name: &'static str = kind.into();
37 self.join(dir_name)
38 }
39
40 pub fn templates_dir(&self) -> PathBuf {
41 self.join(".templates")
42 }
43}