1use thiserror::Error;
4
5#[derive(Error, Debug)]
7pub enum Error {
8 #[error("Markdown parsing error: {0}")]
10 MarkdownParse(String),
11
12 #[error("Markdown to HTML conversion error: {0}")]
14 MarkdownToHtml(String),
15
16 #[error("Syntax highlighting error: {0}")]
18 SyntaxHighlight(String),
19
20 #[error("Unsupported language for syntax highlighting: {0}")]
22 UnsupportedLanguage(String),
23
24 #[error("Language parser not available for: {0}")]
26 LanguageParserUnavailable(String),
27
28 #[error("LaTeX rendering error: {0}")]
30 LatexRender(String),
31
32 #[error("Invalid LaTeX syntax: {0}")]
34 InvalidLatex(String),
35
36 #[error("Unknown LaTeX command: {0}")]
38 UnknownLatexCommand(String),
39
40 #[error("IO error: {0}")]
42 Io(#[from] std::io::Error),
43
44 #[error("Serialization error: {0}")]
46 Serialization(String),
47
48 #[error("Deserialization error: {0}")]
50 Deserialization(String),
51
52 #[error("Invalid input: {0}")]
54 InvalidInput(String),
55
56 #[error("Operation timed out after {0}ms")]
58 Timeout(u64),
59
60 #[error("Internal pipeline error: {0}")]
62 Internal(String),
63}
64
65impl Error {
66 pub fn markdown_parse<S: Into<String>>(message: S) -> Self {
68 Error::MarkdownParse(message.into())
69 }
70
71 pub fn markdown_to_html<S: Into<String>>(message: S) -> Self {
73 Error::MarkdownToHtml(message.into())
74 }
75
76 pub fn syntax_highlight<S: Into<String>>(message: S) -> Self {
78 Error::SyntaxHighlight(message.into())
79 }
80
81 pub fn unsupported_language<S: Into<String>>(language: S) -> Self {
83 Error::UnsupportedLanguage(language.into())
84 }
85
86 pub fn language_parser_unavailable<S: Into<String>>(language: S) -> Self {
88 Error::LanguageParserUnavailable(language.into())
89 }
90
91 pub fn latex_render<S: Into<String>>(message: S) -> Self {
93 Error::LatexRender(message.into())
94 }
95
96 pub fn invalid_latex<S: Into<String>>(message: S) -> Self {
98 Error::InvalidLatex(message.into())
99 }
100
101 pub fn unknown_latex_command<S: Into<String>>(command: S) -> Self {
103 Error::UnknownLatexCommand(command.into())
104 }
105
106 pub fn serialization<S: Into<String>>(message: S) -> Self {
108 Error::Serialization(message.into())
109 }
110
111 pub fn deserialization<S: Into<String>>(message: S) -> Self {
113 Error::Deserialization(message.into())
114 }
115
116 pub fn invalid_input<S: Into<String>>(message: S) -> Self {
118 Error::InvalidInput(message.into())
119 }
120
121 pub fn timeout(duration_ms: u64) -> Self {
123 Error::Timeout(duration_ms)
124 }
125
126 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
138pub 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}