Skip to main content

docs_pipeline/
error.rs

1//! Error types for the docs pipeline.
2
3use thiserror::Error;
4
5/// Error type for the docs pipeline.
6#[derive(Error, Debug)]
7pub enum Error {
8    /// Error during markdown parsing
9    #[error("Markdown parsing error: {0}")]
10    MarkdownParse(String),
11
12    /// Error during markdown to HTML conversion
13    #[error("Markdown to HTML conversion error: {0}")]
14    MarkdownToHtml(String),
15
16    /// Syntax highlighting error
17    #[error("Syntax highlighting error: {0}")]
18    SyntaxHighlight(String),
19
20    /// Unsupported language for syntax highlighting
21    #[error("Unsupported language for syntax highlighting: {0}")]
22    UnsupportedLanguage(String),
23
24    /// Language parser not available
25    #[error("Language parser not available for: {0}")]
26    LanguageParserUnavailable(String),
27
28    /// LaTeX rendering error
29    #[error("LaTeX rendering error: {0}")]
30    LatexRender(String),
31
32    /// Invalid LaTeX syntax
33    #[error("Invalid LaTeX syntax: {0}")]
34    InvalidLatex(String),
35
36    /// Unknown LaTeX command
37    #[error("Unknown LaTeX command: {0}")]
38    UnknownLatexCommand(String),
39
40    /// Input/output error
41    #[error("IO error: {0}")]
42    Io(#[from] std::io::Error),
43
44    /// Serialization error
45    #[error("Serialization error: {0}")]
46    Serialization(String),
47
48    /// Deserialization error
49    #[error("Deserialization error: {0}")]
50    Deserialization(String),
51
52    /// Invalid input
53    #[error("Invalid input: {0}")]
54    InvalidInput(String),
55
56    /// Timeout
57    #[error("Operation timed out after {0}ms")]
58    Timeout(u64),
59
60    /// Internal error
61    #[error("Internal pipeline error: {0}")]
62    Internal(String),
63}
64
65impl Error {
66    /// Create a new markdown parsing error
67    pub fn markdown_parse<S: Into<String>>(message: S) -> Self {
68        Error::MarkdownParse(message.into())
69    }
70
71    /// Create a new markdown to HTML conversion error
72    pub fn markdown_to_html<S: Into<String>>(message: S) -> Self {
73        Error::MarkdownToHtml(message.into())
74    }
75
76    /// Create a new syntax highlighting error
77    pub fn syntax_highlight<S: Into<String>>(message: S) -> Self {
78        Error::SyntaxHighlight(message.into())
79    }
80
81    /// Create a new unsupported language error
82    pub fn unsupported_language<S: Into<String>>(language: S) -> Self {
83        Error::UnsupportedLanguage(language.into())
84    }
85
86    /// Create a new language parser unavailable error
87    pub fn language_parser_unavailable<S: Into<String>>(language: S) -> Self {
88        Error::LanguageParserUnavailable(language.into())
89    }
90
91    /// Create a new LaTeX rendering error
92    pub fn latex_render<S: Into<String>>(message: S) -> Self {
93        Error::LatexRender(message.into())
94    }
95
96    /// Create a new invalid LaTeX error
97    pub fn invalid_latex<S: Into<String>>(message: S) -> Self {
98        Error::InvalidLatex(message.into())
99    }
100
101    /// Create a new unknown LaTeX command error
102    pub fn unknown_latex_command<S: Into<String>>(command: S) -> Self {
103        Error::UnknownLatexCommand(command.into())
104    }
105
106    /// Create a new serialization error
107    pub fn serialization<S: Into<String>>(message: S) -> Self {
108        Error::Serialization(message.into())
109    }
110
111    /// Create a new deserialization error
112    pub fn deserialization<S: Into<String>>(message: S) -> Self {
113        Error::Deserialization(message.into())
114    }
115
116    /// Create a new invalid input error
117    pub fn invalid_input<S: Into<String>>(message: S) -> Self {
118        Error::InvalidInput(message.into())
119    }
120
121    /// Create a new timeout error
122    pub fn timeout(duration_ms: u64) -> Self {
123        Error::Timeout(duration_ms)
124    }
125
126    /// Create a new internal error
127    pub fn internal<S: Into<String>>(message: S) -> Self {
128        Error::Internal(message.into())
129    }
130}
131
132impl From<serde_json::Error> for Error {
133    fn from(err: serde_json::Error) -> Self {
134        Error::Serialization(err.to_string())
135    }
136}
137
138/// Result type alias for pipeline operations
139pub type Result<T> = std::result::Result<T, Error>;
140
141#[cfg(test)]
142mod tests {
143    use super::*;
144
145    #[test]
146    fn test_error_display() {
147        let err = Error::markdown_parse("Invalid markdown");
148        assert_eq!(err.to_string(), "Markdown parsing error: Invalid markdown");
149    }
150
151    #[test]
152    fn test_unsupported_language_display() {
153        let err = Error::unsupported_language("brainfuck");
154        assert_eq!(
155            err.to_string(),
156            "Unsupported language for syntax highlighting: brainfuck"
157        );
158    }
159}