dynconfig 0.1.2

Dynamically change fields of a struct based on a path.
Documentation
use core::fmt;

use alloc::string::{String, ToString};

#[derive(Debug, Clone)]
enum ErrorKind {
    UnknownField(String),
    Custom(String),
}

/// Error returned by [`Dynconfig`].
///
/// [`Dynconfig`]: crate::Dynconfig
#[derive(Debug, Clone)]
pub struct Error(ErrorKind);

impl Error {
    /// Raised when an implementation encounters an unknown field.
    pub fn unknown_field<I>(field: I) -> Self
    where
        I: Into<String>,
    {
        Self(ErrorKind::UnknownField(field.into()))
    }

    /// Raised when there is a general error.
    pub fn custom<T>(msg: T) -> Self
    where
        T: fmt::Display,
    {
        Self(ErrorKind::Custom(msg.to_string()))
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.0 {
            ErrorKind::UnknownField(ref field) => {
                f.write_str("unknown field '")?;
                f.write_str(field)?;
                f.write_str("'")?;
                Ok(())
            }
            ErrorKind::Custom(ref x) => f.write_str(x),
        }
    }
}

impl core::error::Error for Error {}