ricecoder_storage/markdown_config/
error.rs

1//! Error types for markdown configuration parsing and validation
2
3use std::path::PathBuf;
4use thiserror::Error;
5
6/// Errors that can occur during markdown configuration parsing and validation
7#[derive(Debug, Error)]
8pub enum MarkdownConfigError {
9    /// Failed to parse markdown content
10    #[error("Failed to parse markdown: {message}")]
11    ParseError { message: String },
12
13    /// Invalid YAML frontmatter
14    #[error("Invalid YAML frontmatter: {message}")]
15    YamlError { message: String },
16
17    /// Schema validation failed
18    #[error("Schema validation failed: {message}")]
19    ValidationError { message: String },
20
21    /// Failed to load configuration file
22    #[error("Failed to load configuration from {path}: {message}")]
23    LoadError { path: PathBuf, message: String },
24
25    /// Registration failed
26    #[error("Registration failed: {message}")]
27    RegistrationError { message: String },
28
29    /// File watch error
30    #[error("File watch error: {message}")]
31    WatchError { message: String },
32
33    /// Missing required field
34    #[error("Missing required field: {field}")]
35    MissingField { field: String },
36
37    /// Invalid field value
38    #[error("Invalid value for field '{field}': {message}")]
39    InvalidFieldValue { field: String, message: String },
40
41    /// IO error
42    #[error("IO error: {0}")]
43    IoError(#[from] std::io::Error),
44}
45
46impl MarkdownConfigError {
47    /// Create a parse error with context
48    pub fn parse_error(message: impl Into<String>) -> Self {
49        Self::ParseError {
50            message: message.into(),
51        }
52    }
53
54    /// Create a YAML error with context
55    pub fn yaml_error(message: impl Into<String>) -> Self {
56        Self::YamlError {
57            message: message.into(),
58        }
59    }
60
61    /// Create a validation error with context
62    pub fn validation_error(message: impl Into<String>) -> Self {
63        Self::ValidationError {
64            message: message.into(),
65        }
66    }
67
68    /// Create a load error with path and message
69    pub fn load_error(path: impl Into<PathBuf>, message: impl Into<String>) -> Self {
70        Self::LoadError {
71            path: path.into(),
72            message: message.into(),
73        }
74    }
75
76    /// Create a registration error
77    pub fn registration_error(message: impl Into<String>) -> Self {
78        Self::RegistrationError {
79            message: message.into(),
80        }
81    }
82
83    /// Create a missing field error
84    pub fn missing_field(field: impl Into<String>) -> Self {
85        Self::MissingField {
86            field: field.into(),
87        }
88    }
89
90    /// Create an invalid field value error
91    pub fn invalid_field_value(field: impl Into<String>, message: impl Into<String>) -> Self {
92        Self::InvalidFieldValue {
93            field: field.into(),
94            message: message.into(),
95        }
96    }
97
98    /// Create a watch error
99    pub fn watch_error(message: impl Into<String>) -> Self {
100        Self::WatchError {
101            message: message.into(),
102        }
103    }
104}
105
106/// Result type for markdown configuration operations
107pub type MarkdownConfigResult<T> = Result<T, MarkdownConfigError>;