ostrya-core 0.18.0

ostree object model, checksums, on-disk format primitives, and typed config
Documentation
//! The error of the typed config API.

use std::fmt;

/// An error of the typed config API.
///
/// The typed reads and edits of this crate give this error. These are the
/// sources:
///
/// - A [`ValueError`](super::ValueError), a value that a key cannot read,
///   gives a variant with the group and the key of the value.
/// - A required key with no value and an unknown key of a closed group give
///   [`InvalidFormat`](ConfigError::InvalidFormat).
/// - A section name that a key file cannot hold gives
///   [`InvalidInput`](ConfigError::InvalidInput).
/// - A value text that a key file cannot hold gives
///   [`Core`](ConfigError::Core).
///
/// Each variant prints the same text as the variant of `ostrya::Error` with
/// the same name, and the `ostrya` crate converts each variant to that
/// variant.
///
/// The enum is `#[non_exhaustive]`, so a release can add a variant without a
/// breaking change.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ConfigError {
    /// An error of the key file.
    ///
    /// A value that is not a boolean or an integer, and a malformed escape
    /// sequence, give [`Error::KeyFile`](crate::Error::KeyFile). The text is
    /// the text of the error, with no prefix.
    Core(crate::Error),
    /// A value that does not match its expected format.
    InvalidFormat(String),
    /// An argument outside the values that the operation accepts.
    InvalidInput(String),
}

impl fmt::Display for ConfigError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ConfigError::Core(e) => write!(f, "{e}"),
            ConfigError::InvalidFormat(reason) => write!(f, "invalid format: {reason}"),
            ConfigError::InvalidInput(reason) => write!(f, "invalid input: {reason}"),
        }
    }
}

impl std::error::Error for ConfigError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            ConfigError::Core(e) => std::error::Error::source(e),
            _ => None,
        }
    }
}

impl From<crate::Error> for ConfigError {
    fn from(e: crate::Error) -> Self {
        ConfigError::Core(e)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn each_variant_prints_the_text_of_its_namesake() {
        let core = crate::Error::KeyFile("value 'x' for core.fsync is not a boolean".into());
        assert_eq!(
            ConfigError::from(core.clone()),
            ConfigError::Core(core.clone())
        );
        assert_eq!(
            ConfigError::Core(core).to_string(),
            "keyfile: value 'x' for core.fsync is not a boolean"
        );
        assert_eq!(
            ConfigError::InvalidFormat("[core] fsync is empty".into()).to_string(),
            "invalid format: [core] fsync is empty"
        );
        assert_eq!(
            ConfigError::InvalidInput("no section".into()).to_string(),
            "invalid input: no section"
        );
    }

    #[test]
    fn the_source_of_core_is_the_source_of_the_inner_error() {
        use std::error::Error as _;

        let inner = crate::Error::Gvariant(ostrya_gvariant::Error::InvalidValue("truncated"));
        let err = ConfigError::Core(inner.clone());
        assert_eq!(
            err.source().map(ToString::to_string),
            inner.source().map(ToString::to_string)
        );
        assert!(err.source().is_some());
        assert!(ConfigError::InvalidFormat("x".into()).source().is_none());
    }
}