use std::io::Write;
use yaml_rust::{Yaml, YamlEmitter};
use crate::errors;
pub fn write_yaml<P>(doc: &Yaml, path: P) -> Result<(), errors::GardenError>
where
P: std::convert::AsRef<std::path::Path> + std::fmt::Debug,
{
let mut out_str = String::new();
{
let mut emitter = YamlEmitter::new(&mut out_str);
emitter.multiline_strings(true);
emitter.dump(doc).unwrap_or(()); }
out_str += "\n";
let mut file = std::fs::File::create(&path).map_err(|io_err| {
errors::GardenError::CreateConfigurationError {
path: path.as_ref().into(),
err: io_err,
}
})?;
file.write_all(&out_str.into_bytes()).map_err(|_| {
errors::GardenError::WriteConfigurationError {
path: path.as_ref().into(),
}
})?;
file.sync_all()
.map_err(|sync_err| errors::GardenError::SyncConfigurationError {
path: path.as_ref().into(),
err: sync_err,
})
}