bracket/error/
mod.rs

1//! Error types.
2use std::fmt;
3use thiserror::Error;
4
5pub mod helper;
6pub mod render;
7pub mod source;
8pub mod syntax;
9
10pub use helper::HelperError;
11pub use render::RenderError;
12pub use source::{ErrorInfo, SourcePos};
13pub use syntax::SyntaxError;
14
15/// Generic error type that wraps more specific types and is
16/// returned when using the `Registry`.
17#[derive(Error, Eq, PartialEq)]
18pub enum Error {
19    /// Proxy syntax errors.
20    #[error(transparent)]
21    Syntax(#[from] SyntaxError),
22    /// Proxy render errors.
23    #[error(transparent)]
24    Render(#[from] RenderError),
25    /// Error when a named template does not exist.
26    #[error("Template not found '{0}'")]
27    TemplateNotFound(String),
28    /// Proxy IO errors.
29    #[error(transparent)]
30    Io(#[from] IoError),
31}
32
33impl fmt::Debug for Error {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match *self {
36            Self::Syntax(ref e) => fmt::Debug::fmt(e, f),
37            Self::Render(ref e) => fmt::Debug::fmt(e, f),
38            Self::TemplateNotFound(_) => fmt::Display::fmt(self, f),
39            Self::Io(ref e) => fmt::Debug::fmt(e, f),
40        }
41    }
42}
43
44impl From<std::io::Error> for Error {
45    fn from(err: std::io::Error) -> Self {
46        Self::Io(IoError::Io(err))
47    }
48}
49
50/// Wrapper for IO errors that implements `PartialEq` to
51/// facilitate easier testing using `assert_eq!()`.
52#[derive(thiserror::Error)]
53pub enum IoError {
54    /// Proxy IO errors.
55    #[error(transparent)]
56    Io(#[from] std::io::Error),
57}
58
59impl PartialEq for IoError {
60    fn eq(&self, other: &Self) -> bool {
61        match (self, other) {
62            (Self::Io(ref s), Self::Io(ref o)) => s.kind() == o.kind(),
63        }
64    }
65}
66
67impl Eq for IoError {}
68
69impl fmt::Debug for IoError {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        match *self {
72            Self::Io(ref e) => fmt::Display::fmt(e, f),
73        }
74    }
75}