buni-rs 1.0.0

Reference Buni deserializer in Rust
Documentation
use nom::error::ErrorKind;
use nom::Input;
use serde::de;
use std::fmt;
use std::fmt::{Debug, Display, Formatter};

#[derive(Debug)]
pub enum DeserializeError<I: Input + Debug + Display> {
    Message(String),
    ParseError(Option<I>, ErrorKind),
    WrappedParseError(Box<Self>, Option<I>, ErrorKind),
    ConversionError(String),

    ExpectedInteger,
    EmptyString,
    ExpectedString,
    ExpectedList,
    ExpectedListOrNull,
    ExpectedListOrString,
    ExpectedAnnotatedList,
    UnexpectedNamedList,
    NotSupported,
}

impl<I: Input + Debug + Display> std::error::Error for DeserializeError<I> {}

impl<I: Input + Debug + Display> de::Error for DeserializeError<I> {
    fn custom<T: Display>(msg: T) -> Self {
        DeserializeError::Message(msg.to_string())
    }
}

impl<I: Input + Debug + Display> nom::error::ParseError<I> for DeserializeError<I> {
    fn from_error_kind(input: I, kind: ErrorKind) -> Self {
        DeserializeError::ParseError(Some(input), kind)
    }

    fn append(input: I, kind: ErrorKind, other: DeserializeError<I>) -> DeserializeError<I> {
        DeserializeError::WrappedParseError(Box::new(other), Some(input), kind)
    }
}

impl<I: Input + Debug + Display> Display for DeserializeError<I> {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match self {
            DeserializeError::Message(msg) => write!(formatter, "serde message: {}", msg),
            DeserializeError::ParseError(input, kind) => {
                match input {
                    None => write!(formatter, "parse error: {}", kind.description()),
                    Some(input) => write!(formatter, "parse error: {} for input {}", kind.description(), input)
                }
            },
            DeserializeError::WrappedParseError(err, input, kind) => {
                match input {
                    None => write!(formatter, "parse error: {} (wrapped: {})", kind.description(), err.to_string()),
                    Some(input) => write!(formatter, "parse error: {} for input {} (wrapped: {})", kind.description(), input, err.to_string())
                }
            },
            DeserializeError::ConversionError(err) => write!(formatter, "type conversion error: {}", err),

            DeserializeError::ExpectedInteger => write!(formatter, "expected integer"),
            DeserializeError::EmptyString => write!(formatter, "empty string"),
            DeserializeError::ExpectedString => write!(formatter, "expected string"),
            DeserializeError::ExpectedList => write!(formatter, "expected list"),
            DeserializeError::ExpectedListOrNull => write!(formatter, "expected list or null"),
            DeserializeError::ExpectedListOrString => write!(formatter, "expected list or string"),
            DeserializeError::ExpectedAnnotatedList => write!(formatter, "expected annotated list"),
            DeserializeError::UnexpectedNamedList => write!(formatter, "unexpected named list"),
            DeserializeError::NotSupported => write!(formatter, "not supported"),
        }
    }
}

impl<I: Input + Debug + Display> DeserializeError<I> {
    pub fn map_to_conversion_error<E: Display>(e: E) -> Self {
        DeserializeError::ConversionError(e.to_string())
    }
}