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 // TODO: return Some(e) for this case after the merge of
47 // https://github.com/tafia/quick-xml/pull/170
48 Error::XmlError(_e) => None,
49 Error::Utf8Error(e) => Some(e),
50 Error::IoError(e) => Some(e),
51 Error::EndOfDocument => None,
52 Error::InvalidElementClosed => None,
53 Error::InvalidElement => None,
54 #[cfg(not(comments))]
55 Error::CommentsDisabled => None,
56 }
57 }
58}
59
60impl std::fmt::Display for Error {
61 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
62 match self {
63 Error::XmlError(e) => write!(fmt, "XML error: {}", e),
64 Error::Utf8Error(e) => write!(fmt, "UTF-8 error: {}", e),
65 Error::IoError(e) => write!(fmt, "IO error: {}", e),
66 Error::EndOfDocument => {
67 write!(fmt, "the end of the document has been reached prematurely")
68 }
69 Error::InvalidElementClosed => {
70 write!(fmt, "the XML is invalid, an element was wrongly closed")
71 }
72 Error::InvalidElement => write!(fmt, "the XML element is invalid"),
73 #[cfg(not(comments))]
74 Error::CommentsDisabled => write!(
75 fmt,
76 "a comment has been found even though comments are disabled by feature"
77 ),
78 }
79 }
80}
81
82impl From<::quick_xml::Error> for Error {
83 fn from(err: ::quick_xml::Error) -> Error {
84 Error::XmlError(err)
85 }
86}
87
88impl From<::std::str::Utf8Error> for Error {
89 fn from(err: ::std::str::Utf8Error) -> Error {
90 Error::Utf8Error(err)
91 }
92}
93
94impl From<::std::io::Error> for Error {
95 fn from(err: ::std::io::Error) -> Error {
96 Error::IoError(err)
97 }
98}
99
100/// Our simplified Result type.
101pub type Result<T> = ::std::result::Result<T, Error>;