1use chrono::Datelike;
2use clap::Parser;
3use handlebars::Handlebars;
4use std::collections::BTreeMap;
5
6const TEMPLATE: &str = include_str!("./templates/MIT.hbs");
7const TEMPLATE_ZERO: &str = include_str!("./templates/MIT-0.hbs");
8const TEMPLATE_BLESSING: &str = include_str!("./templates/blessing.hbs");
9
10#[derive(Debug, Clone, Parser)]
12#[clap(
13 name = "mit",
14 author = "btwiuse <btwiuse@gmail.com>",
15 about = "generate MIT{,-0} license",
16 version
17)]
18pub struct App {
19 #[clap(short = '0', long = "zero", help = "Use MIT-0 variant")]
21 pub zero: bool,
22 #[clap(short = 'y', long = "year", value_name = "YEAR", help = "Set year")]
24 pub year: Option<String>,
25 #[clap(
27 short = 'a',
28 long = "author",
29 value_name = "AUTHOR",
30 default_value = "unknown",
31 help = "Set author name"
32 )]
33 pub author: String,
34}
35
36fn this_year() -> i32 {
37 let current_date = chrono::Utc::now();
38 current_date.year()
39}
40
41impl App {
42 pub fn run(&self) {
43 print!("{}", self.to_string());
44 }
45 pub fn template(&self) -> &str {
46 TEMPLATE_BLESSING
47 }
48 pub fn to_string(&self) -> String {
49 let mut handlebars = Handlebars::new();
50 assert!(handlebars
51 .register_template_string("template", self.template())
52 .is_ok());
53 let mut data = BTreeMap::new();
54
55 let year = self.year.clone();
56 let year = year.unwrap_or(format!("{}", this_year()));
57 data.insert("year".to_string(), year);
58
59 let author = self.author.clone();
60 data.insert("author".to_string(), author);
61
62 handlebars.render("template", &data).unwrap()
63 }
64}