use std::error::Error;
use std::fmt;
use super::ExecutionErrorKind;
use crate::ValidatorId;
#[must_use]
pub struct ExecutionError {
kind: ExecutionErrorKind,
path: super::ValidationPath,
rule_id: Option<ValidatorId>,
dependency: Option<&'static str>,
trusted_source: Option<Box<dyn Error + Send + Sync + 'static>>,
}
impl ExecutionError {
#[inline]
pub fn new(kind: ExecutionErrorKind) -> Self {
Self {
kind,
path: super::ValidationPath::root(),
rule_id: None,
dependency: None,
trusted_source: None,
}
}
#[inline]
pub const fn kind(&self) -> ExecutionErrorKind {
self.kind
}
#[inline]
pub const fn rule_id(&self) -> Option<ValidatorId> {
self.rule_id
}
#[inline]
pub const fn dependency(&self) -> Option<&'static str> {
self.dependency
}
#[must_use]
#[inline]
pub const fn path(&self) -> &super::ValidationPath {
&self.path
}
pub fn trusted_source(&self) -> Option<&(dyn Error + Send + Sync + 'static)> {
self.trusted_source.as_deref()
}
#[inline]
pub const fn with_rule(mut self, rule_id: ValidatorId) -> Self {
self.rule_id = Some(rule_id);
self
}
#[inline]
pub const fn with_dependency(mut self, dependency: &'static str) -> Self {
self.dependency = Some(dependency);
self
}
pub fn with_trusted_source<E>(mut self, source: E) -> Self
where
E: Error + Send + Sync + 'static,
{
self.trusted_source = Some(Box::new(source));
self
}
#[inline]
pub fn with_path(mut self, path: super::ValidationPath) -> Self {
self.path = path;
self
}
}
impl fmt::Debug for ExecutionError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("ExecutionError")
.field("kind", &self.kind)
.field("rule_id", &self.rule_id)
.field("has_dependency", &self.dependency.is_some())
.field("has_trusted_source", &self.trusted_source.is_some())
.finish_non_exhaustive()
}
}
impl fmt::Display for ExecutionError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "validation execution failed: {}", self.kind)
}
}
impl Error for ExecutionError {}