use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("Markdown parsing error: {0}")]
MarkdownParse(String),
#[error("Markdown to HTML conversion error: {0}")]
MarkdownToHtml(String),
#[error("Syntax highlighting error: {0}")]
SyntaxHighlight(String),
#[error("Unsupported language for syntax highlighting: {0}")]
UnsupportedLanguage(String),
#[error("Language parser not available for: {0}")]
LanguageParserUnavailable(String),
#[error("LaTeX rendering error: {0}")]
LatexRender(String),
#[error("Invalid LaTeX syntax: {0}")]
InvalidLatex(String),
#[error("Unknown LaTeX command: {0}")]
UnknownLatexCommand(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Deserialization error: {0}")]
Deserialization(String),
#[error("Invalid input: {0}")]
InvalidInput(String),
#[error("Operation timed out after {0}ms")]
Timeout(u64),
#[error("Internal pipeline error: {0}")]
Internal(String),
}
impl Error {
pub fn markdown_parse<S: Into<String>>(message: S) -> Self {
Error::MarkdownParse(message.into())
}
pub fn markdown_to_html<S: Into<String>>(message: S) -> Self {
Error::MarkdownToHtml(message.into())
}
pub fn syntax_highlight<S: Into<String>>(message: S) -> Self {
Error::SyntaxHighlight(message.into())
}
pub fn unsupported_language<S: Into<String>>(language: S) -> Self {
Error::UnsupportedLanguage(language.into())
}
pub fn language_parser_unavailable<S: Into<String>>(language: S) -> Self {
Error::LanguageParserUnavailable(language.into())
}
pub fn latex_render<S: Into<String>>(message: S) -> Self {
Error::LatexRender(message.into())
}
pub fn invalid_latex<S: Into<String>>(message: S) -> Self {
Error::InvalidLatex(message.into())
}
pub fn unknown_latex_command<S: Into<String>>(command: S) -> Self {
Error::UnknownLatexCommand(command.into())
}
pub fn serialization<S: Into<String>>(message: S) -> Self {
Error::Serialization(message.into())
}
pub fn deserialization<S: Into<String>>(message: S) -> Self {
Error::Deserialization(message.into())
}
pub fn invalid_input<S: Into<String>>(message: S) -> Self {
Error::InvalidInput(message.into())
}
pub fn timeout(duration_ms: u64) -> Self {
Error::Timeout(duration_ms)
}
pub fn internal<S: Into<String>>(message: S) -> Self {
Error::Internal(message.into())
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Error::Serialization(err.to_string())
}
}
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = Error::markdown_parse("Invalid markdown");
assert_eq!(err.to_string(), "Markdown parsing error: Invalid markdown");
}
#[test]
fn test_unsupported_language_display() {
let err = Error::unsupported_language("brainfuck");
assert_eq!(
err.to_string(),
"Unsupported language for syntax highlighting: brainfuck"
);
}
}