xml_doc/
error.rs

1use quick_xml::Error as XMLError;
2use std::{str::Utf8Error, string::FromUtf8Error};
3
4/// Wrapper around `std::Result`
5pub type Result<T> = std::result::Result<T, Error>;
6
7/// Error types
8#[derive(Debug)]
9pub enum Error {
10    /// [`std::io`] related error.
11    Io(std::io::Error),
12    /// Decoding related error.
13    /// Maybe the XML declaration has an encoding value that it doesn't recognize,
14    /// or it doesn't match its actual encoding,
15    CannotDecode,
16    /// Assorted errors while parsing XML.
17    MalformedXML(String),
18    /// The container element cannot have a parent.
19    /// Use `element.is_container()` to check if it is a container before
20    /// assigning it to another parent.
21    ContainerCannotMove,
22    /// You need to call `element.detatch()` before assigning another parent.
23    HasAParent,
24}
25
26impl From<XMLError> for Error {
27    fn from(err: XMLError) -> Error {
28        match err {
29            XMLError::EndEventMismatch { expected, found } => Error::MalformedXML(format!(
30                "Closing tag mismatch. Expected {}, found {}",
31                expected, found,
32            )),
33            XMLError::Io(err) => Error::Io(err),
34            XMLError::Utf8(_) => Error::CannotDecode,
35            err => Error::MalformedXML(format!("{:?}", err)),
36        }
37    }
38}
39
40impl From<FromUtf8Error> for Error {
41    fn from(_: FromUtf8Error) -> Error {
42        Error::CannotDecode
43    }
44}
45impl From<Utf8Error> for Error {
46    fn from(_: Utf8Error) -> Error {
47        Error::CannotDecode
48    }
49}
50
51impl From<std::io::Error> for Error {
52    fn from(err: std::io::Error) -> Error {
53        Error::Io(err)
54    }
55}