xml_builder/
xmlerror.rs

1use std::error::Error;
2use std::fmt::{Debug, Display};
3
4/// Custom `Result` type thrown by this crate.
5pub type Result<T> = std::result::Result<T, XMLError>;
6
7/// Error type thrown by this crate
8pub enum XMLError {
9    /// Thrown when the given element cannot be inserted into the XML object tree.
10    InsertError(String),
11    /// Thrown when the given `Writer` cannot be written to.
12    IOError(String),
13}
14
15impl From<std::io::Error> for XMLError {
16    fn from(e: std::io::Error) -> Self {
17        XMLError::IOError(e.to_string())
18    }
19}
20
21impl Debug for XMLError {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            XMLError::InsertError(e) => write!(f, "Error encountered during insertion: {}", e),
25            XMLError::IOError(e) => write!(f, "Error encountered during write: {}", e),
26        }
27    }
28}
29
30impl Display for XMLError {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        write!(f, "{self:?}")
33    }
34}
35
36impl Error for XMLError {}