dt_core/
error.rs

1use std::fmt;
2
3/// Error definitions to use across the library.
4#[derive(Debug, PartialEq)]
5pub enum Error {
6    /// Errors that occur when a config is deemed as invalid.
7    ConfigError(String),
8    /// Errors that occur during I/O operations.
9    IoError(String),
10    /// Errors that occur while parsing of structures fails.
11    ParseError(String),
12    /// Errors that occur while manipulating paths.
13    PathError(String),
14    /// Errors that occur while rendering templates.
15    RenderingError(String),
16    /// Errors that occur during syncing.
17    SyncingError(String),
18    /// Errors that occur while registering templates
19    TemplatingError(String),
20}
21
22/// `Result` type to use across the library.
23pub type Result<T> = std::result::Result<T, Error>;
24
25impl std::error::Error for Error {}
26
27impl fmt::Display for Error {
28    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29        match *self {
30            Error::ConfigError(ref msg) => {
31                write!(f, "Config Error: {}", msg)
32            }
33            Error::IoError(ref msg) => {
34                write!(f, "IO Error: {}", msg)
35            }
36            Error::ParseError(ref msg) => {
37                write!(f, "Parse Error: {}", msg)
38            }
39            Error::PathError(ref msg) => {
40                write!(f, "Path Error: {}", msg)
41            }
42            Error::RenderingError(ref msg) => {
43                write!(f, "Rendering Error: {}", msg)
44            }
45            Error::SyncingError(ref msg) => {
46                write!(f, "Syncing Error: {}", msg)
47            }
48            Error::TemplatingError(ref msg) => {
49                write!(f, "Templating Error: {}", msg)
50            }
51        }
52    }
53}
54
55impl From<std::io::Error> for Error {
56    fn from(err: std::io::Error) -> Self {
57        Error::IoError(err.to_string())
58    }
59}
60impl From<toml::de::Error> for Error {
61    fn from(err: toml::de::Error) -> Self {
62        Self::ParseError(err.to_string())
63    }
64}
65impl From<std::path::StripPrefixError> for Error {
66    fn from(err: std::path::StripPrefixError) -> Self {
67        Self::PathError(err.to_string())
68    }
69}
70impl From<glob::PatternError> for Error {
71    fn from(err: glob::PatternError) -> Self {
72        Self::PathError(err.to_string())
73    }
74}
75impl From<handlebars::RenderError> for Error {
76    fn from(err: handlebars::RenderError) -> Self {
77        Self::RenderingError(err.to_string())
78    }
79}
80impl From<std::str::Utf8Error> for Error {
81    fn from(err: std::str::Utf8Error) -> Self {
82        Self::RenderingError(err.to_string())
83    }
84}
85impl From<handlebars::TemplateError> for Error {
86    fn from(err: handlebars::TemplateError) -> Self {
87        Self::TemplatingError(err.to_string())
88    }
89}
90
91// Author: Blurgy <gy@blurgy.xyz>
92// Date:   Oct 29 2021, 23:07 [CST]