configlation 0.1.1

Generate configuration from inputs and template
Documentation
mod config;

use anyhow::Context;
use serde::{Deserialize, Serialize};
use std::{
    fs::{self, OpenOptions},
    io::Write,
    os::unix::fs::OpenOptionsExt,
    path::Path,
};

pub use config::{Config, Contents, Input, InputKind, Output};

/// # Errors
///
/// Returns an error if reading the file fails or the contents fail to
/// deserialize into the type.
pub fn read_toml<T: for<'a> Deserialize<'a>>(
    path: impl AsRef<Path>,
) -> anyhow::Result<T> {
    toml::from_str(
        &fs::read_to_string(&path)
            .context(format!("reading file: {:?}", path.as_ref()))?,
    )
    .context(format!("parsing config file: {:?}", path.as_ref()))
}

/// # Errors
///
/// Returns an error if the value fails to serialize or writing the file fails.
pub fn write_toml<T: Serialize>(
    path: &impl AsRef<Path>,
    value: &T,
    mode: u32,
) -> anyhow::Result<()> {
    let mut f = OpenOptions::new().write(true).mode(mode).open(path)?;
    f.write_all(toml::to_string_pretty(value)?.as_bytes())
        .context(format!("writing file: {:?}", path.as_ref()))?;

    Ok(())
}