mirror of
https://github.com/mbilker/kbinxml-rs.git
synced 2026-09-10 11:35:22 -05:00
error: add QuickXmlError conversion
This commit is contained in:
35
src/error.rs
35
src/error.rs
@@ -3,9 +3,10 @@ use std::result::Result as StdResult;
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
use failure::{Backtrace, Context, Fail};
|
||||
use value::Value;
|
||||
use quick_xml::Error as QuickXmlError;
|
||||
|
||||
use node_types::{KbinType, StandardType};
|
||||
use value::Value;
|
||||
|
||||
pub type Result<T> = StdResult<T, KbinError>;
|
||||
|
||||
@@ -14,7 +15,7 @@ pub struct KbinError {
|
||||
inner: Context<KbinErrorKind>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Fail)]
|
||||
#[derive(Debug, Fail)]
|
||||
pub enum KbinErrorKind {
|
||||
#[fail(display = "Unable to read {} byte from header", _0)]
|
||||
HeaderRead(&'static str),
|
||||
@@ -106,6 +107,9 @@ pub enum KbinErrorKind {
|
||||
#[fail(display = "Value mismatch, expected {}, but found {:?}", _0, _1)]
|
||||
ValueTypeMismatch(StandardType, Value),
|
||||
|
||||
#[fail(display = "Value mismatch, expected an array, but found {:?}", _0)]
|
||||
ExpectedValueArray(Value),
|
||||
|
||||
#[fail(display = "Invalid input for boolean: {}", _0)]
|
||||
InvalidBooleanInput(u8),
|
||||
|
||||
@@ -114,6 +118,15 @@ pub enum KbinErrorKind {
|
||||
|
||||
#[fail(display = "Invalid state")]
|
||||
InvalidState,
|
||||
|
||||
#[fail(display = "Error handling XML")]
|
||||
XmlError(#[cause] QuickXmlError),
|
||||
}
|
||||
|
||||
impl KbinError {
|
||||
pub fn get_context(&self) -> &KbinErrorKind {
|
||||
self.inner.get_context()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for KbinError {
|
||||
@@ -122,14 +135,6 @@ impl fmt::Display for KbinError {
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for KbinError {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
inner: Context::new(self.inner.get_context().clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Fail for KbinError {
|
||||
fn cause(&self) -> Option<&Fail> {
|
||||
self.inner.cause()
|
||||
@@ -153,11 +158,19 @@ impl From<Context<KbinErrorKind>> for KbinError {
|
||||
}
|
||||
|
||||
impl From<FromUtf8Error> for KbinError {
|
||||
fn from(inner: FromUtf8Error) -> KbinError {
|
||||
fn from(inner: FromUtf8Error) -> Self {
|
||||
inner.context(KbinErrorKind::Utf8).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<QuickXmlError> for KbinError {
|
||||
fn from(inner: QuickXmlError) -> Self {
|
||||
Self {
|
||||
inner: Context::new(KbinErrorKind::XmlError(inner)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "serde")] {
|
||||
use std::error::Error as StdError;
|
||||
|
||||
Reference in New Issue
Block a user