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