1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
//! A simple tool to generate rust code by passing a ron value
//! to a tera template

use std::{
    fs,
    collections::HashMap,
    path::Path,
    error::Error
};

use heck::{CamelCase, ShoutySnakeCase, SnakeCase};

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Mode {
    Overwrite,
    Verify,
}

pub use Mode::*;

/// A helper to update file on disk if it has changed.
/// With verify = false,
pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<(), Box<dyn Error>> {
    match fs::read_to_string(path) {
        Ok(ref old_contents) if old_contents == contents => {
            return Ok(());
        }
        _ => (),
    }
    if mode == Verify {
        Err(format!("`{}` is not up-to-date", path.display()))?;
    }
    eprintln!("updating {}", path.display());
    fs::write(path, contents)?;
    Ok(())
}

pub fn generate(
    template: &Path,
    src: &Path,
    mode: Mode,
) -> Result<(), Box<dyn Error>> {
    assert_eq!(
        template.extension().and_then(|it| it.to_str()), Some("tera"),
        "template file must have .rs.tera extension",
    );
    let file_name = template.file_stem().unwrap().to_str().unwrap();
    assert!(
        file_name.ends_with(".rs"),
        "template file must have .rs.tera extension",
    );
    let tgt = template.with_file_name(file_name);
    let template = fs::read_to_string(template)?;
    let src: ron::Value = {
        let text = fs::read_to_string(src)?;
        ron::de::from_str(&text)?
    };
    let content = render(&template, src)?;
    update(
        &tgt,
        &content,
        mode,
    )
}

pub fn render(
    template: &str,
    src: ron::Value,
) -> Result<String, Box<dyn Error>> {
    let mut tera = mk_tera();
    tera.add_raw_template("_src", template)
        .map_err(|e| format!("template parsing error: {:?}", e))?;
    let res = tera.render("_src", &src)
        .map_err(|e| format!("template rendering error: {:?}", e))?;
    return Ok(res);
}

fn mk_tera() -> tera::Tera {
    let mut res = tera::Tera::default();
    res.register_filter("camel", |arg, _| {
        Ok(arg.as_str().unwrap().to_camel_case().into())
    });
    res.register_filter("snake", |arg, _| {
        Ok(arg.as_str().unwrap().to_snake_case().into())
    });
    res.register_filter("SCREAM", |arg, _| {
        Ok(arg.as_str().unwrap().to_shouty_snake_case().into())
    });
    res.register_function("concat", Box::new(concat));
    res
}

fn concat(args: HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
    let mut elements = Vec::new();
    for &key in ["a", "b", "c"].iter() {
        let val = match args.get(key) {
            Some(val) => val,
            None => continue,
        };
        let val = val.as_array().unwrap();
        elements.extend(val.iter().cloned());
    }
    Ok(tera::Value::Array(elements))
}