use std::path::Path;
use anyhow::Context;
use serde::{de::DeserializeOwned, Serialize};
use casper_node::utils::read_file;
pub fn load_from_file<P: AsRef<Path>, C: DeserializeOwned>(config_path: P) -> anyhow::Result<C> {
let path_ref = config_path.as_ref();
let config: C = toml::from_slice(
&read_file(path_ref).with_context(|| "failed to read configuration file")?,
)
.with_context(|| format!("Failed to parse configuration file {}", path_ref.display()))?;
Ok(config)
}
pub fn to_string<C: Serialize>(cfg: &C) -> anyhow::Result<String> {
toml::to_string_pretty(cfg).with_context(|| "Failed to serialize default configuration")
}
#[cfg(test)]
mod tests {
use casper_node::reactor::validator::Config;
#[test]
fn example_config_should_parse() {
let config_path = format!(
"{}/../resources/local/config.toml",
env!("CARGO_MANIFEST_DIR")
);
let _config: Config = super::load_from_file(config_path).unwrap();
}
}