zonfig 0.1.0

A small dynamic configuration loader with file watching and hot reload support.
Documentation
use std::path::Path;

use serde::de::DeserializeOwned;

use crate::error::{Error, Result};
use crate::format::Format;

/// Loads a configuration file and deserializes it into `T`.
///
/// The format is detected from the file extension. Supported extensions are
/// `.json`, `.yaml`, `.yml`, and `.toml`.
pub fn load<T>(path: impl AsRef<Path>) -> Result<T>
where
    T: DeserializeOwned,
{
    let path = path.as_ref();
    let format = Format::from_path(path)?;
    load_with_format(path, format)
}

/// Loads a configuration file with an explicitly selected format.
pub fn load_with_format<T>(path: impl AsRef<Path>, format: Format) -> Result<T>
where
    T: DeserializeOwned,
{
    let path = path.as_ref();
    let content = std::fs::read_to_string(path).map_err(|source| Error::Read {
        path: path.to_path_buf(),
        source,
    })?;

    format.parse(path, &content)
}