use crate::path::Path;
#[derive(Debug)]
pub struct DeserializationErrors(Vec<DeserializationError>);
impl From<Vec<DeserializationError>> for DeserializationErrors {
fn from(errors: Vec<DeserializationError>) -> Self {
DeserializationErrors(errors)
}
}
impl DeserializationErrors {
pub fn iter(&self) -> impl ExactSizeIterator<Item = &DeserializationError> {
self.0.iter()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl IntoIterator for DeserializationErrors {
type Item = DeserializationError;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl std::fmt::Display for DeserializationErrors {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Something went wrong during deserialization:")?;
for error in self.iter() {
writeln!(f, "- {error}")?;
}
Ok(())
}
}
impl std::error::Error for DeserializationErrors {}
#[derive(Debug)]
pub struct DeserializationError {
pub(crate) path: Option<Path>,
pub(crate) details: String,
}
impl DeserializationError {
pub fn message(&self) -> &str {
self.details.as_ref()
}
pub fn path(&self) -> Option<&Path> {
self.path.as_ref()
}
}
impl std::fmt::Display for DeserializationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(path) = &self.path {
if !path.is_empty() {
write!(f, "{}: ", path)?;
}
}
write!(f, "{}", self.details.trim())
}
}