tera_introspection/
errors.rs1use std::error::Error as StdError;
2use std::fmt;
3
4#[non_exhaustive]
6#[derive(Debug)]
7pub enum ErrorKind {
8 Msg(String),
10 Json(serde_json::Error),
12 Io(std::io::ErrorKind),
14}
15
16#[derive(Debug)]
18pub struct Error {
19 pub kind: ErrorKind,
21 source: Option<Box<dyn StdError + Sync + Send>>,
22}
23
24impl fmt::Display for Error {
25 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26 match self.kind {
27 ErrorKind::Msg(ref message) => write!(f, "{}", message),
28 ErrorKind::Json(ref e) => write!(f, "{}", e),
29 ErrorKind::Io(ref io_error) => {
30 write!(
31 f,
32 "Io error while writing rendered value to output: {:?}",
33 io_error
34 )
35 }
36 }
37 }
38}
39
40impl StdError for Error {
41 fn source(&self) -> Option<&(dyn StdError + 'static)> {
42 self.source
43 .as_ref()
44 .map(|c| &**c as &(dyn StdError + 'static))
45 }
46}
47
48impl Error {
49 pub fn msg(value: impl ToString) -> Self {
51 Self {
52 kind: ErrorKind::Msg(value.to_string()),
53 source: None,
54 }
55 }
56
57 pub fn json(value: serde_json::Error) -> Self {
59 Self {
60 kind: ErrorKind::Json(value),
61 source: None,
62 }
63 }
64
65 pub fn io_error(error: std::io::Error) -> Self {
67 Self {
68 kind: ErrorKind::Io(error.kind()),
69 source: Some(Box::new(error)),
70 }
71 }
72}
73
74impl From<std::io::Error> for Error {
75 fn from(error: std::io::Error) -> Self {
76 Self::io_error(error)
77 }
78}
79impl From<&str> for Error {
80 fn from(e: &str) -> Self {
81 Self::msg(e)
82 }
83}
84impl From<String> for Error {
85 fn from(e: String) -> Self {
86 Self::msg(e)
87 }
88}
89impl From<serde_json::Error> for Error {
90 fn from(e: serde_json::Error) -> Self {
91 Self::json(e)
92 }
93}
94pub type Result<T> = ::std::result::Result<T, Error>;
96
97#[cfg(test)]
98mod tests {
99 #[test]
100 fn test_error_is_send_and_sync() {
101 fn test_send_sync<T: Send + Sync>() {}
102
103 test_send_sync::<super::Error>();
104 }
105}