Macro impl_from_deserializer_error

Source
macro_rules! impl_from_deserializer_error {
    ($err: path) => { ... };
}
Expand description

Implements conversion from Error with From for the given error type.

Allows for custom error types with source error types to easily be converted into with ?.

The given error type should implement From<Error<Infallible>>.

ยงExamples

use std::convert::Infallible;

use xml_stinks::deserializer::Error as DeserializerError;
use xml_stinks::impl_from_deserializer_error;

#[derive(Debug, thiserror::Error)]
enum FooError
{
    #[error("Deserialization failed")]
    DeserializeFailed(#[from] DeserializerError<Infallible>),

    #[error("Invalid bar")]
    InvalidBar(#[from] BarError),
}

impl_from_deserializer_error!(FooError);

#[derive(Debug, thiserror::Error)]
enum BarError
{
    #[error("Oops")]
    Oops,
}

let err_a: FooError = DeserializerError::<Infallible>::UnexpectedEndOfFile.into();

assert!(matches!(
    err_a,
    FooError::DeserializeFailed(DeserializerError::UnexpectedEndOfFile)
));

let err_b: FooError = DeserializerError::DeserializeFailed(BarError::Oops).into();

assert!(matches!(err_b, FooError::InvalidBar(BarError::Oops)));