1use crate::violation::Violation;
7use ion_rs::IonError;
8use std::io;
9use thiserror::Error;
10
11pub type IonSchemaResult<T> = Result<T, IonSchemaError>;
13
14pub type ValidationResult = Result<(), Violation>;
16
17#[derive(Debug, Error)]
19pub enum IonSchemaError {
20 #[error("{source:?}")]
22 IoError {
23 #[from]
24 source: io::Error,
25 },
26
27 #[error("{description}")]
29 UnresolvableSchemaError { description: String },
30
31 #[error("{description}")]
33 InvalidSchemaError { description: String },
34
35 #[error("{source:?}")]
37 IonError {
38 #[from]
39 source: IonError,
40 },
41}
42
43impl PartialEq for IonSchemaError {
45 fn eq(&self, other: &Self) -> bool {
46 use IonSchemaError::*;
47 match (self, other) {
48 (IoError { source: s1 }, IoError { source: s2 }) => s1.kind() == s2.kind(),
50 (
51 UnresolvableSchemaError { description: s1 },
52 UnresolvableSchemaError { description: s2 },
53 ) => s1 == s2,
54 (InvalidSchemaError { description: s1 }, InvalidSchemaError { description: s2 }) => {
55 s1 == s2
56 }
57 (IonError { source: s1 }, IonError { source: s2 }) => s1 == s2,
58 _ => false,
59 }
60 }
61}
62
63pub fn unresolvable_schema_error<T, S: AsRef<str>>(description: S) -> IonSchemaResult<T> {
66 Err(IonSchemaError::UnresolvableSchemaError {
67 description: description.as_ref().to_string(),
68 })
69}
70
71pub fn invalid_schema_error_raw<S: AsRef<str>>(description: S) -> IonSchemaError {
74 IonSchemaError::InvalidSchemaError {
75 description: description.as_ref().to_string(),
76 }
77}
78
79pub fn invalid_schema_error<T, S: AsRef<str>>(description: S) -> IonSchemaResult<T> {
82 Err(IonSchemaError::InvalidSchemaError {
83 description: description.as_ref().to_string(),
84 })
85}
86
87pub fn unresolvable_schema_error_raw<S: AsRef<str>>(description: S) -> IonSchemaError {
90 IonSchemaError::UnresolvableSchemaError {
91 description: description.as_ref().to_string(),
92 }
93}
94
95#[macro_export]
99macro_rules! isl_require {
100 ($expression:expr => $fmt_string:literal $(, $($tt:tt)*)?) => {
101 if ($expression) {
102 Ok(())
103 } else {
104 Err($crate::result::IonSchemaError::InvalidSchemaError {
105 description: format!($fmt_string),
106 })
107 }
108 };
109}