mod error; use error::YamplError; use handbar::Handbar; use serde::{Deserialize, Serialize}; use serde_yaml::Mapping; use std::fs; #[derive(Serialize, Deserialize, Debug)] struct Config { template: String, params: Mapping, } pub fn render(config_path: String, output_path: String) -> Result<(), YamplError> { let reg = Handbar::new(); let config: String = match fs::read_to_string(config_path.clone()) { Err(e) => { return Err(YamplError::new(format!( "Failed to read config file:\n{:?}", e, ))) } Ok(config) => config.parse().unwrap_or_default(), }; let config: String = match reg.render_template(&config, &Mapping::new()) { Err(e) => { return Err(YamplError::new(format!( "Failed to render config file:\n{:?}", e, ))); } Ok(config) => config, }; let config: Config = match serde_yaml::from_str(&config) { Err(e) => { return Err(YamplError::new(format!( "Failed to parse config file:\n{:?}", e, ))); } Ok(config) => config, }; let template: String = match fs::read_to_string(config.template.clone()) { Err(e) => { return Err(YamplError::new(format!( "Failed to include template:\n{:?}", e, ))); } Ok(template) => template.parse().unwrap_or_default(), }; let rendered: String = match reg.render_template(&template, &config.params) { Err(e) => { return Err(YamplError::new(format!( "Failed to render template:\n{:?}", e, ))); } Ok(rendered) => rendered, }; if let Err(e) = fs::write(output_path.clone(), rendered) { println!("5: {:?}", e); return Err(YamplError::new(format!( "Failed to save output file:\n{:?}", e, ))); }; Ok(()) }