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 comment is to be parsed by minidom
46 NoComments,
47
48 /// An error which is returned when a prefixed is defined twice
49 DuplicatePrefix,
50}
51
52impl StdError for Error {
53 fn cause(&self) -> Option<&dyn StdError> {
54 match self {
55 Error::XmlError(e) => Some(e),
56 Error::Utf8Error(e) => Some(e),
57 Error::IoError(e) => Some(e),
58 Error::EndOfDocument => None,
59 Error::InvalidElementClosed => None,
60 Error::InvalidElement => None,
61 Error::InvalidPrefix => None,
62 Error::MissingNamespace => None,
63 Error::NoComments => None,
64 Error::DuplicatePrefix => None,
65 }
66 }
67}
68
69impl std::fmt::Display for Error {
70 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
71 match self {
72 Error::XmlError(e) => write!(fmt, "XML error: {}", e),
73 Error::Utf8Error(e) => write!(fmt, "UTF-8 error: {}", e),
74 Error::IoError(e) => write!(fmt, "IO error: {}", e),
75 Error::EndOfDocument => {
76 write!(fmt, "the end of the document has been reached prematurely")
77 }
78 Error::InvalidElementClosed => {
79 write!(fmt, "the XML is invalid, an element was wrongly closed")
80 }
81 Error::InvalidElement => write!(fmt, "the XML element is invalid"),
82 Error::InvalidPrefix => write!(fmt, "the prefix is invalid"),
83 Error::MissingNamespace => write!(fmt, "the XML element is missing a namespace",),
84 Error::NoComments => write!(
85 fmt,
86 "a comment has been found even though comments are forbidden"
87 ),
88 Error::DuplicatePrefix => write!(fmt, "the prefix is already defined"),
89 }
90 }
91}
92
93impl From<::quick_xml::Error> for Error {
94 fn from(err: ::quick_xml::Error) -> Error {
95 Error::XmlError(err)
96 }
97}
98
99impl From<::std::str::Utf8Error> for Error {
100 fn from(err: ::std::str::Utf8Error) -> Error {
101 Error::Utf8Error(err)
102 }
103}
104
105impl From<::std::io::Error> for Error {
106 fn from(err: ::std::io::Error) -> Error {
107 Error::IoError(err)
108 }
109}
110
111/// Our simplified Result type.
112pub type Result<T> = ::std::result::Result<T, Error>;