use crate::violation::Violation;
use ion_rs::IonError;
use std::io;
use thiserror::Error;
pub type IonSchemaResult<T> = Result<T, IonSchemaError>;
pub type ValidationResult = Result<(), Violation>;
#[derive(Debug, Error)]
pub enum IonSchemaError {
#[error("{source:?}")]
IoError {
#[from]
source: io::Error,
},
#[error("{description}")]
UnresolvableSchemaError { description: String },
#[error("{description}")]
InvalidSchemaError { description: String },
#[error("{source:?}")]
IonError {
#[from]
source: IonError,
},
}
impl PartialEq for IonSchemaError {
fn eq(&self, other: &Self) -> bool {
use IonSchemaError::*;
match (self, other) {
(IoError { source: s1 }, IoError { source: s2 }) => s1.kind() == s2.kind(),
(
UnresolvableSchemaError { description: s1 },
UnresolvableSchemaError { description: s2 },
) => s1 == s2,
(InvalidSchemaError { description: s1 }, InvalidSchemaError { description: s2 }) => {
s1 == s2
}
(IonError { source: s1 }, IonError { source: s2 }) => s1 == s2,
_ => false,
}
}
}
pub fn unresolvable_schema_error<T, S: AsRef<str>>(description: S) -> IonSchemaResult<T> {
Err(IonSchemaError::UnresolvableSchemaError {
description: description.as_ref().to_string(),
})
}
pub fn invalid_schema_error_raw<S: AsRef<str>>(description: S) -> IonSchemaError {
IonSchemaError::InvalidSchemaError {
description: description.as_ref().to_string(),
}
}
pub fn invalid_schema_error<T, S: AsRef<str>>(description: S) -> IonSchemaResult<T> {
Err(IonSchemaError::InvalidSchemaError {
description: description.as_ref().to_string(),
})
}
pub fn unresolvable_schema_error_raw<S: AsRef<str>>(description: S) -> IonSchemaError {
IonSchemaError::UnresolvableSchemaError {
description: description.as_ref().to_string(),
}
}
#[macro_export]
macro_rules! isl_require {
($expression:expr => $fmt_string:literal $(, $($tt:tt)*)?) => {
if ($expression) {
Ok(())
} else {
Err($crate::result::IonSchemaError::InvalidSchemaError {
description: format!($fmt_string),
})
}
};
}