gifnoc 0.1.5

Type-safe configuration with layered overrides via a proc-macro DSL
Documentation
use crate::nesting;
use serde_json::Value;
use std::path::Path;

/// Reads a YAML file and returns its contents as a [`serde_json::Value`].
///
/// The returned value is intended to be passed to [`Configurable::update`][crate::Configurable::update].
/// Panics if the file cannot be read or contains invalid YAML. Dot-separated keys are parsed into
/// hierarchically nested JSONs.
///
/// Requires the `yaml` feature:
/// ```toml
/// [dependencies]
/// gifnoc = { version = "...", features = ["yaml"] }
/// ```
///
/// # Example
///
/// ```rust,no_run
/// use gifnoc::{config, Configurable};
///
/// config! {
///     AppConfig {
///         host: String = "localhost",
///         port: u32 = 8080u32,
///     }
/// }
///
/// let config = AppConfig::default().update(gifnoc::yaml::from_file("config.yaml"));
/// ```
pub fn from_file(path: impl AsRef<Path>) -> Value {
    let content = std::fs::read_to_string(path).unwrap();
    let parsed: Value = serde_yml::from_str(&content).unwrap();
    let flat = nesting::flatten(parsed);
    nesting::nest(flat)
}