use crate::Error;
use lazy_static::lazy_static;
use std::fmt;
use std::str::FromStr;
static WIX_SOURCE_TEMPLATE: &str = include_str!("main.wxs.mustache");
static APACHE2_LICENSE_TEMPLATE: &str = include_str!("Apache-2.0.rtf.mustache");
static GPL3_LICENSE_TEMPLATE: &str = include_str!("GPL-3.0.rtf.mustache");
static MIT_LICENSE_TEMPLATE: &str = include_str!("MIT.rtf.mustache");
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Template {
Apache2,
Gpl3,
Mit,
Wxs,
}
lazy_static! {
static ref POSSIBLE_VALUES: Vec<String> = vec![
Template::Apache2.id().to_owned(),
Template::Apache2.id().to_lowercase(),
Template::Gpl3.id().to_owned(),
Template::Gpl3.id().to_lowercase(),
Template::Mit.id().to_owned(),
Template::Mit.id().to_lowercase(),
Template::Wxs.id().to_owned(),
Template::Wxs.id().to_lowercase(),
];
}
impl Template {
pub fn id(&self) -> &str {
match *self {
Template::Apache2 => "Apache-2.0",
Template::Gpl3 => "GPL-3.0",
Template::Mit => "MIT",
Template::Wxs => "WXS",
}
}
pub fn possible_values() -> &'static Vec<String> {
&POSSIBLE_VALUES
}
pub fn license_ids() -> Vec<String> {
vec![
Template::Apache2.id().to_owned(),
Template::Gpl3.id().to_owned(),
Template::Mit.id().to_owned(),
]
}
pub fn to_str(&self) -> &str {
match *self {
Template::Apache2 => APACHE2_LICENSE_TEMPLATE,
Template::Gpl3 => GPL3_LICENSE_TEMPLATE,
Template::Mit => MIT_LICENSE_TEMPLATE,
Template::Wxs => WIX_SOURCE_TEMPLATE,
}
}
}
impl fmt::Display for Template {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.id())
}
}
impl FromStr for Template {
type Err = Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s.to_lowercase().trim() {
"apache-2.0" => Ok(Template::Apache2),
"gpl-3.0" => Ok(Template::Gpl3),
"mit" => Ok(Template::Mit),
"wxs" => Ok(Template::Wxs),
_ => Err(Error::Generic(format!(
"Cannot convert from '{s}' to a Template variant"
))),
}
}
}