1use std::fmt;
2
3#[derive(Debug)]
16#[non_exhaustive]
17pub enum DocError {
18 Frontmatter(String),
20 Markdown(String),
22 Template(tera::Error),
24 Io(std::io::Error),
26 Escape(String),
28 Extension(String),
30}
31
32impl fmt::Display for DocError {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 match self {
35 DocError::Frontmatter(msg) => write!(f, "invalid frontmatter: {msg}"),
36 DocError::Markdown(msg) => write!(f, "markdown render error: {msg}"),
37 DocError::Template(e) => write!(f, "template error: {e}"),
38 DocError::Io(e) => write!(f, "io error: {e}"),
39 DocError::Escape(path) => write!(f, "output path escaped root: {path}"),
40 DocError::Extension(msg) => write!(f, "extension error: {msg}"),
41 }
42 }
43}
44
45impl std::error::Error for DocError {
46 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
47 match self {
48 DocError::Template(e) => Some(e),
49 DocError::Io(e) => Some(e),
50 _ => None,
51 }
52 }
53}
54
55impl DocError {
56 pub fn user_message(&self) -> String {
58 match self {
59 DocError::Frontmatter(_) => "invalid frontmatter".to_string(),
60 DocError::Markdown(_) => "could not render markdown".to_string(),
61 DocError::Template(_) => "template error".to_string(),
62 DocError::Io(_) => "io error".to_string(),
63 DocError::Escape(_) => "output path escaped root".to_string(),
64 DocError::Extension(_) => "extension error".to_string(),
65 }
66 }
67}
68
69impl From<std::io::Error> for DocError {
70 fn from(e: std::io::Error) -> Self {
71 DocError::Io(e)
72 }
73}