use std::collections::HashMap;
use tera::{Tera, try_get_value};
use serde_json::value::{to_value, Value};

pub fn do_nothing_filter(value: &Value, _: &HashMap<String, Value>) -> tera::Result<Value> {
    let s = try_get_value!("do_nothing_filter", "value", String, value);
    Ok(to_value(&s).unwrap())
}

pub fn init_tera(path: &str) -> Tera {
    let tpl_path = format!("{}/**/*",&path);
    let mut tera = match Tera::new(&tpl_path) {
        Ok(t) => { 
            println!("Templates loaded from: {}",&tpl_path);
            log::info!("Templates loaded from: {}",&tpl_path);
            t
        },
        Err(e) => {
            println!("Tempates from {} parsing error(s): {}",&tpl_path, e);
            ::std::process::exit(1);
        }
    };
    tera.autoescape_on(vec![]);
    tera.register_filter("do_nothing", do_nothing_filter);
    tera
}