1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::fmt;
use std::fmt::{Debug, Formatter};
use std::str::{Utf8Error};
use std::sync::Arc;
use std::io::Error as IoError;
use quick_xml::events::attributes::AttrError;
use quick_xml::{Error as QXMLError};
use quick_xml::utils::write_byte_string;

#[derive(Clone, Debug)]
pub enum Error {
    Io(Arc<IoError>),
    NonDecodable(Option<Utf8Error>),
    UnexpectedEof(String),
    EndEventMismatch {
        expected: String,
        found: String,
    },
    UnexpectedToken(String),
    UnexpectedBang(u8),
    TextNotFound,
    XmlDeclWithoutVersion(Option<String>),
    InvalidAttr(AttrError),
    EscapeError,
    UnknownPrefix(Vec<u8>),
    MissingElement(String)
}

impl From<QXMLError> for Error {
    fn from(value: QXMLError) -> Self {
      match value {
          QXMLError::Io(x) => Self::Io(x),
          QXMLError::NonDecodable(x) => Self::NonDecodable(x),
          QXMLError::UnexpectedEof(x) => Self::UnexpectedEof(x),
          QXMLError::EndEventMismatch { expected, found} => Self::EndEventMismatch {
              expected,
              found,
          },
          QXMLError::UnexpectedToken(x) => Self::UnexpectedToken(x),
          QXMLError::UnexpectedBang(x) => Self::UnexpectedBang(x),
          QXMLError::TextNotFound => Self::TextNotFound,
          QXMLError::XmlDeclWithoutVersion(x) => Self::XmlDeclWithoutVersion(x),
          QXMLError::InvalidAttr(x) => Self::InvalidAttr(x),
          QXMLError::EscapeError(_) =>Self::EscapeError,
          QXMLError::UnknownPrefix(x) => Self::UnknownPrefix(x),
      }
    }
}

impl From<AttrError> for Error {
    fn from(value: AttrError) -> Self {
       Self::InvalidAttr(value)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Error::Io(e) => write!(f, "I/O error: {}", e),
            Error::NonDecodable(None) => write!(f, "Malformed input, decoding impossible"),
            Error::NonDecodable(Some(e)) => write!(f, "Malformed UTF-8 input: {}", e),
            Error::UnexpectedEof(e) => write!(f, "Unexpected EOF during reading {}", e),
            Error::EndEventMismatch { expected, found } => {
                write!(f, "Expecting </{}> found </{}>", expected, found)
            }
            Error::UnexpectedToken(e) => write!(f, "Unexpected token '{}'", e),
            Error::UnexpectedBang(b) => write!(
                f,
                "Only Comment (`--`), CDATA (`[CDATA[`) and DOCTYPE (`DOCTYPE`) nodes can start with a '!', but symbol `{}` found",
                *b as char
            ),
            Error::TextNotFound => write!(f, "Cannot read text, expecting Event::Text"),
            Error::XmlDeclWithoutVersion(e) => write!(
                f,
                "XmlDecl must start with 'version' attribute, found {:?}",
                e
            ),
            Error::InvalidAttr(e) => write!(f, "error while parsing attribute: {}", e),
            Error::EscapeError => write!(f, "escape error"),
            Error::UnknownPrefix(prefix) => {
                f.write_str("Unknown namespace prefix '")?;
                write_byte_string(f, prefix)?;
                f.write_str("'")
            },
            Error::MissingElement(p) => write!(f, "Missing element {}", p),


        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Io(e) => Some(e),
            Error::NonDecodable(Some(e)) => Some(e),
            Error::InvalidAttr(e) => Some(e),
            _ => None,
        }
    }
}