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::io;
15
16use thiserror::Error;
17
18/// Our main error type.
19#[derive(Debug, Error)]
20pub enum Error {
21 /// Error from rxml parsing or writing
22 #[error("XML error: {0}")]
23 XmlError(#[from] rxml::Error),
24
25 /// I/O error from accessing the source or destination.
26 ///
27 /// Even though the [`rxml`] crate emits its errors through
28 /// [`io::Error`] when using it with [`BufRead`][`io::BufRead`],
29 /// any rxml errors will still be reported through the
30 /// [`XmlError`][`Self::XmlError`] variant.
31 #[error("I/O error: {0}")]
32 Io(io::Error),
33
34 /// An error which is returned when the end of the document was reached prematurely.
35 #[error("the end of the document has been reached prematurely")]
36 EndOfDocument,
37
38 /// An error which is returned when an element being serialized doesn't contain a prefix
39 /// (be it None or Some(_)).
40 #[error("the prefix is invalid")]
41 InvalidPrefix,
42
43 /// An error which is returned when an element doesn't contain a namespace
44 #[error("the XML element is missing a namespace")]
45 MissingNamespace,
46
47 /// An error which is returned when a prefixed is defined twice
48 #[error("the prefix is already defined")]
49 DuplicatePrefix,
50}
51
52impl From<io::Error> for Error {
53 fn from(other: io::Error) -> Self {
54 match other.downcast::<rxml::Error>() {
55 Ok(e) => Self::XmlError(e),
56 Err(e) => Self::Io(e),
57 }
58 }
59}
60
61impl From<rxml::strings::Error> for Error {
62 fn from(err: rxml::strings::Error) -> Error {
63 rxml::error::Error::from(err).into()
64 }
65}
66
67/// Our simplified Result type.
68pub type Result<T> = ::core::result::Result<T, Error>;