use chrono::Datelike;
use clap::Parser;
use handlebars::Handlebars;
use std::collections::BTreeMap;
const TEMPLATE: &str = include_str!("./templates/MIT.hbs");
const TEMPLATE_ZERO: &str = include_str!("./templates/MIT-0.hbs");
const TEMPLATE_BLESSING: &str = include_str!("./templates/blessing.hbs");
#[derive(Debug, Clone, Parser)]
#[clap(
name = "mit",
author = "btwiuse <btwiuse@gmail.com>",
about = "generate MIT{,-0} license",
version
)]
pub struct App {
#[clap(short = '0', long = "zero", help = "Use MIT-0 variant")]
pub zero: bool,
#[clap(short = 'y', long = "year", value_name = "YEAR", help = "Set year")]
pub year: Option<String>,
#[clap(
short = 'a',
long = "author",
value_name = "AUTHOR",
default_value = "unknown",
help = "Set author name"
)]
pub author: String,
}
fn this_year() -> i32 {
let current_date = chrono::Utc::now();
current_date.year()
}
impl App {
pub fn run(&self) {
print!("{}", self.to_string());
}
pub fn template(&self) -> &str {
TEMPLATE_BLESSING
}
pub fn to_string(&self) -> String {
let mut handlebars = Handlebars::new();
assert!(handlebars
.register_template_string("template", self.template())
.is_ok());
let mut data = BTreeMap::new();
let year = self.year.clone();
let year = year.unwrap_or(format!("{}", this_year()));
data.insert("year".to_string(), year);
let author = self.author.clone();
data.insert("author".to_string(), author);
handlebars.render("template", &data).unwrap()
}
}