use anyhow::{Context, Result};
#[derive(thiserror::Error, Debug)]
pub enum MarkdownError {
#[error("Failed to parse Markdown: {0}")]
ParseError(String),
#[error("Failed to convert Markdown to HTML: {0}")]
ConversionError(String),
#[error("Failed to process custom block: {0}")]
CustomBlockError(String),
#[error("Syntax highlighting error: {0}")]
SyntaxHighlightError(String),
#[error("Invalid Markdown options: {0}")]
InvalidOptionsError(String),
#[error("Failed to load syntax set: {0}")]
SyntaxSetError(String),
}
pub fn parse_markdown_with_context(input: &str) -> Result<String> {
let parsed_content = some_markdown_parsing_function(input)
.with_context(|| "Failed while parsing markdown content")?;
Ok(parsed_content)
}
fn some_markdown_parsing_function(input: &str) -> Result<String> {
if input.is_empty() {
return Err(MarkdownError::ParseError(
"Input is empty".to_string(),
)
.into());
}
Ok("Parsed markdown content".to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_empty_input() {
let result = some_markdown_parsing_function("");
assert!(result.is_err());
if let Err(err) = result {
assert_eq!(
format!("{}", err),
"Failed to parse Markdown: Input is empty"
);
}
}
#[test]
fn test_successful_parse() {
let result =
some_markdown_parsing_function("Some markdown content");
assert!(result.is_ok());
assert_eq!(result.unwrap(), "Parsed markdown content");
}
#[test]
fn test_parse_markdown_with_context() {
let result = parse_markdown_with_context("");
assert!(result.is_err());
if let Err(err) = result {
let err_msg = format!("{:?}", err);
assert!(err_msg
.contains("Failed while parsing markdown content"));
}
}
}