use crate::error::JournalError; use std::path::Path; use tera::{Context, Tera}; const NEW_ENTRY: &str = r#"[[!meta title="{{ title }}"]] [[!meta date="{{ date }}"]] This is the default template. "#; pub struct Templates { tera: Tera, } impl Templates { pub fn new(dirname: &Path) -> Result { let glob = format!("{}/.config/templates/*", dirname.display()); let mut tera = Tera::new(&glob).expect("Tera::new"); add_default_template(&mut tera, "new_entry", NEW_ENTRY); Ok(Self { tera }) } pub fn new_draft(&self, context: &Context) -> Result { self.render("new_entry", &context) } fn render(&self, name: &str, context: &Context) -> Result { match self.tera.render(name, &context) { Ok(s) => Ok(s), Err(e) => match e.kind { tera::ErrorKind::TemplateNotFound(x) => Err(JournalError::TemplateNotFound(x)), _ => Err(JournalError::TemplateRender(name.to_string(), e)), }, } } } fn add_default_template(tera: &mut Tera, name: &str, template: &str) { let context = Context::new(); if let Err(err) = tera.render(name, &context) { if let tera::ErrorKind::TemplateNotFound(_) = err.kind { tera.add_raw_template(name, template) .expect("Tera::add_raw_template"); } } }