mit/
lib.rs

1#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
2
3use clap::Parser;
4use handlebars::Handlebars;
5use std::collections::BTreeMap;
6
7const TEMPLATE: &str = include_str!("./templates/MIT.hbs");
8const TEMPLATE_ZERO: &str = include_str!("./templates/MIT-0.hbs");
9
10/// clap command parser struct
11#[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    /// use the MIT No Attribution (MIT-0) variant
20    #[clap(short = '0', long = "zero", help = "Use MIT-0 variant")]
21    pub zero: bool,
22    /// year, optional
23    #[clap(
24        short = 'y',
25        long = "year",
26        value_name = "YEAR",
27        help = "Set year [optional]"
28    )]
29    pub year: Option<String>,
30    /// author name, required
31    #[clap(
32        short = 'a',
33        long = "author",
34        value_name = "AUTHOR",
35        help = "Set author name"
36    )]
37    pub author: String,
38}
39
40impl App {
41    pub fn run(&self) {
42        print!("{}", self.to_string());
43    }
44    pub fn template(&self) -> &str {
45        if self.zero {
46            TEMPLATE_ZERO
47        } else {
48            TEMPLATE
49        }
50    }
51    pub fn to_string(&self) -> String {
52        let mut handlebars = Handlebars::new();
53        assert!(handlebars
54            .register_template_string("template", self.template())
55            .is_ok());
56        let mut data = BTreeMap::new();
57
58        let year = self.year.clone();
59        let year = year.unwrap_or("".to_string());
60        let author = self.author.clone();
61        let year_author = if year.len() == 0 {
62            author
63        } else {
64            format!("{} {}", year, author)
65        };
66        data.insert("year_author".to_string(), year_author);
67
68        handlebars.render("template", &data).unwrap()
69    }
70}