use crate::traits::PublisherError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ErrorKind {
Parse,
Coercion,
Content,
MissingRequired,
TypeMismatch,
Enum,
}
impl ErrorKind {
pub(super) fn as_str(self) -> &'static str {
match self {
ErrorKind::Parse => "parse",
ErrorKind::Coercion => "coercion",
ErrorKind::Content => "content",
ErrorKind::MissingRequired => "missing_required",
ErrorKind::TypeMismatch => "type_mismatch",
ErrorKind::Enum => "enum",
}
}
}
#[derive(Debug)]
pub(super) struct TransformError {
pub(super) path: String,
pub(super) kind: ErrorKind,
pub(super) detail: String,
}
impl TransformError {
pub(super) fn new(path: String, kind: ErrorKind, detail: impl Into<String>) -> Self {
Self {
path,
kind,
detail: detail.into(),
}
}
}
impl std::fmt::Display for TransformError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"transform failed at {} [{}]: {}",
self.path,
self.kind.as_str(),
self.detail
)
}
}
impl std::error::Error for TransformError {}
impl From<TransformError> for PublisherError {
fn from(err: TransformError) -> Self {
PublisherError::NonRetryable(anyhow::Error::new(err))
}
}