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    /// Unexpected end of input.
15    UnexpectedEof {
16        /// What was expected.
17        expected: &'static str,
18    },
19
20    /// Type mismatch.
21    TypeMismatch {
22        /// What was expected.
23        expected: &'static str,
24        /// What was found.
25        got: String,
26    },
27
28    /// Unknown element.
29    UnknownElement {
30        /// The element tag name.
31        tag: String,
32    },
33
34    /// Unknown attribute (when deny_unknown_fields is set).
35    UnknownAttribute {
36        /// The attribute name.
37        name: String,
38    },
39
40    /// Missing required attribute.
41    MissingAttribute {
42        /// The attribute name.
43        name: &'static str,
44    },
45
46    /// Unsupported type.
47    Unsupported(String),
48}
49
50impl<E> From<facet_reflect::ReflectError> for DomDeserializeError<E> {
51    fn from(e: facet_reflect::ReflectError) -> Self {
52        crate::trace!("🚨 ReflectError -> DomDeserializeError: {e}");
53        Self::Reflect(e)
54    }
55}
56
57impl<E> From<facet_dessert::DessertError> for DomDeserializeError<E> {
58    fn from(e: facet_dessert::DessertError) -> Self {
59        crate::trace!("🚨 DessertError -> DomDeserializeError: {e}");
60        match e {
61            facet_dessert::DessertError::Reflect { error, .. } => Self::Reflect(error),
62            facet_dessert::DessertError::CannotBorrow { message } => {
63                Self::Unsupported(message.into_owned())
64            }
65        }
66    }
67}
68
69impl<E: std::error::Error> fmt::Display for DomDeserializeError<E> {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        match self {
72            Self::Parser(e) => write!(f, "parser error: {e}"),
73            Self::Reflect(e) => write!(f, "reflection error: {e}"),
74            Self::UnexpectedEof { expected } => write!(f, "unexpected EOF, expected {expected}"),
75            Self::TypeMismatch { expected, got } => {
76                write!(f, "type mismatch: expected {expected}, got {got}")
77            }
78            Self::UnknownElement { tag } => write!(f, "unknown element: <{tag}>"),
79            Self::UnknownAttribute { name } => write!(f, "unknown attribute: {name}"),
80            Self::MissingAttribute { name } => write!(f, "missing required attribute: {name}"),
81            Self::Unsupported(msg) => write!(f, "unsupported: {msg}"),
82        }
83    }
84}
85
86impl<E: std::error::Error + 'static> std::error::Error for DomDeserializeError<E> {
87    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
88        match self {
89            Self::Parser(e) => Some(e),
90            Self::Reflect(e) => Some(e),
91            _ => None,
92        }
93    }
94}