1use std::fmt;
4
5#[derive(Debug)]
7pub enum DomDeserializeError<E> {
8 Parser(E),
10
11 Reflect(facet_reflect::ReflectError),
13
14 Alloc(facet_reflect::AllocError),
16
17 ShapeMismatch(facet_reflect::ShapeMismatchError),
19
20 UnexpectedEof {
22 expected: &'static str,
24 },
25
26 TypeMismatch {
28 expected: &'static str,
30 got: String,
32 },
33
34 UnknownElement {
36 tag: String,
38 },
39
40 UnknownAttribute {
42 name: String,
44 },
45
46 MissingAttribute {
48 name: &'static str,
50 },
51
52 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}