Skip to main content

json_e/
errors.rs

1#![allow(unused_macros)]
2
3use thiserror::Error;
4
5/// Construct a new interpreter error, as an anyhow::Error
6macro_rules! interpreter_error {
7    ($err:expr $(,)?) => ({
8        anyhow::Error::new($crate::errors::InterpreterError($err.to_string()))
9    });
10    ($fmt:expr, $($arg:tt)*) => {
11        anyhow::Error::new($crate::errors::InterpreterError(format!($fmt, $($arg)*)))
12    };
13}
14
15/// Construct a new template error, as an anyhow::Error
16macro_rules! template_error {
17    ($err:expr $(,)?) => ({
18        anyhow::Error::new($crate::errors::TemplateError($err.to_string()))
19    });
20    ($fmt:expr, $($arg:tt)*) => {
21        anyhow::Error::new($crate::errors::TemplateError(format!($fmt, $($arg)*)))
22    };
23}
24
25/// Utility for asserting that an anyhow::Result contains an interpreter error
26#[cfg(test)]
27macro_rules! assert_interpreter_error {
28    ($left:expr, $right:expr) => ({
29        assert_eq!(
30            $left.expect_err("Expected an error, got").downcast_ref::<$crate::errors::InterpreterError>().expect("Expected a InterpreterError"),
31            &$crate::errors::InterpreterError($right.to_string())
32        );
33    });
34    ($left:expr, $right:expr,) => ({
35        assert_interpreter_error!($left, $right)
36    });
37    ($left:expr, $right:expr, $($arg:tt)+) => ({
38        assert_eq!(
39            $left.expect_err("Expected an error, got").downcast_ref::<$crate::errors::InterpreterError>().expect("Expected a InterpreterError"),
40            &$crate::errors::InterpreterError($right.to_string()),
41            $($arg)*
42        );
43    });
44}
45
46/// Utility for asserting that an anyhow::Result contains an template error
47#[cfg(test)]
48macro_rules! assert_template_error {
49    ($left:expr, $right:expr) => ({
50        assert_eq!(
51            $left.expect_err("Expected an error, got").downcast_ref::<$crate::errors::TemplateError>().expect("Expected a TemplateError"),
52            &$crate::errors::TemplateError($right.to_string())
53        );
54    });
55    ($left:expr, $right:expr,) => ({
56        assert_template_error!($left, $right)
57    });
58    ($left:expr, $right:expr, $($arg:tt)+) => ({
59        assert_eq!(
60            $left.expect_err("Expected an error, got").downcast_ref::<$crate::errors::TemplateError>().expect("Expected a TemplateError"),
61            &$crate::errors::TemplateError($right.to_string()),
62            $($arg)*
63        );
64    });
65}
66
67/// An InterpreterError indicates something that failed during evaluation of a JSON-e expression.
68#[derive(Debug, Error, Eq, PartialEq, Clone)]
69#[error("Interpreter Error: {0}")]
70pub struct InterpreterError(pub(crate) String);
71
72/// An TemplateError indicates something that failed during evaluation of a JSON-e expression.
73#[derive(Debug, Error, Eq, PartialEq, Clone)]
74#[error("TemplateError: {0}")]
75pub struct TemplateError(pub(crate) String);