2020-02-14 23:40:38 +00:00
|
|
|
// Copyright (c) 2020 lumi <lumi@pew.im>
|
|
|
|
// Copyright (c) 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
// Copyright (c) 2020 Bastien Orivel <eijebong+minidom@bananium.fr>
|
|
|
|
// Copyright (c) 2020 Astro <astro@spaceboyz.net>
|
|
|
|
// Copyright (c) 2020 Maxime “pep” Buquet <pep@bouah.net>
|
|
|
|
// Copyright (c) 2020 Matt Bilker <me@mbilker.us>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2017-03-08 19:34:17 +00:00
|
|
|
//! Provides an error type for this crate.
|
|
|
|
|
2017-02-19 19:46:44 +00:00
|
|
|
use std::convert::From;
|
2019-09-07 14:02:40 +00:00
|
|
|
use std::error::Error as StdError;
|
2017-02-19 19:46:44 +00:00
|
|
|
|
2018-02-18 18:54:09 +00:00
|
|
|
/// Our main error type.
|
2019-09-05 18:06:17 +00:00
|
|
|
#[derive(Debug)]
|
2018-02-18 18:54:09 +00:00
|
|
|
pub enum Error {
|
|
|
|
/// An error from quick_xml.
|
2019-09-05 18:06:17 +00:00
|
|
|
XmlError(::quick_xml::Error),
|
2018-02-18 18:54:09 +00:00
|
|
|
|
|
|
|
/// An UTF-8 conversion error.
|
2019-09-05 18:06:17 +00:00
|
|
|
Utf8Error(::std::str::Utf8Error),
|
2018-02-18 18:54:09 +00:00
|
|
|
|
|
|
|
/// An I/O error, from std::io.
|
2019-09-05 18:06:17 +00:00
|
|
|
IoError(::std::io::Error),
|
2018-02-18 18:54:09 +00:00
|
|
|
|
|
|
|
/// An error which is returned when the end of the document was reached prematurely.
|
|
|
|
EndOfDocument,
|
|
|
|
|
|
|
|
/// An error which is returned when an element is closed when it shouldn't be
|
|
|
|
InvalidElementClosed,
|
|
|
|
|
|
|
|
/// An error which is returned when an elemet's name contains more than one colon
|
|
|
|
InvalidElement,
|
2019-08-22 16:04:47 +00:00
|
|
|
|
|
|
|
/// An error which is returned when a comment is to be parsed by minidom
|
|
|
|
#[cfg(not(comments))]
|
|
|
|
CommentsDisabled,
|
2018-02-18 18:54:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 14:02:40 +00:00
|
|
|
impl StdError for Error {
|
|
|
|
fn cause(&self) -> Option<&dyn StdError> {
|
|
|
|
match self {
|
2020-02-26 11:54:05 +00:00
|
|
|
Error::XmlError(e) => Some(e),
|
2019-09-07 14:02:40 +00:00
|
|
|
Error::Utf8Error(e) => Some(e),
|
|
|
|
Error::IoError(e) => Some(e),
|
|
|
|
Error::EndOfDocument => None,
|
|
|
|
Error::InvalidElementClosed => None,
|
|
|
|
Error::InvalidElement => None,
|
|
|
|
#[cfg(not(comments))]
|
|
|
|
Error::CommentsDisabled => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 18:06:17 +00:00
|
|
|
impl std::fmt::Display for Error {
|
|
|
|
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Error::XmlError(e) => write!(fmt, "XML error: {}", e),
|
|
|
|
Error::Utf8Error(e) => write!(fmt, "UTF-8 error: {}", e),
|
|
|
|
Error::IoError(e) => write!(fmt, "IO error: {}", e),
|
2019-10-22 23:32:41 +00:00
|
|
|
Error::EndOfDocument => {
|
|
|
|
write!(fmt, "the end of the document has been reached prematurely")
|
|
|
|
}
|
|
|
|
Error::InvalidElementClosed => {
|
|
|
|
write!(fmt, "the XML is invalid, an element was wrongly closed")
|
|
|
|
}
|
2019-09-05 18:06:17 +00:00
|
|
|
Error::InvalidElement => write!(fmt, "the XML element is invalid"),
|
|
|
|
#[cfg(not(comments))]
|
2019-10-22 23:32:41 +00:00
|
|
|
Error::CommentsDisabled => write!(
|
|
|
|
fmt,
|
|
|
|
"a comment has been found even though comments are disabled by feature"
|
|
|
|
),
|
2019-09-05 18:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 01:01:18 +00:00
|
|
|
impl From<::quick_xml::Error> for Error {
|
|
|
|
fn from(err: ::quick_xml::Error) -> Error {
|
2018-02-18 18:54:09 +00:00
|
|
|
Error::XmlError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<::std::str::Utf8Error> for Error {
|
|
|
|
fn from(err: ::std::str::Utf8Error) -> Error {
|
|
|
|
Error::Utf8Error(err)
|
2017-02-19 19:46:44 +00:00
|
|
|
}
|
2018-02-18 18:54:09 +00:00
|
|
|
}
|
2017-02-19 19:46:44 +00:00
|
|
|
|
2018-02-18 18:54:09 +00:00
|
|
|
impl From<::std::io::Error> for Error {
|
|
|
|
fn from(err: ::std::io::Error) -> Error {
|
|
|
|
Error::IoError(err)
|
2017-02-19 19:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-18 18:54:09 +00:00
|
|
|
|
|
|
|
/// Our simplified Result type.
|
|
|
|
pub type Result<T> = ::std::result::Result<T, Error>;
|