Skip to main content

facet_dom/
error.rs

1//! Error types for DOM deserialization.
2
3use std::fmt;
4
5/// Error type for DOM deserialization.
6#[derive(Debug)]
7pub enum DomDeserializeError<E> {
8    /// Parser error.
9    Parser(E),
10
11    /// Reflection error.
12    Reflect(facet_reflect::ReflectError),
13
14    /// Allocation error.
15    Alloc(facet_reflect::AllocError),
16
17    /// Shape mismatch error.
18    ShapeMismatch(facet_reflect::ShapeMismatchError),
19
20    /// Unexpected end of input.
21    UnexpectedEof {
22        /// What was expected.
23        expected: &'static str,
24    },
25
26    /// Type mismatch.
27    TypeMismatch {
28        /// What was expected.
29        expected: &'static str,
30        /// What was found.
31        got: String,
32    },
33
34    /// Unknown element.
35    UnknownElement {
36        /// The element tag name.
37        tag: String,
38    },
39
40    /// Unknown attribute (when deny_unknown_fields is set).
41    UnknownAttribute {
42        /// The attribute name.
43        name: String,
44    },
45
46    /// Missing required attribute.
47    MissingAttribute {
48        /// The attribute name.
49        name: &'static str,
50    },
51
52    /// Unsupported type.
53    Unsupported(String),
54}
55
56impl<E> From<facet_reflect::ReflectError> for DomDeserializeError<E> {
57    fn from(e: facet_reflect::ReflectError) -> Self {
58        crate::trace!("🚨 ReflectError -> DomDeserializeError: {e}");
59        Self::Reflect(e)
60    }
61}
62
63impl<E> From<facet_reflect::AllocError> for DomDeserializeError<E> {
64    fn from(e: facet_reflect::AllocError) -> Self {
65        crate::trace!("🚨 AllocError -> DomDeserializeError: {e}");
66        Self::Alloc(e)
67    }
68}
69
70impl<E> From<facet_reflect::ShapeMismatchError> for DomDeserializeError<E> {
71    fn from(e: facet_reflect::ShapeMismatchError) -> Self {
72        crate::trace!("🚨 ShapeMismatchError -> DomDeserializeError: {e}");
73        Self::ShapeMismatch(e)
74    }
75}
76
77impl<E> From<facet_dessert::DessertError> for DomDeserializeError<E> {
78    fn from(e: facet_dessert::DessertError) -> Self {
79        crate::trace!("🚨 DessertError -> DomDeserializeError: {e}");
80        match e {
81            facet_dessert::DessertError::Reflect { error, .. } => Self::Reflect(error),
82            facet_dessert::DessertError::CannotBorrow { message } => {
83                Self::Unsupported(message.into_owned())
84            }
85        }
86    }
87}
88
89impl<E: std::error::Error> fmt::Display for DomDeserializeError<E> {
90    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        match self {
92            Self::Parser(e) => write!(f, "parser error: {e}"),
93            Self::Reflect(e) => write!(f, "reflection error: {e}"),
94            Self::Alloc(e) => write!(f, "allocation error: {e}"),
95            Self::ShapeMismatch(e) => write!(f, "shape mismatch: {e}"),
96            Self::UnexpectedEof { expected } => write!(f, "unexpected EOF, expected {expected}"),
97            Self::TypeMismatch { expected, got } => {
98                write!(f, "type mismatch: expected {expected}, got {got}")
99            }
100            Self::UnknownElement { tag } => write!(f, "unknown element: <{tag}>"),
101            Self::UnknownAttribute { name } => write!(f, "unknown attribute: {name}"),
102            Self::MissingAttribute { name } => write!(f, "missing required attribute: {name}"),
103            Self::Unsupported(msg) => write!(f, "unsupported: {msg}"),
104        }
105    }
106}
107
108impl<E: std::error::Error + 'static> std::error::Error for DomDeserializeError<E> {
109    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
110        match self {
111            Self::Parser(e) => Some(e),
112            Self::Reflect(e) => Some(e),
113            Self::Alloc(e) => Some(e),
114            Self::ShapeMismatch(e) => Some(e),
115            _ => None,
116        }
117    }
118}