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 than one colon
36 InvalidElement,
37
38 /// An error which is returned when a comment is to be parsed by minidom
39 #[cfg(not(comments))]
40 CommentsDisabled,
41}
42
43impl StdError for Error {
44 fn cause(&self) -> Option<&dyn StdError> {
45 match self {
46 Error::XmlError(e) => Some(e),
47 Error::Utf8Error(e) => Some(e),
48 Error::IoError(e) => Some(e),
49 Error::EndOfDocument => None,
50 Error::InvalidElementClosed => None,
51 Error::InvalidElement => None,
52 #[cfg(not(comments))]
53 Error::CommentsDisabled => None,
54 }
55 }
56}
57
58impl std::fmt::Display for Error {
59 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
60 match self {
61 Error::XmlError(e) => write!(fmt, "XML error: {}", e),
62 Error::Utf8Error(e) => write!(fmt, "UTF-8 error: {}", e),
63 Error::IoError(e) => write!(fmt, "IO error: {}", e),
64 Error::EndOfDocument => {
65 write!(fmt, "the end of the document has been reached prematurely")
66 }
67 Error::InvalidElementClosed => {
68 write!(fmt, "the XML is invalid, an element was wrongly closed")
69 }
70 Error::InvalidElement => write!(fmt, "the XML element is invalid"),
71 #[cfg(not(comments))]
72 Error::CommentsDisabled => write!(
73 fmt,
74 "a comment has been found even though comments are disabled by feature"
75 ),
76 }
77 }
78}
79
80impl From<::quick_xml::Error> for Error {
81 fn from(err: ::quick_xml::Error) -> Error {
82 Error::XmlError(err)
83 }
84}
85
86impl From<::std::str::Utf8Error> for Error {
87 fn from(err: ::std::str::Utf8Error) -> Error {
88 Error::Utf8Error(err)
89 }
90}
91
92impl From<::std::io::Error> for Error {
93 fn from(err: ::std::io::Error) -> Error {
94 Error::IoError(err)
95 }
96}
97
98/// Our simplified Result type.
99pub type Result<T> = ::std::result::Result<T, Error>;