use std::result::Result;
use thiserror::Error;
use crate::parser;
#[allow(missing_docs)]
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum ValidateError {
#[error(transparent)]
ParseError(#[from] parser::ParseError),
#[error("Structural({0})")]
Structural(String),
#[error("Mismatch(expected {})", .0.expected)]
Mismatch(Mismatch),
#[error("Mismatch(expected {})", .0.expected)]
MapCut(Mismatch),
#[error("MissingRule({0})")]
MissingRule(String),
#[error("Unsupported {0}")]
Unsupported(String),
#[error("ValueError({0})")]
ValueError(String),
#[error("GenericError")]
GenericError,
}
impl ValidateError {
pub(crate) fn is_fatal(&self) -> bool {
!matches!(self, ValidateError::Mismatch(_) | ValidateError::MapCut(_))
}
pub(crate) fn erase_mapcut(self) -> ValidateError {
match self {
ValidateError::MapCut(m) => ValidateError::Mismatch(m),
_ => self,
}
}
pub(crate) fn is_mismatch(&self) -> bool {
matches!(self, ValidateError::Mismatch(_))
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct Mismatch {
expected: String,
}
#[doc(hidden)]
pub fn mismatch<E: Into<String>>(expected: E) -> ValidateError {
ValidateError::Mismatch(Mismatch {
expected: expected.into(),
})
}
pub type ValidateResult = Result<(), ValidateError>;
#[doc(hidden)]
pub trait ErrorMatch {
fn err_mismatch(&self);
fn err_missing_rule(&self);
fn err_generic(&self);
fn err_parse(&self);
fn err_structural(&self);
}
impl ErrorMatch for ValidateResult {
#[track_caller]
fn err_mismatch(&self) {
match self {
Err(ValidateError::Mismatch(_)) => (),
_ => panic!("expected Mismatch, got {:?}", self),
}
}
#[track_caller]
fn err_missing_rule(&self) {
match self {
Err(ValidateError::MissingRule(_)) => (),
_ => panic!("expected MissingRule, got {:?}", self),
}
}
#[track_caller]
fn err_generic(&self) {
match self {
Err(ValidateError::GenericError) => (),
_ => panic!("expected GenericError, got {:?}", self),
}
}
#[track_caller]
fn err_parse(&self) {
match self {
Err(ValidateError::ParseError(_)) => (),
_ => panic!("expected ParseError, got {:?}", self),
}
}
#[track_caller]
fn err_structural(&self) {
match self {
Err(ValidateError::Structural(_)) => (),
_ => panic!("expected Structural, got {:?}", self),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_extras() {
let e: ValidateError = mismatch("");
assert!(!e.is_fatal());
let e = ValidateError::Structural("".into());
assert!(e.is_fatal());
}
}