1use quick_xml::{escape::EscapeError, events::attributes::AttrError, Error as XMLError};
2use std::{str::Utf8Error, string::FromUtf8Error, sync::Arc};
3use thiserror::Error;
4
5pub 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#[derive(Debug, Error)]
22pub enum EditXMLError {
23 #[error("IO Error: {0}")]
25 Io(#[from] Arc<std::io::Error>),
26 #[error(transparent)]
30 CannotDecode(#[from] DecodeError),
31 #[error("Malformed XML: {0}")]
33 MalformedXML(#[from] MalformedReason),
34 #[error("Container element cannot move")]
38 ContainerCannotMove,
39 #[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::Io(err) => EditXMLError::Io(err),
56 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}