json_e/
errors.rs

1#![allow(unused_macros)]
2
3use thiserror::Error;
4
5/// Construct a new syntax error, as an anyhow::Error
6macro_rules! syntax_error {
7    ($err:expr $(,)?) => ({
8        anyhow::Error::new($crate::errors::SyntaxError($err.to_string()))
9    });
10    ($fmt:expr, $($arg:tt)*) => {
11        anyhow::Error::new($crate::errors::SyntaxError(format!($fmt, $($arg)*)))
12    };
13}
14
15/// Construct a new interpreter error, as an anyhow::Error
16macro_rules! interpreter_error {
17    ($err:expr $(,)?) => ({
18        anyhow::Error::new($crate::errors::InterpreterError($err.to_string()))
19    });
20    ($fmt:expr, $($arg:tt)*) => {
21        anyhow::Error::new($crate::errors::InterpreterError(format!($fmt, $($arg)*)))
22    };
23}
24
25/// Construct a new template error, as an anyhow::Error
26macro_rules! template_error {
27    ($err:expr $(,)?) => ({
28        anyhow::Error::new($crate::errors::TemplateError($err.to_string()))
29    });
30    ($fmt:expr, $($arg:tt)*) => {
31        anyhow::Error::new($crate::errors::TemplateError(format!($fmt, $($arg)*)))
32    };
33}
34
35/// Utility for asserting that an anyhow::Result contains a syntax error
36#[cfg(test)]
37macro_rules! assert_syntax_error {
38    ($left:expr, $right:expr) => ({
39        assert_eq!(
40            $left.expect_err("Expected an error, got").downcast_ref::<$crate::errors::SyntaxError>().expect("Expected a SyntaxError"),
41            &$crate::errors::SyntaxError($right.to_string())
42        );
43    });
44    ($left:expr, $right:expr,) => ({
45        assert_syntax_error!($left, $right)
46    });
47    ($left:expr, $right:expr, $($arg:tt)+) => ({
48        assert_eq!(
49            $left.expect_err("Expected an error, got").downcast_ref::<$crate::errors::SyntaxError>().expect("Expected a SyntaxError"),
50            &$crate::errors::SyntaxError($right.to_string()),
51            $($arg)*
52        );
53    });
54}
55
56/// Utility for asserting that an anyhow::Result contains an interpreter error
57#[cfg(test)]
58macro_rules! assert_interpreter_error {
59    ($left:expr, $right:expr) => ({
60        assert_eq!(
61            $left.expect_err("Expected an error, got").downcast_ref::<$crate::errors::InterpreterError>().expect("Expected a InterpreterError"),
62            &$crate::errors::InterpreterError($right.to_string())
63        );
64    });
65    ($left:expr, $right:expr,) => ({
66        assert_interpreter_error!($left, $right)
67    });
68    ($left:expr, $right:expr, $($arg:tt)+) => ({
69        assert_eq!(
70            $left.expect_err("Expected an error, got").downcast_ref::<$crate::errors::InterpreterError>().expect("Expected a InterpreterError"),
71            &$crate::errors::InterpreterError($right.to_string()),
72            $($arg)*
73        );
74    });
75}
76
77/// Utility for asserting that an anyhow::Result contains an template error
78#[cfg(test)]
79macro_rules! assert_template_error {
80    ($left:expr, $right:expr) => ({
81        assert_eq!(
82            $left.expect_err("Expected an error, got").downcast_ref::<$crate::errors::TemplateError>().expect("Expected a TemplateError"),
83            &$crate::errors::TemplateError($right.to_string())
84        );
85    });
86    ($left:expr, $right:expr,) => ({
87        assert_template_error!($left, $right)
88    });
89    ($left:expr, $right:expr, $($arg:tt)+) => ({
90        assert_eq!(
91            $left.expect_err("Expected an error, got").downcast_ref::<$crate::errors::TemplateError>().expect("Expected a TemplateError"),
92            &$crate::errors::TemplateError($right.to_string()),
93            $($arg)*
94        );
95    });
96}
97
98/// A SyntaxError indicates something wrong with the syntax of a JSON-e expression.
99#[derive(Debug, Error, Eq, PartialEq, Clone)]
100#[error("Syntax Error: {0}")]
101pub struct SyntaxError(pub(crate) String);
102
103/// An InterpreterError indicates something that failed during evaluation of a JSON-e expression.
104#[derive(Debug, Error, Eq, PartialEq, Clone)]
105#[error("Interpreter Error: {0}")]
106pub struct InterpreterError(pub(crate) String);
107
108/// An TemplateError indicates something that failed during evaluation of a JSON-e expression.
109#[derive(Debug, Error, Eq, PartialEq, Clone)]
110#[error("TemplateError: {0}")]
111pub struct TemplateError(pub(crate) String);