wix/templates/mod.rs
1// Copyright (C) 2017 Christopher R. Field.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::Error;
16use lazy_static::lazy_static;
17use std::fmt;
18use std::str::FromStr;
19
20/// The WiX Source (wxs) template.
21static WIX_SOURCE_TEMPLATE: &str = include_str!("main.wxs.mustache");
22
23/// The Apache-2.0 Rich Text Format (RTF) license template.
24static APACHE2_LICENSE_TEMPLATE: &str = include_str!("Apache-2.0.rtf.mustache");
25
26/// The GPL-3.0 Rich Text Format (RTF) license template.
27static GPL3_LICENSE_TEMPLATE: &str = include_str!("GPL-3.0.rtf.mustache");
28
29/// The MIT Rich Text Format (RTF) license template.
30static MIT_LICENSE_TEMPLATE: &str = include_str!("MIT.rtf.mustache");
31
32/// The different templates that can be printed or written to a file.
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub enum Template {
35 /// The [Apache-2.0] license.
36 ///
37 /// [Apache-2.0]: https://opensource.org/licenses/Apache-2.0
38 Apache2,
39 /// The [GPL-3.0] license.
40 ///
41 /// [GPL-3.0]: https://opensource.org/licenses/gpl-3.0.html
42 Gpl3,
43 /// The [MIT] license.
44 ///
45 /// [MIT]: https://opensource.org/licenses/MIT
46 Mit,
47 /// A [WiX Source (wxs)] file.
48 ///
49 /// [Wix Source (wxs)]: http://wixtoolset.org/documentation/manual/v3/overview/files.html
50 Wxs,
51}
52
53lazy_static! {
54 static ref POSSIBLE_VALUES: Vec<String> = vec![
55 Template::Apache2.id().to_owned(),
56 Template::Apache2.id().to_lowercase(),
57 Template::Gpl3.id().to_owned(),
58 Template::Gpl3.id().to_lowercase(),
59 Template::Mit.id().to_owned(),
60 Template::Mit.id().to_lowercase(),
61 Template::Wxs.id().to_owned(),
62 Template::Wxs.id().to_lowercase(),
63 ];
64}
65
66impl Template {
67 /// Gets the ID for the template.
68 ///
69 /// In the case of a license template, the ID is the [SPDX ID] which is also used for the
70 /// `license` field in the package's manifest (Cargo.toml). This is also the same value used
71 /// with the `cargo wix print` subcommand.
72 ///
73 /// # Examples
74 ///
75 /// ```rust
76 /// use wix::Template;
77 ///
78 /// assert_eq!(Template::Apache2.id(), "Apache-2.0");
79 /// assert_eq!(Template::Gpl3.id(), "GPL-3.0");
80 /// assert_eq!(Template::Mit.id(), "MIT");
81 /// assert_eq!(Template::Wxs.id(), "WXS");
82 /// ```
83 ///
84 /// [SPDX ID]: https://spdx.org/licenses/
85 pub fn id(&self) -> &str {
86 match *self {
87 Template::Apache2 => "Apache-2.0",
88 Template::Gpl3 => "GPL-3.0",
89 Template::Mit => "MIT",
90 Template::Wxs => "WXS",
91 }
92 }
93
94 /// Gets the possible string representations of each variant.
95 ///
96 /// The possibilities are combination of case (upper and lower) for the
97 /// various templates that are available.
98 ///
99 /// # Examples
100 ///
101 /// ```rust
102 /// use wix::Template;
103 ///
104 /// assert_eq!(
105 /// Template::possible_values(),
106 /// &vec![
107 /// "Apache-2.0".to_owned(),
108 /// "apache-2.0".to_owned(),
109 /// "GPL-3.0".to_owned(),
110 /// "gpl-3.0".to_owned(),
111 /// "MIT".to_owned(),
112 /// "mit".to_owned(),
113 /// "WXS".to_owned(),
114 /// "wxs".to_owned()
115 /// ]
116 /// );
117 /// ```
118 pub fn possible_values() -> &'static Vec<String> {
119 &POSSIBLE_VALUES
120 }
121
122 /// Gets the IDs of all supported licenses.
123 ///
124 /// # Examples
125 ///
126 /// ```rust
127 /// use wix::Template;
128 ///
129 /// assert_eq!(
130 /// Template::license_ids(),
131 /// vec![
132 /// "Apache-2.0".to_owned(),
133 /// "GPL-3.0".to_owned(),
134 /// "MIT".to_owned(),
135 /// ]
136 /// );
137 /// ```
138 pub fn license_ids() -> Vec<String> {
139 vec![
140 Template::Apache2.id().to_owned(),
141 Template::Gpl3.id().to_owned(),
142 Template::Mit.id().to_owned(),
143 ]
144 }
145
146 /// Gets the embedded contents of the template as a string.
147 pub fn to_str(&self) -> &str {
148 match *self {
149 Template::Apache2 => APACHE2_LICENSE_TEMPLATE,
150 Template::Gpl3 => GPL3_LICENSE_TEMPLATE,
151 Template::Mit => MIT_LICENSE_TEMPLATE,
152 Template::Wxs => WIX_SOURCE_TEMPLATE,
153 }
154 }
155}
156
157impl fmt::Display for Template {
158 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
159 write!(f, "{}", self.id())
160 }
161}
162
163impl FromStr for Template {
164 type Err = Error;
165
166 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
167 match s.to_lowercase().trim() {
168 "apache-2.0" => Ok(Template::Apache2),
169 "gpl-3.0" => Ok(Template::Gpl3),
170 "mit" => Ok(Template::Mit),
171 "wxs" => Ok(Template::Wxs),
172 _ => Err(Error::Generic(format!(
173 "Cannot convert from '{s}' to a Template variant"
174 ))),
175 }
176 }
177}