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};
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()))
}
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(())
}