yaml_schema/
error.rs

1use thiserror::Error;
2
3/// Unexpected errors that can occur during the validation of a YAML schema
4#[derive(Debug, Error)]
5pub enum Error {
6    #[error("Not yet implemented!")]
7    NotYetImplemented,
8    #[error("IO error: {0}")]
9    IOError(#[from] std::io::Error),
10    #[error("File not found: {0}")]
11    FileNotFound(String),
12    #[error("YAML parsing error: {0}")]
13    YamlParsingError(#[from] saphyr::ScanError),
14    #[error("Float parsing error: {0}")]
15    FloatParsingError(#[from] std::num::ParseFloatError),
16    #[error("Regex parsing error: {0}")]
17    RegexParsingError(#[from] regex::Error),
18    #[error("Error loading schema: {0}")]
19    SchemaLoadingError(String),
20    #[error("Unsupported type '{0}'!")]
21    UnsupportedType(String),
22    #[error("Generic YAML schema error: {0}")]
23    GenericError(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}
35
36#[macro_export]
37macro_rules! fail_fast {
38    ($context:expr) => {
39        if $context.fail_fast {
40            return Err($crate::Error::FailFast);
41        }
42    };
43}
44
45#[macro_export]
46macro_rules! schema_loading_error {
47    ($s:literal, $($e:expr),+) => {
48        $crate::Error::SchemaLoadingError(format!($s, $($e),+))
49    };
50    ($s:literal) => {
51        $crate::Error::SchemaLoadingError($s.to_string())
52    };
53}
54
55#[macro_export]
56macro_rules! unsupported_type {
57    ($s:literal, $($e:expr),+) => {
58        $crate::Error::UnsupportedType(format!($s, $($e),+))
59    };
60    ($e:expr) => {
61        $crate::Error::UnsupportedType($e)
62    };
63}
64
65#[macro_export]
66macro_rules! generic_error {
67    ($s:literal, $($e:expr),+) => {
68        $crate::Error::GenericError(format!($s, $($e),+))
69    };
70    ($s:literal) => {
71        $crate::Error::GenericError($s.to_string())
72    };
73}
74
75#[macro_export]
76macro_rules! expected_mapping {
77    ($marked_yaml:expr) => {
78        $crate::Error::ExpectedMapping(
79            $crate::utils::format_marker(&$marked_yaml.span.start),
80            format!("{:?}", $marked_yaml.data),
81        )
82    };
83}
84
85#[macro_export]
86macro_rules! expected_scalar {
87    ($s:literal, $($e:expr),+) => {
88        $crate::Error::ExpectedScalar(format!($s, $($e),+))
89    };
90    ($s:literal) => {
91        $crate::Error::ExpectedScalar($s.to_string())
92    };
93}
94
95#[macro_export]
96macro_rules! expected_type_is_string {
97    ($marked_yaml:expr) => {
98        $crate::Error::ExpectedTypeIsString(
99            $crate::utils::format_marker(&$marked_yaml.span.start),
100            format!("{:?}", $marked_yaml.data),
101        )
102    };
103}