edit_xml/
error.rs

1use quick_xml::{escape::EscapeError, events::attributes::AttrError, Error as XMLError};
2use std::{str::Utf8Error, string::FromUtf8Error, sync::Arc};
3use thiserror::Error;
4
5/// Wrapper around `std::Result`
6pub type Result<T> = std::result::Result<T, EditXMLError>;
7#[derive(Debug, Error)]
8pub enum MalformedReason {
9    #[error("Missing XML Declaration. Expected `<?xml version=\"1.0\" encoding=\"UTF-8\"?>`")]
10    MissingDeclaration,
11    #[error("Unexpected Item {0}")]
12    UnexpectedItem(&'static str),
13    #[error("Malformed Element Tree")]
14    GenericMalformedTree,
15    #[error("Standalone Document should be yes or no")]
16    InvalidStandAloneValue,
17    #[error("Missing closing tag")]
18    MissingClosingTag,
19}
20/// Error types
21#[derive(Debug, Error)]
22pub enum EditXMLError {
23    /// [`std::io`] related error.
24    #[error("IO Error: {0}")]
25    Io(#[from] Arc<std::io::Error>),
26    /// Decoding related error.
27    /// Maybe the XML declaration has an encoding value that it doesn't recognize,
28    /// or it doesn't match its actual encoding,
29    #[error(transparent)]
30    CannotDecode(#[from] DecodeError),
31    /// Assorted errors while parsing XML.
32    #[error("Malformed XML: {0}")]
33    MalformedXML(#[from] MalformedReason),
34    /// The container element cannot have a parent.
35    /// Use `element.is_container()` to check if it is a container before
36    /// assigning it to another parent.
37    #[error("Container element cannot move")]
38    ContainerCannotMove,
39    /// You need to call `element.detach()` before assigning another parent.
40    #[error("Element already has a parent. Call detach() before changing parent.")]
41    HasAParent,
42    #[error("Attribute Error {0}")]
43    AttrError(#[from] AttrError),
44    #[error("{0}")]
45    OtherXML(#[source] XMLError),
46}
47
48impl From<XMLError> for EditXMLError {
49    fn from(err: XMLError) -> EditXMLError {
50        match err {
51            //XMLError::EndEventMismatch { expected, found } => Error::MalformedXML(format!(
52            //    "Closing tag mismatch. Expected {}, found {}",
53            //    expected, found,
54            //)),
55            XMLError::Io(err) => EditXMLError::Io(err),
56            // TODO XMLError::(_) => Error::CannotDecode,
57            err => EditXMLError::OtherXML(err),
58        }
59    }
60}
61macro_rules! decode_error_proxy {
62    (
63        $($ty:ty),*
64    ) => {
65        $(
66            impl From<$ty> for EditXMLError {
67                fn from(error: $ty) -> EditXMLError {
68                    DecodeError::from(error).into()
69                }
70            }
71        )*
72    };
73}
74decode_error_proxy!(Utf8Error, FromUtf8Error, EscapeError);
75
76impl From<std::io::Error> for EditXMLError {
77    fn from(err: std::io::Error) -> EditXMLError {
78        EditXMLError::Io(Arc::new(err))
79    }
80}
81#[derive(Debug, Error)]
82pub enum DecodeError {
83    #[error("Cannot decode String from UTF-8 {0}")]
84    UTF8(#[from] Utf8Error),
85    #[error("Cannot decode String from UTF-8 {0}")]
86    FromUTF8(#[from] FromUtf8Error),
87    #[error("Cannot decode XML")]
88    Other,
89    #[error("Cannot decode XML")]
90    MissingEncoding,
91    #[error(transparent)]
92    EscapeError(#[from] EscapeError),
93}