basalt_core/obsidian/note.rs
1use std::{fs, path::PathBuf};
2
3use crate::obsidian::{Error, Result};
4
5/// Represents a single note (Markdown file) within a vault.
6#[derive(Debug, Clone, PartialEq, Default)]
7pub struct Note {
8 /// The base filename without `.md` extension.
9 pub name: String,
10
11 /// Filesystem path to the `.md` file.
12 pub path: PathBuf,
13}
14
15impl Note {
16 /// Reads the note's contents from disk to a `String`.
17 ///
18 /// # Examples
19 ///
20 /// ```
21 /// use basalt_core::obsidian::Note;
22 ///
23 /// let note = Note {
24 /// name: "Example".to_string(),
25 /// path: "path/to/Example.md".into(),
26 /// };
27 ///
28 /// _ = Note::read_to_string(¬e);
29 /// ```
30 pub fn read_to_string(note: &Note) -> Result<String> {
31 fs::read_to_string(¬e.path).map_err(Error::Io)
32 }
33
34 /// Replaces the content in the notes' markdown file with the given content.
35 ///
36 /// # Examples
37 ///
38 /// ```
39 /// use basalt_core::obsidian::Note;
40 ///
41 /// let note = Note {
42 /// name: "Example".to_string(),
43 /// path: "path/to/Example.md".into(),
44 /// };
45 ///
46 /// _ = Note::write(¬e, String::from("# Heading"));
47 /// ```
48 pub fn write(note: &Note, contents: String) -> Result<()> {
49 fs::write(¬e.path, contents).map_err(Error::Io)
50 }
51}