basalt_core/obsidian/note.rs
1use std::{fs, path::PathBuf, time::SystemTime};
2
3use crate::obsidian::{Error, Result};
4
5/// Represents a single note (Markdown file) within a vault.
6#[derive(Debug, Clone, PartialEq)]
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 /// File creation time.
15 ///
16 /// TODO: Use chrono or time crates for better time format handling
17 pub created: SystemTime,
18}
19
20impl Note {
21 /// Reads the note's contents from disk to a `String`.
22 ///
23 /// # Examples
24 ///
25 /// ```
26 /// use basalt_core::obsidian::Note;
27 ///
28 /// let note = Note {
29 /// name: "Example".to_string(),
30 /// path: "path/to/Example.md".into(),
31 /// ..Default::default()
32 /// };
33 ///
34 /// _ = Note::read_to_string(¬e);
35 /// ```
36 pub fn read_to_string(note: &Note) -> Result<String> {
37 fs::read_to_string(¬e.path).map_err(Error::Io)
38 }
39
40 /// Writes given content to notes path.
41 ///
42 /// # Examples
43 ///
44 /// ```
45 /// use basalt_core::obsidian::Note;
46 ///
47 /// let note = Note {
48 /// name: "Example".to_string(),
49 /// path: "path/to/Example.md".into(),
50 /// ..Default::default()
51 /// };
52 ///
53 /// _ = Note::write(¬e, String::from("# Heading"));
54 /// ```
55 pub fn write(note: &Note, contents: String) -> Result<()> {
56 fs::write(¬e.path, contents).map_err(Error::Io)
57 }
58}