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