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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (C) 2017 Christopher R. Field.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::Error;
use lazy_static::lazy_static;
use std::fmt;
use std::str::FromStr;

/// The WiX Source (wxs) template.
static WIX_SOURCE_TEMPLATE: &str = include_str!("main.wxs.mustache");

/// The Apache-2.0 Rich Text Format (RTF) license template.
static APACHE2_LICENSE_TEMPLATE: &str = include_str!("Apache-2.0.rtf.mustache");

/// The GPL-3.0 Rich Text Format (RTF) license template.
static GPL3_LICENSE_TEMPLATE: &str = include_str!("GPL-3.0.rtf.mustache");

/// The MIT Rich Text Format (RTF) license template.
static MIT_LICENSE_TEMPLATE: &str = include_str!("MIT.rtf.mustache");

/// The different templates that can be printed or written to a file.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Template {
    /// The [Apache-2.0] license.
    ///
    /// [Apache-2.0]: https://opensource.org/licenses/Apache-2.0
    Apache2,
    /// The [GPL-3.0] license.
    ///
    /// [GPL-3.0]: https://opensource.org/licenses/gpl-3.0.html
    Gpl3,
    /// The [MIT] license.
    ///
    /// [MIT]: https://opensource.org/licenses/MIT
    Mit,
    /// A [WiX Source (wxs)] file.
    ///
    /// [Wix Source (wxs)]: http://wixtoolset.org/documentation/manual/v3/overview/files.html
    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 {
    /// Gets the ID for the template.
    ///
    /// In the case of a license template, the ID is the [SPDX ID] which is also used for the
    /// `license` field in the package's manifest (Cargo.toml). This is also the same value used
    /// with the `cargo wix print` subcommand.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use wix::Template;
    ///
    /// assert_eq!(Template::Apache2.id(), "Apache-2.0");
    /// assert_eq!(Template::Gpl3.id(), "GPL-3.0");
    /// assert_eq!(Template::Mit.id(), "MIT");
    /// assert_eq!(Template::Wxs.id(), "WXS");
    /// ```
    ///
    /// [SPDX ID]: https://spdx.org/licenses/
    pub fn id(&self) -> &str {
        match *self {
            Template::Apache2 => "Apache-2.0",
            Template::Gpl3 => "GPL-3.0",
            Template::Mit => "MIT",
            Template::Wxs => "WXS",
        }
    }

    /// Gets the possible string representations of each variant.
    ///
    /// The possibilities are combination of case (upper and lower) for the
    /// various templates that are available.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use wix::Template;
    ///
    /// assert_eq!(
    ///     Template::possible_values(),
    ///     &vec![
    ///         "Apache-2.0".to_owned(),
    ///         "apache-2.0".to_owned(),
    ///         "GPL-3.0".to_owned(),
    ///         "gpl-3.0".to_owned(),
    ///         "MIT".to_owned(),
    ///         "mit".to_owned(),
    ///         "WXS".to_owned(),
    ///         "wxs".to_owned()
    ///     ]
    /// );
    /// ```
    pub fn possible_values() -> &'static Vec<String> {
        &POSSIBLE_VALUES
    }

    /// Gets the IDs of all supported licenses.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use wix::Template;
    ///
    /// assert_eq!(
    ///     Template::license_ids(),
    ///     vec![
    ///         "Apache-2.0".to_owned(),
    ///         "GPL-3.0".to_owned(),
    ///         "MIT".to_owned(),
    ///     ]
    /// );
    /// ```
    pub fn license_ids() -> Vec<String> {
        vec![
            Template::Apache2.id().to_owned(),
            Template::Gpl3.id().to_owned(),
            Template::Mit.id().to_owned(),
        ]
    }

    /// Gets the embedded contents of the template as a string.
    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"
            ))),
        }
    }
}