obsidian_parser/
error.rs

1//! Error handling for Obsidian vault parsing operations
2
3use std::path::PathBuf;
4use thiserror::Error;
5
6/// Main error type for Obsidian parsing operations
7#[derive(Debug, Error)]
8pub enum Error {
9    /// I/O operation failed (file reading, directory traversal, etc.)
10    #[error("I/O error: {0}")]
11    Io(#[from] std::io::Error),
12
13    /// Invalid frontmatter format detected
14    ///
15    /// Occurs when:
16    /// - Frontmatter delimiters are incomplete (`---` missing)
17    /// - Content between delimiters is empty
18    ///
19    /// # Example
20    /// Parsing a file with malformed frontmatter:
21    /// ```text
22    /// ---
23    /// incomplete yaml
24    /// // Missing closing ---
25    /// ```
26    #[error("Invalid frontmatter format")]
27    InvalidFormat,
28
29    /// YAML parsing error in frontmatter properties
30    ///
31    /// # Example
32    /// Parsing invalid YAML syntax:
33    /// ```text
34    /// ---
35    /// key: @invalid_value
36    /// ---
37    /// ```
38    #[error("YAML parsing error: {0}")]
39    Yaml(#[from] serde_yml::Error),
40
41    /// Expected a directory path
42    ///
43    /// # Example
44    /// ```no_run
45    /// use obsidian_parser::prelude::*;
46    ///
47    /// // Will fail if passed a file path
48    /// Vault::open_default("notes.md").unwrap();
49    /// ```
50    #[error("Path: `{0}` is not a directory")]
51    IsNotDir(PathBuf),
52
53    /// Expected a file path
54    ///
55    /// # Example
56    /// ```no_run
57    /// use obsidian_parser::prelude::*;
58    ///
59    /// // Will fail if passed a directory path
60    /// ObFileOnDisk::from_file_default("/home/test");
61    /// ```
62    #[error("Path: `{0}` is not a directory")]
63    IsNotFile(PathBuf),
64
65    /// File contains invalid UTF-8 encoding
66    ///
67    /// Obsidian requires UTF-8 encoded files. This error occurs when
68    /// binary or improperly encoded files are encountered.
69    ///
70    /// # Example
71    /// ```no_run
72    /// # use obsidian_parser::prelude::*;
73    /// # use std::fs::File;
74    /// # use std::io::Write;
75    ///
76    /// // Create invalid UTF-8 file
77    /// let mut f = File::create("invalid.md").unwrap();
78    /// f.write_all(&[0xff, 0xfe, 0xfd]).unwrap();
79    ///
80    /// // Parsing will fail
81    /// ObFileInMemory::from_file_default("invalid.md").unwrap_err();
82    /// ```
83    #[error("File is not is not encoded in UTF-8")]
84    FromUtf8(#[from] std::string::FromUtf8Error),
85}