obsidian_parser/obfile/
obfile_on_disk.rs

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