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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
extern crate chrono;
#[macro_use]
extern crate failure;
extern crate handlebars;
#[macro_use]
extern crate lazy_static;
extern crate regex;
extern crate toml;
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;

use std::borrow::Borrow;
use std::collections::BTreeMap;
use std::path::PathBuf;

use chrono::{Datelike, Local};
use failure::Error;
use handlebars::Handlebars;
use regex::Regex;

lazy_static! {
    /// A list of licenses with text included in the program.
    static ref LICENSES: Vec<License> = {
        let mut toml: BTreeMap<String, Vec<License>> = toml::from_str(include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/licenses.toml"
        ))).unwrap();

        toml.remove("license").unwrap()
    };
}

/// An open-source license.
#[derive(Debug, PartialEq, Deserialize)]
pub struct License {
    /// The identifier for the license on the command line, if multiple licenses are present.
    ///
    /// For example, `LICENSE-APACHE` vs `LICENSE-MIT`.
    pub identifier: String,

    /// The [SPDX license identifier](https://github.com/spdx/license-list-data/tree/v2.4).
    pub spdx: String,

    /// A handlebars template of the license text.
    pub text: String,
}

/// Parses author names from a list of author names, which might include git-style author names
/// such as `John Doe <jd@example.com>`.
pub fn parse_author_names<'a>(authors: &[&'a str]) -> Result<Vec<&'a str>, Error> {
    if authors.is_empty() {
        return Err(failure::err_msg("at least one author is required"));
    }

    let names = authors
        .into_iter()
        .map(|author| match parse_git_style_author(author) {
            Some(name) => name,
            None => author,
        })
        .collect();

    Ok(names)
}

/// Returns true if the given license ID is known by SPDX 2.4.
fn is_valid_spdx_id(id: &str) -> bool {
    #[derive(Debug, Deserialize)]
    #[serde(rename_all = "camelCase")]
    struct LicenseList {
        licenses: Vec<License>,
    }

    #[derive(Debug, Deserialize)]
    #[serde(rename_all = "camelCase")]
    struct License {
        license_id: String,
    }

    lazy_static! {
        static ref SPDX_LICENSE_LIST: LicenseList = serde_json::from_str(include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/spdx-licenses.json"
        ))).unwrap();
    }

    SPDX_LICENSE_LIST
        .licenses
        .iter()
        .any(|license| license.license_id == id)
}

/// Errors that occur while parsing an SPDX expression.
#[derive(Debug, Fail)]
pub enum ParseError {
    #[fail(display = "invalid SPDX license ID: '{}'", _0)]
    InvalidLicenseId(String),

    #[fail(
        display = "SPDX ID '{}' is valid, but unsupported by apply-license. Consider opening a PR.",
        _0
    )]
    UnsupportedLicenseId(String),
}

/// Parse a list of license identifiers from an SPDX license expression.
///
/// The cargo manifest format allows combining license expressions with `/`, so we allow it as
/// well, though it's not valid SPDX.
pub fn parse_spdx(license_expr: &str) -> Result<Vec<&'static License>, ParseError> {
    let split: Box<Iterator<Item = &str>> = if license_expr.contains("/") {
        Box::new(license_expr.split("/"))
    } else {
        Box::new(license_expr.split_whitespace())
    };

    split
        .flat_map(|token| match token {
            "WITH" | "OR" | "AND" => None,
            token => Some(token),
        })
        .map(|id| {
            if is_valid_spdx_id(id) {
                LICENSES
                    .iter()
                    .find(|license| license.spdx == id)
                    .ok_or_else(|| ParseError::UnsupportedLicenseId(id.to_string()))
            } else {
                Err(ParseError::InvalidLicenseId(id.to_string()))
            }
        })
        .collect()
}

/// Given a list of authors and SPDX license identifiers, returns a map from file name to contents.
///
/// If only one license file is present, writes the file name will be `LICENSE`. If two or more
/// licenses are present, then each file will be named `LICENSE-{id}` (e.g., `LICENSE-MIT`).
pub fn render_license_text<S: Borrow<str>>(
    licenses: &[&License],
    authors: &[S],
) -> Result<BTreeMap<PathBuf, String>, Error> {
    let mut reg = Handlebars::new();

    for license in LICENSES.iter() {
        reg.register_template_string(&license.spdx, &license.text)
            .expect("syntax error in license template");
    }

    #[derive(Debug, Serialize)]
    struct TemplateData {
        year: i32,
        copyright_holders: String,
    }

    licenses
        .into_iter()
        .map(|license| {
            let name = if licenses.len() == 1 {
                String::from("LICENSE")
            } else {
                format!("LICENSE-{}", license.identifier)
            };

            let contents = reg.render(
                &license.spdx,
                &TemplateData {
                    year: Local::today().year(),
                    copyright_holders: authors.join(", "),
                },
            )?;

            Ok((PathBuf::from(name), contents))
        })
        .collect()
}

fn parse_git_style_author(name: &str) -> Option<&str> {
    lazy_static! {
        static ref GIT_NAME_RE: Regex = Regex::new(r"(?P<name>.+) <(?P<email>.+)>").unwrap();
    }

    GIT_NAME_RE
        .captures(name)
        .map(|caps| caps.name("name").unwrap().as_str())
}

#[cfg(test)]
mod tests {
    use {is_valid_spdx_id, parse_spdx, License, LICENSES};

    fn get_license(id: &str) -> &'static License {
        LICENSES.iter().find(|l| l.spdx == id).unwrap()
    }

    #[test]
    fn parse_licenses() {
        assert!(LICENSES.iter().any(|l| l.spdx == "MIT"));
    }

    #[test]
    fn valid_spdx_ids() {
        assert!(is_valid_spdx_id("MIT"));
        assert!(!is_valid_spdx_id("foobar"));
    }

    #[test]
    fn simple() {
        assert_eq!(parse_spdx("GPL-3.0").unwrap(), &[get_license("GPL-3.0")]);
    }

    #[test]
    fn compound() {
        assert_eq!(
            parse_spdx("MIT OR Apache-2.0").unwrap(),
            &[
                get_license("MIT"),
                get_license("Apache-2.0")
            ],
        );
    }

    #[test]
    fn cargo_manifest_licenses() {
        assert_eq!(
            parse_spdx("MIT/Apache-2.0").unwrap(),
            &[get_license("MIT"), get_license("Apache-2.0")]
        );
    }
}