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::error::Error as StdError;
15
16/// Our main error type.
17#[derive(Debug)]
18pub enum Error {
19 /// Error from rxml parsing or writing
20 XmlError(rxml::Error),
21
22 /// An error which is returned when the end of the document was reached prematurely.
23 EndOfDocument,
24
25 /// An error which is returned when an element being serialized doesn't contain a prefix
26 /// (be it None or Some(_)).
27 InvalidPrefix,
28
29 /// An error which is returned when an element doesn't contain a namespace
30 MissingNamespace,
31
32 /// An error which is returned when a prefixed is defined twice
33 DuplicatePrefix,
34}
35
36impl StdError for Error {
37 fn cause(&self) -> Option<&dyn StdError> {
38 match self {
39 Error::XmlError(e) => Some(e),
40 Error::EndOfDocument => None,
41 Error::InvalidPrefix => None,
42 Error::MissingNamespace => None,
43 Error::DuplicatePrefix => None,
44 }
45 }
46}
47
48impl std::fmt::Display for Error {
49 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
50 match self {
51 Error::XmlError(e) => write!(fmt, "XML error: {}", e),
52 Error::EndOfDocument => {
53 write!(fmt, "the end of the document has been reached prematurely")
54 }
55 Error::InvalidPrefix => write!(fmt, "the prefix is invalid"),
56 Error::MissingNamespace => write!(fmt, "the XML element is missing a namespace",),
57 Error::DuplicatePrefix => write!(fmt, "the prefix is already defined"),
58 }
59 }
60}
61
62impl From<rxml::Error> for Error {
63 fn from(err: rxml::Error) -> Error {
64 Error::XmlError(err)
65 }
66}
67
68impl From<rxml::error::XmlError> for Error {
69 fn from(err: rxml::error::XmlError) -> Error {
70 Error::XmlError(err.into())
71 }
72}
73
74impl From<rxml::strings::Error> for Error {
75 fn from(err: rxml::strings::Error) -> Error {
76 rxml::error::XmlError::from(err).into()
77 }
78}
79
80/// Our simplified Result type.
81pub type Result<T> = ::std::result::Result<T, Error>;