obsidian_parser/obfile/
obfile_on_disk.rs

1use crate::error::Error;
2use crate::obfile::{ObFile, parse_obfile};
3use serde::de::DeserializeOwned;
4use std::marker::PhantomData;
5use std::{collections::HashMap, path::PathBuf};
6
7/// On-disk representation of an Obsidian note file
8///
9/// Optimized for vault operations where:
10/// 1. Memory efficiency is critical (large vaults)
11/// 2. Storage is fast (SSD/NVMe)
12/// 3. Content is accessed infrequently
13///
14/// # Tradeoffs vs `ObFileInMemory`
15/// | Characteristic       | `ObFileOnDisk`          | `ObFileInMemory`            |
16/// |----------------------|-------------------------|-----------------------------|
17/// | Memory usage         | **Minimal** (~24 bytes) | High (content + properties) |
18/// | File access          | On-demand               | Preloaded                   |
19/// | Best for             | SSD-based vaults        | RAM-heavy workflows         |
20/// | Content access cost  | Disk read               | Zero cost                   |
21///
22/// # Recommendation
23/// Prefer `ObFileOnDisk` for vault operations on modern hardware. The combination of
24/// SSD speeds and Rust's efficient I/O makes this implementation ideal for:
25/// - Large vaults (1000+ files)
26/// - Graph processing
27///
28/// # Warning
29/// Requires **persistent file access** throughout the object's lifetime. If files are moved/deleted,
30/// calling `content()` or `properties()` will **panic**
31#[derive(Debug, Default, PartialEq, Clone)]
32pub struct ObFileOnDisk<T = HashMap<String, serde_yml::Value>>
33where
34    T: DeserializeOwned + Default + Clone + Send,
35{
36    /// Absolute path to the source Markdown file
37    pub path: PathBuf,
38
39    phantom: PhantomData<T>,
40}
41
42impl<T: DeserializeOwned + Default + Clone + Send> ObFile<T> for ObFileOnDisk<T> {
43    /// Returns the note's content body (without frontmatter)
44    ///
45    /// # Panics
46    /// - If file doesn't exist
47    /// - On filesystem errors
48    /// - If file contains invalid UTF-8
49    ///
50    /// # Performance
51    /// Performs disk read on every call. Suitable for:
52    /// - Single-pass processing (link extraction, analysis)
53    /// - Large files where in-memory storage is prohibitive
54    ///
55    /// For repeated access, consider caching or `ObFileInMemory`.
56    fn content(&self) -> String {
57        let raw_text = std::fs::read_to_string(&self.path).unwrap();
58        let (valid_properties, parts) = parse_obfile(&raw_text);
59
60        match (valid_properties, &parts[..]) {
61            (false, _) => raw_text,
62            (true, [_, _, content]) => (*content).trim().to_string(),
63            _ => unimplemented!(),
64        }
65    }
66
67    /// Parses YAML frontmatter directly from disk
68    ///
69    /// # Panics
70    /// - If properties can't be deserialized
71    fn properties(&self) -> T {
72        let raw_text = std::fs::read_to_string(&self.path).unwrap();
73        let (valid_properties, parts) = parse_obfile(&raw_text);
74
75        match (valid_properties, &parts[..]) {
76            (false, _) => T::default(),
77            (true, [_, properties, _]) => serde_yml::from_str(properties).unwrap(),
78            _ => unreachable!(),
79        }
80    }
81
82    fn path(&self) -> Option<PathBuf> {
83        Some(self.path.clone())
84    }
85
86    /// Creates instance from text (requires path!)
87    ///
88    /// Dont use this function. Use `from_file`
89    fn from_string<P: AsRef<std::path::Path>>(
90        _raw_text: &str,
91        path: Option<P>,
92    ) -> Result<Self, Error> {
93        let path_buf = path.expect("Path is required").as_ref().to_path_buf();
94
95        Self::from_file(path_buf)
96    }
97
98    /// Creates instance from path
99    fn from_file<P: AsRef<std::path::Path>>(path: P) -> Result<Self, Error> {
100        let path_buf = path.as_ref().to_path_buf();
101
102        if !path_buf.is_file() {
103            return Err(Error::IsNotFile(path_buf));
104        }
105
106        Ok(Self {
107            path: path_buf,
108            phantom: PhantomData,
109        })
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116    use crate::obfile::ObFileDefault;
117    use crate::obfile::tests::{from_file, from_file_with_unicode, impl_test_for_obfile};
118    use crate::test_utils::init_test_logger;
119    use std::io::Write;
120    use tempfile::NamedTempFile;
121
122    impl_test_for_obfile!(impl_from_file, from_file, ObFileOnDisk);
123
124    impl_test_for_obfile!(
125        impl_from_file_with_unicode,
126        from_file_with_unicode,
127        ObFileOnDisk
128    );
129
130    #[test]
131    #[should_panic]
132    fn use_from_string_without_path() {
133        init_test_logger();
134        ObFileOnDisk::from_string_default("", None::<&str>).unwrap();
135    }
136
137    #[test]
138    #[should_panic]
139    fn use_from_file_with_path_not_file() {
140        init_test_logger();
141        let temp_dir = tempfile::tempdir().unwrap();
142
143        ObFileOnDisk::from_file_default(temp_dir.path()).unwrap();
144    }
145
146    #[test]
147    fn get_path() {
148        init_test_logger();
149        let test_file = NamedTempFile::new().unwrap();
150        let file = ObFileOnDisk::from_file_default(test_file.path()).unwrap();
151
152        assert_eq!(file.path().unwrap(), test_file.path());
153        assert_eq!(file.path, test_file.path());
154    }
155
156    #[test]
157    fn get_content() {
158        init_test_logger();
159        let test_data = "DATA";
160        let mut test_file = NamedTempFile::new().unwrap();
161        test_file.write_all(test_data.as_bytes()).unwrap();
162
163        let file = ObFileOnDisk::from_file_default(test_file.path()).unwrap();
164        assert_eq!(file.content(), test_data);
165    }
166
167    #[test]
168    fn get_properties() {
169        init_test_logger();
170        let test_data = "---\ntime: now\n---\nDATA";
171        let mut test_file = NamedTempFile::new().unwrap();
172        test_file.write_all(test_data.as_bytes()).unwrap();
173
174        let file = ObFileOnDisk::from_file_default(test_file.path()).unwrap();
175        assert_eq!(file.content(), "DATA");
176        assert_eq!(file.properties()["time"], "now");
177    }
178}