use std::fmt::{self, Write};
use sealed::sealed;
use super::ErrorShow;
#[derive(Debug)]
pub struct ValidateError {
pub field: &'static str,
pub path: String,
pub value: String,
pub cause: Option<Box<dyn ErrorShow + 'static>>,
pub message: Option<&'static str>,
pub expression: &'static str,
pub type_name: &'static str,
}
#[sealed]
impl crate::error::display::ErrorDisplay for ValidateError {
fn full_display(&self, w: &mut impl Write) -> fmt::Result {
if let Some(msg) = self.message {
writeln!(w, "Validate error: {}", msg)?;
} else {
writeln!(w, "Validate error:")?;
}
if let Some(cause) = &self.cause {
writeln!(w, "Cause: {}", cause)?;
}
writeln!(
w,
"Value: {}: {} = {}",
self.field, self.type_name, self.value
)?;
writeln!(w, "Validator: {}", self.expression)?;
writeln!(w, "Path: {}", self.path)?;
writeln!(w)?;
Ok(())
}
fn brief_display(&self, w: &mut impl Write) -> fmt::Result {
write!(w, "Validate error [{}]", self.path)?;
if let Some(msg) = self.message {
writeln!(w, ": {}", msg)?;
} else if let Some(cause) = &self.cause {
writeln!(w, ": {}", cause)?;
}
Ok(())
}
fn human_readable_display(&self, w: &mut impl Write) -> fmt::Result {
write!(w, "Validate: ")?;
if let Some(msg) = self.message {
writeln!(w, "{}", msg)?;
} else if let Some(cause) = &self.cause {
writeln!(w, "{}", cause)?;
} else {
writeln!(w, "Invalid value found.")?;
}
writeln!(w, "Backtrace:")?;
writeln!(w, " Value: {}", self.value)?;
writeln!(w, " Operation: {}", self.expression)?;
if let Some(cause) = &self.cause {
writeln!(w, " Error: {:?}", cause)?;
}
Ok(())
}
}