basalt_core/obsidian/
note.rs

1//! This module provides functionality operating with Obsidian notes.
2use std::path::{Path, PathBuf};
3
4use crate::obsidian::Error;
5
6/// Represents a single note (Markdown file) within a vault.
7#[derive(Debug, Clone, PartialEq, Default)]
8pub struct Note {
9    name: String,
10    path: PathBuf,
11}
12
13impl Note {
14    /// The base filename without `.md` extension.
15    pub fn name(&self) -> &str {
16        &self.name
17    }
18
19    /// Filesystem path to the `.md` file.
20    pub fn path(&self) -> &Path {
21        &self.path
22    }
23}
24
25impl TryFrom<(&str, PathBuf)> for Note {
26    type Error = Error;
27    fn try_from((name, path): (&str, PathBuf)) -> Result<Self, Self::Error> {
28        match path.is_file() {
29            true => Ok(Self {
30                name: name.to_string(),
31                path,
32            }),
33            false => Err(Error::Io(std::io::ErrorKind::IsADirectory.into())),
34        }
35    }
36}
37
38impl TryFrom<(String, PathBuf)> for Note {
39    type Error = Error;
40    fn try_from((name, path): (String, PathBuf)) -> Result<Self, Self::Error> {
41        Self::try_from((name.as_str(), path))
42    }
43}
44
45impl Note {
46    /// Creates a Note struct that is unchecked to be a valid file.
47    /// Only used in tests.
48    /// NOTE: This will be removed.
49    pub fn new_unchecked(name: &str, path: &Path) -> Self {
50        Self {
51            name: name.to_string(),
52            path: path.to_path_buf(),
53        }
54    }
55}