minidom/
error.rs

1// Copyright (c) 2020 lumi <lumi@pew.im>
2// Copyright (c) 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
3// Copyright (c) 2020 Bastien Orivel <eijebong+minidom@bananium.fr>
4// Copyright (c) 2020 Astro <astro@spaceboyz.net>
5// Copyright (c) 2020 Maxime “pep” Buquet <pep@bouah.net>
6// Copyright (c) 2020 Matt Bilker <me@mbilker.us>
7//
8// This Source Code Form is subject to the terms of the Mozilla Public
9// License, v. 2.0. If a copy of the MPL was not distributed with this
10// file, You can obtain one at http://mozilla.org/MPL/2.0/.
11
12//! Provides an error type for this crate.
13
14use std::convert::From;
15use std::error::Error as StdError;
16
17/// Our main error type.
18#[derive(Debug)]
19pub enum Error {
20    /// An error from quick_xml.
21    XmlError(::quick_xml::Error),
22
23    /// An UTF-8 conversion error.
24    Utf8Error(::std::str::Utf8Error),
25
26    /// An I/O error, from std::io.
27    IoError(::std::io::Error),
28
29    /// An error which is returned when the end of the document was reached prematurely.
30    EndOfDocument,
31
32    /// An error which is returned when an element is closed when it shouldn't be
33    InvalidElementClosed,
34
35    /// An error which is returned when an elemet's name contains more colons than permitted
36    InvalidElement,
37
38    /// An error which is returned when an element being serialized doesn't contain a prefix
39    /// (be it None or Some(_)).
40    InvalidPrefix,
41
42    /// An error which is returned when an element doesn't contain a namespace
43    MissingNamespace,
44
45    /// An error which is returned when a prefixed is defined twice
46    DuplicatePrefix,
47}
48
49impl StdError for Error {
50    fn cause(&self) -> Option<&dyn StdError> {
51        match self {
52            Error::XmlError(e) => Some(e),
53            Error::Utf8Error(e) => Some(e),
54            Error::IoError(e) => Some(e),
55            Error::EndOfDocument => None,
56            Error::InvalidElementClosed => None,
57            Error::InvalidElement => None,
58            Error::InvalidPrefix => None,
59            Error::MissingNamespace => None,
60            Error::DuplicatePrefix => None,
61        }
62    }
63}
64
65impl std::fmt::Display for Error {
66    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
67        match self {
68            Error::XmlError(e) => write!(fmt, "XML error: {}", e),
69            Error::Utf8Error(e) => write!(fmt, "UTF-8 error: {}", e),
70            Error::IoError(e) => write!(fmt, "IO error: {}", e),
71            Error::EndOfDocument => {
72                write!(fmt, "the end of the document has been reached prematurely")
73            }
74            Error::InvalidElementClosed => {
75                write!(fmt, "the XML is invalid, an element was wrongly closed")
76            }
77            Error::InvalidElement => write!(fmt, "the XML element is invalid"),
78            Error::InvalidPrefix => write!(fmt, "the prefix is invalid"),
79            Error::MissingNamespace => write!(fmt, "the XML element is missing a namespace",),
80            Error::DuplicatePrefix => write!(fmt, "the prefix is already defined"),
81        }
82    }
83}
84
85impl From<::quick_xml::Error> for Error {
86    fn from(err: ::quick_xml::Error) -> Error {
87        Error::XmlError(err)
88    }
89}
90
91impl From<::quick_xml::events::attributes::AttrError> for Error {
92    fn from(err: ::quick_xml::events::attributes::AttrError) -> Error {
93        Error::XmlError(err.into())
94    }
95}
96
97impl From<::std::str::Utf8Error> for Error {
98    fn from(err: ::std::str::Utf8Error) -> Error {
99        Error::Utf8Error(err)
100    }
101}
102
103impl From<::std::io::Error> for Error {
104    fn from(err: ::std::io::Error) -> Error {
105        Error::IoError(err)
106    }
107}
108
109/// Our simplified Result type.
110pub type Result<T> = ::std::result::Result<T, Error>;