fhirbolt_serde/xml/
error.rs

1//! Errors when (de)serializing to/from XML.
2
3use std::{
4    fmt::{self, Display},
5    str,
6};
7
8use serde::{de, ser};
9
10mod quick_xml {
11    pub use quick_xml::{events::attributes::AttrError, Error};
12}
13
14/// Alias for a Result with the error type [`fhirbolt::serde::xml::Error`](Error).
15pub type Result<T> = std::result::Result<T, Error>;
16
17/// This type represents all possible errors that can occur when serializing or deserializing XML data.
18#[derive(Debug)]
19pub enum Error {
20    /// Generic error message.
21    Message(String),
22    /// Error reading or writing XML.
23    InvalidXml(quick_xml::Error),
24    /// Invalid XML attribute.
25    InvalidXmlAttribute(quick_xml::AttrError),
26    /// Invalid XML version.
27    InvalidXmlVersion(String),
28    /// Invalid XML encoding.
29    InvalidXmlEncoding(String),
30    /// Invalid XML standalone.
31    InvalidXmlStandalone(String),
32    /// Invalid XML namespace.
33    InvalidXmlNamespace(Option<String>, String),
34    /// Unsupported XML event.
35    InvalidXmlEvent(&'static str),
36    /// Unexpected Fhir event.
37    InvalidFhirEvent {
38        found: &'static str,
39        expected: &'static str,
40    },
41    /// Error reading UTF8.
42    Utf8Error(str::Utf8Error),
43}
44
45impl ser::Error for Error {
46    fn custom<T: Display>(msg: T) -> Self {
47        Error::Message(msg.to_string())
48    }
49}
50
51impl de::Error for Error {
52    fn custom<T: Display>(msg: T) -> Self {
53        Error::Message(msg.to_string())
54    }
55}
56
57impl Display for Error {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        match self {
60            Error::Message(msg) => write!(f, "{}", msg),
61            Error::InvalidXml(e) => write!(f, "{}", e),
62            Error::InvalidXmlAttribute(e) => write!(f, "{}", e),
63            Error::InvalidXmlVersion(v) => {
64                write!(f, "invalid XML version '{}' (expected '1.0')", v)
65            }
66            Error::InvalidXmlEncoding(e) => {
67                write!(f, "invalid XML encoding '{}' (expected 'UTF-8')", e)
68            }
69            Error::InvalidXmlStandalone(s) => {
70                write!(f, "invalid XML standalone '{}' (expected 'no')", s)
71            }
72            Error::InvalidXmlNamespace(None, expected) => {
73                write!(f, "invalid XML unbound namespace (expected '{}')", expected)
74            }
75            Error::InvalidXmlNamespace(Some(ns), expected) => {
76                write!(f, "invalid XML namespace {} (expected '{}')", ns, expected)
77            }
78            Error::InvalidXmlEvent(e) => write!(f, "invalid XML event: {}", e),
79            Error::InvalidFhirEvent { found, expected } => {
80                write!(
81                    f,
82                    "invalid FHIR event: found {}, expected: {}",
83                    found, expected
84                )
85            }
86            Error::Utf8Error(e) => write!(f, "{}", e),
87        }
88    }
89}
90
91impl std::error::Error for Error {}
92
93impl From<quick_xml::Error> for Error {
94    fn from(e: quick_xml::Error) -> Self {
95        Self::InvalidXml(e)
96    }
97}
98
99impl From<quick_xml::AttrError> for Error {
100    fn from(e: quick_xml::AttrError) -> Self {
101        Self::InvalidXmlAttribute(e)
102    }
103}
104
105impl From<str::Utf8Error> for Error {
106    fn from(e: str::Utf8Error) -> Self {
107        Self::Utf8Error(e)
108    }
109}