Skip to main content

basalt_core/obsidian/
directory.rs

1//! This module provides functionality operating with Obsidian vault folders.
2use std::path::{Path, PathBuf};
3
4use crate::obsidian::Error;
5
6/// Represents a directory within the vault.
7#[derive(Debug, Clone, PartialEq, Default)]
8pub struct Directory {
9    name: String,
10    path: PathBuf,
11}
12
13impl Directory {
14    /// Create a new directory structure
15    pub fn new(name: &str, path: &Path) -> Self {
16        Self {
17            name: name.to_string(),
18            path: path.to_path_buf(),
19        }
20    }
21
22    /// The complete filesystem path to the directory.
23    pub fn name(&self) -> &str {
24        &self.name
25    }
26
27    /// The directory name without the parent path.
28    pub fn path(&self) -> &Path {
29        &self.path
30    }
31}
32
33impl TryFrom<(&str, PathBuf)> for Directory {
34    type Error = Error;
35    fn try_from((name, path): (&str, PathBuf)) -> Result<Self, Self::Error> {
36        match path.is_dir() {
37            true => Ok(Self {
38                name: name.to_string(),
39                path,
40            }),
41            false => Err(Error::Io(std::io::ErrorKind::NotADirectory.into())),
42        }
43    }
44}
45
46impl TryFrom<(String, PathBuf)> for Directory {
47    type Error = Error;
48    fn try_from((name, path): (String, PathBuf)) -> Result<Self, Self::Error> {
49        Self::try_from((name.as_str(), path))
50    }
51}