use std::error::Error;
use std::fmt;
use super::BindErrorKind;
use crate::ValidatorId;
#[must_use]
pub struct BindError {
kind: BindErrorKind,
rule_id: Option<ValidatorId>,
parameter: Option<String>,
dependency: Option<String>,
}
impl BindError {
#[inline]
pub fn new(kind: BindErrorKind) -> Self {
Self {
kind,
rule_id: None,
parameter: None,
dependency: None,
}
}
#[inline]
pub const fn kind(&self) -> BindErrorKind {
self.kind
}
#[inline]
pub const fn rule_id(&self) -> Option<ValidatorId> {
self.rule_id
}
#[inline]
pub fn parameter(&self) -> Option<&str> {
self.parameter.as_deref()
}
#[inline]
pub fn dependency(&self) -> Option<&str> {
self.dependency.as_deref()
}
#[inline]
pub const fn with_rule(mut self, rule_id: ValidatorId) -> Self {
self.rule_id = Some(rule_id);
self
}
pub fn with_parameter(mut self, parameter: impl Into<String>) -> Self {
self.parameter = Some(parameter.into());
self
}
pub fn with_dependency(mut self, dependency: impl Into<String>) -> Self {
self.dependency = Some(dependency.into());
self
}
}
impl fmt::Debug for BindError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("BindError")
.field("kind", &self.kind)
.field("rule_id", &self.rule_id)
.field("has_parameter", &self.parameter.is_some())
.field("has_dependency", &self.dependency.is_some())
.finish()
}
}
impl fmt::Display for BindError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "validation binding failed: {}", self.kind)
}
}
impl Error for BindError {}