serde-structprop 0.3.0

Serde serializer and deserializer for the structprop config file format
Documentation
//! Error type used throughout `serde-structprop`.
//!
//! [`Error`] implements both [`serde::de::Error`] and [`serde::ser::Error`] so
//! that it can be used as the single error type for all (de)serialization
//! operations.

use std::fmt;

/// All errors that can arise during serialization or deserialization of a
/// structprop document.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
    /// A custom message forwarded from serde's own error machinery (e.g. type
    /// mismatch messages generated by a `Deserialize` impl).
    Message(String),

    /// A syntax or structural error encountered while lexing or parsing the
    /// input document.  The inner string describes the problem and, where
    /// possible, the unexpected token.
    Parse(String),

    /// The caller asked to (de)serialize a Rust type that has no equivalent
    /// representation in the structprop format (e.g. raw byte slices).
    /// The inner `&str` names the unsupported type.
    UnsupportedType(&'static str),

    /// Map and struct keys must be plain strings.  This error is returned when
    /// a serializer encounters a non-string key.
    KeyMustBeString,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Message(msg) => write!(f, "{msg}"),
            Error::Parse(msg) => write!(f, "parse error: {msg}"),
            Error::UnsupportedType(t) => write!(f, "unsupported type: {t}"),
            Error::KeyMustBeString => write!(f, "map keys must be strings"),
        }
    }
}

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

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

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

/// Convenience alias for `std::result::Result<T, `[`Error`]`>`.
pub type Result<T> = std::result::Result<T, Error>;