magical markdown slides
1use std::io;
2use thiserror::Error;
3
4/// Errors that can occur during slide parsing and rendering
5#[derive(Error, Debug)]
6pub enum SlideError {
7 #[error("Failed to read file: {0}")]
8 IoError(#[from] io::Error),
9
10 #[error("Failed to parse markdown at line {line}: {message}")]
11 ParseError { line: usize, message: String },
12
13 #[error("Invalid slide format: {0}")]
14 InvalidFormat(String),
15
16 #[error("Front matter error: {0}")]
17 FrontMatterError(String),
18
19 #[error("YAML parsing failed: {0}")]
20 YamlError(#[from] serde_yml::Error),
21
22 #[error("JSON parsing failed: {0}")]
23 JsonError(#[from] serde_json::Error),
24
25 #[error("Theme validation error: {0}")]
26 ThemeError(String),
27}
28
29pub type Result<T> = std::result::Result<T, SlideError>;
30
31impl SlideError {
32 pub fn parse_error(line: usize, message: impl Into<String>) -> Self {
33 Self::ParseError {
34 line,
35 message: message.into(),
36 }
37 }
38
39 pub fn invalid_format(message: impl Into<String>) -> Self {
40 Self::InvalidFormat(message.into())
41 }
42
43 pub fn front_matter(message: impl Into<String>) -> Self {
44 Self::FrontMatterError(message.into())
45 }
46
47 pub fn theme_error(message: impl Into<String>) -> Self {
48 Self::ThemeError(message.into())
49 }
50}
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn error_creation() {
58 let err = SlideError::parse_error(10, "Invalid syntax");
59 assert!(err.to_string().contains("line 10"));
60 assert!(err.to_string().contains("Invalid syntax"));
61 }
62
63 #[test]
64 fn error_conversion() {
65 let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
66 let slide_err: SlideError = io_err.into();
67 assert!(slide_err.to_string().contains("Failed to read file"));
68 }
69}