serde-libconfigasaurus 0.3.0

Serde serialization and deserialization for libconfig format
Documentation
//! Error types for libconfig serialization and deserialization

use std::fmt::{self, Display};

use serde::{de, ser};

/// A Result type alias where the error is `libconfig::Error`
pub type Result<T> = std::result::Result<T, Error>;

/// Errors that can occur when serializing or deserializing libconfig data
#[derive(Debug)]
pub enum Error {
    /// A custom error message from Serde
    Message(String),

    /// Unexpected end of input
    Eof,

    /// Syntax error in the input
    Syntax,

    /// Expected a boolean value
    ExpectedBoolean,

    /// Expected an integer value
    ExpectedInteger,

    /// Expected a float value
    ExpectedFloat,

    /// Expected a string value
    ExpectedString,

    /// Expected an array
    ExpectedArray,

    /// Expected a comma in an array
    ExpectedArrayComma,

    /// Expected end of array
    ExpectedArrayEnd,

    /// Expected a semicolon after array
    ExpectedArraySemicolon,

    /// Expected a list
    ExpectedList,

    /// Expected a comma in a list
    ExpectedListComma,

    /// Expected end of list
    ExpectedListEnd,

    /// Expected a group (struct)
    ExpectedGroup,

    /// Expected an equals sign or colon
    ExpectedEquals,

    /// Expected a semicolon
    ExpectedSemicolon,

    /// Expected a comma or semicolon
    ExpectedCommaOrSemicolon,

    /// Expected end of group
    ExpectedGroupEnd,

    /// Expected an identifier
    ExpectedIdentifier,

    /// Trailing characters after parsing
    TrailingCharacters,

    /// Heterogeneous arrays are not supported
    HeterogeneousArray,

    /// Invalid escape sequence
    InvalidEscape,

    /// Invalid number format
    InvalidNumber,

    /// Maps with non-string keys are not supported
    KeyMustBeAString,

    /// Unit type not supported at top level
    UnitNotSupported,

    /// Type not supported
    TypeNotSupported(String),
}

impl ser::Error for Error {
    fn custom<T: Display>(msg: T) -> Self {
        Error::Message(msg.to_string())
    }
}

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

impl Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::Message(msg) => formatter.write_str(msg),
            Error::Eof => formatter.write_str("unexpected end of input"),
            Error::Syntax => formatter.write_str("syntax error"),
            Error::ExpectedBoolean => formatter.write_str("expected boolean"),
            Error::ExpectedInteger => formatter.write_str("expected integer"),
            Error::ExpectedFloat => formatter.write_str("expected float"),
            Error::ExpectedString => formatter.write_str("expected string"),
            Error::ExpectedArray => formatter.write_str("expected array"),
            Error::ExpectedArrayComma => formatter.write_str("expected comma in array"),
            Error::ExpectedArrayEnd => formatter.write_str("expected end of array"),
            Error::ExpectedArraySemicolon => formatter.write_str("expected semicolon after array"),
            Error::ExpectedList => formatter.write_str("expected list"),
            Error::ExpectedListComma => formatter.write_str("expected comma in list"),
            Error::ExpectedListEnd => formatter.write_str("expected end of list"),
            Error::ExpectedGroup => formatter.write_str("expected group"),
            Error::ExpectedEquals => formatter.write_str("expected '=' or ':'"),
            Error::ExpectedSemicolon => formatter.write_str("expected semicolon"),
            Error::ExpectedCommaOrSemicolon => formatter.write_str("expected comma or semicolon"),
            Error::ExpectedGroupEnd => formatter.write_str("expected end of group"),
            Error::ExpectedIdentifier => formatter.write_str("expected identifier"),
            Error::TrailingCharacters => formatter.write_str("trailing characters"),
            Error::HeterogeneousArray => formatter.write_str("heterogeneous arrays not supported"),
            Error::InvalidEscape => formatter.write_str("invalid escape sequence"),
            Error::InvalidNumber => formatter.write_str("invalid number"),
            Error::KeyMustBeAString => formatter.write_str("map keys must be strings"),
            Error::UnitNotSupported => formatter.write_str("unit type not supported"),
            Error::TypeNotSupported(ty) => write!(formatter, "type not supported: {}", ty),
        }
    }
}

impl std::error::Error for Error {}