1use thiserror::Error;
2
3use crate::loader::UrlLoadError;
4
5#[derive(Debug, Error)]
7pub enum Error {
8 #[error("Generic YAML schema error: {0}")]
9 GenericError(String),
10 #[error(transparent)]
11 IOError(#[from] std::io::Error),
12 #[error("File not found: {0}")]
13 FileNotFound(String),
14 #[error(transparent)]
15 YamlParsingError(#[from] saphyr::ScanError),
16 #[error(transparent)]
17 FloatParsingError(#[from] std::num::ParseFloatError),
18 #[error(transparent)]
19 RegexParsingError(#[from] regex::Error),
20 #[error("Error loading schema: {0}")]
21 SchemaLoadingError(String),
22 #[error("Unsupported type: {0}")]
23 UnsupportedType(String),
24 #[error("{0} Expected mapping, but got: {1}")]
25 ExpectedMapping(String, String),
26 #[error("Expected YAML scalar: {0}")]
27 ExpectedScalar(String),
28 #[error("{0} Expected a string value for `type:`, but got: {1}")]
29 ExpectedTypeIsString(String, String),
30 #[error("Fail fast signal")]
31 FailFast,
32 #[error("Invalid regular expression: {0}")]
33 InvalidRegularExpression(String),
34 #[error(transparent)]
35 UrlLoadError(#[from] UrlLoadError),
36 #[error("Circular $ref detected: {0}")]
37 CircularReference(String),
38 #[error(transparent)]
39 JsonPtrError(#[from] jsonptr::ParseError),
40 #[error("Not yet implemented!")]
41 NotYetImplemented,
42}
43
44#[macro_export]
45macro_rules! fail_fast {
46 ($context:expr) => {
47 if $context.fail_fast {
48 return Err($crate::Error::FailFast);
49 }
50 };
51}
52
53#[macro_export]
54macro_rules! schema_loading_error {
55 ($s:literal, $($e:expr),+) => {
56 $crate::Error::SchemaLoadingError(format!($s, $($e),+))
57 };
58 ($s:literal) => {
59 $crate::Error::SchemaLoadingError($s.to_string())
60 };
61}
62
63#[macro_export]
64macro_rules! unsupported_type {
65 ($s:literal, $($e:expr),+) => {
66 $crate::Error::UnsupportedType(format!($s, $($e),+))
67 };
68 ($e:expr) => {
69 $crate::Error::UnsupportedType($e.to_string())
70 };
71}
72
73#[macro_export]
74macro_rules! generic_error {
75 ($s:literal, $($e:expr),+) => {
76 $crate::Error::GenericError(format!($s, $($e),+))
77 };
78 ($s:literal) => {
79 $crate::Error::GenericError($s.to_string())
80 };
81}
82
83#[macro_export]
84macro_rules! expected_mapping {
85 ($marked_yaml:expr) => {
86 $crate::Error::ExpectedMapping(
87 $crate::utils::format_marker(&$marked_yaml.span.start),
88 format!("{:?}", $marked_yaml.data),
89 )
90 };
91}
92
93#[macro_export]
94macro_rules! expected_scalar {
95 ($s:literal, $($e:expr),+) => {
96 $crate::Error::ExpectedScalar(format!($s, $($e),+))
97 };
98 ($s:literal) => {
99 $crate::Error::ExpectedScalar($s.to_string())
100 };
101}
102
103#[macro_export]
104macro_rules! expected_type_is_string {
105 ($marked_yaml:expr) => {
106 $crate::Error::ExpectedTypeIsString(
107 $crate::utils::format_marker(&$marked_yaml.span.start),
108 format!("{:?}", $marked_yaml.data),
109 )
110 };
111}
112
113#[macro_export]
114macro_rules! circular_reference {
115 ($s:literal, $($e:expr),+) => {
116 $crate::Error::CircularReference(format!($s, $($e),+))
117 };
118 ($s:literal) => {
119 $crate::Error::CircularReference($s.to_string())
120 };
121}