use crate::{Header, SingleLineChecker};
use lazy_static::lazy_static;
use std::marker;
pub use license;
#[cfg(test)]
mod tests;
type BoxedLicense = Box<dyn license::License + Sync + Send>;
pub struct SpdxLicense<L: LicenseTokens> {
license_text: BoxedLicense,
search_pattern: String,
lines_to_search: usize,
marker: marker::PhantomData<L>,
}
impl<L: LicenseTokens> SpdxLicense<L> {
pub fn new(license_text: BoxedLicense, search_pattern: String, lines_to_search: usize) -> Self {
Self {
license_text,
search_pattern,
lines_to_search,
marker: marker::PhantomData,
}
}
pub fn build_header(
&self,
replacement_values: L::TokenReplacementValues,
) -> Header<SingleLineChecker> {
let checker = SingleLineChecker::new(self.search_pattern.clone(), self.lines_to_search);
let text = self
.license_text
.header()
.unwrap_or(self.license_text.text());
let header = L::replacement_pairs(replacement_values).iter().fold(
text.to_string(),
|current_text, (replace_token, replace_value)| {
current_text.replacen(replace_token, replace_value, 1)
},
);
Header::new(checker, header)
}
}
pub trait LicenseTokens {
type TokenReplacementValues;
fn replacement_pairs(replacements: Self::TokenReplacementValues)
-> Vec<(&'static str, String)>;
}
pub struct NoTokens;
impl LicenseTokens for NoTokens {
type TokenReplacementValues = ();
fn replacement_pairs(
_replacements: Self::TokenReplacementValues,
) -> Vec<(&'static str, String)> {
Vec::new()
}
}
#[doc(hidden)]
pub struct Apache2Tokens;
impl LicenseTokens for Apache2Tokens {
type TokenReplacementValues = YearCopyrightOwnerValue;
fn replacement_pairs(
replacements: Self::TokenReplacementValues,
) -> Vec<(&'static str, String)> {
vec![
("[yyyy]", replacements.year.to_string()),
("[name of copyright owner]", replacements.copyright_owner),
]
}
}
#[doc(hidden)]
pub struct MitTokens;
impl LicenseTokens for MitTokens {
type TokenReplacementValues = YearCopyrightOwnerValue;
fn replacement_pairs(
replacements: Self::TokenReplacementValues,
) -> Vec<(&'static str, String)> {
vec![
("<year>", replacements.year.to_string()),
("<copyright holders>", replacements.copyright_owner),
]
}
}
#[doc(hidden)]
pub struct Bsd3ClauseTokens {}
impl LicenseTokens for Bsd3ClauseTokens {
type TokenReplacementValues = YearCopyrightOwnerValue;
fn replacement_pairs(
replacements: Self::TokenReplacementValues,
) -> Vec<(&'static str, String)> {
vec![
("<year>", replacements.year.to_string()),
("<owner>", replacements.copyright_owner),
]
}
}
#[doc(hidden)]
pub struct Gpl3Tokens {}
impl LicenseTokens for Gpl3Tokens {
type TokenReplacementValues = YearCopyrightOwnerValue;
fn replacement_pairs(
replacements: Self::TokenReplacementValues,
) -> Vec<(&'static str, String)> {
vec![
("<year>", replacements.year.to_string()),
("<name of author>", replacements.copyright_owner),
]
}
}
pub struct YearCopyrightOwnerValue {
pub year: u32,
pub copyright_owner: String,
}
impl YearCopyrightOwnerValue {
pub fn new(year: u32, copyright_owner: String) -> Self {
Self {
year,
copyright_owner,
}
}
}
lazy_static! {
pub static ref APACHE_2_0: SpdxLicense<Apache2Tokens> = SpdxLicense ::new(
Box::new(license::licenses::Apache2_0),
"Apache License, Version 2.0".to_string(),
10
);
}
lazy_static! {
pub static ref MIT: SpdxLicense<MitTokens> = SpdxLicense ::new(
Box::new(license::licenses::Mit),
"MIT License".to_string(),
10
);
}
lazy_static! {
pub static ref BSD_3: SpdxLicense<Bsd3ClauseTokens> = SpdxLicense ::new(
Box::new(license::licenses::Bsd3Clause),
"Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:".to_string(),
10
);
}
lazy_static! {
pub static ref GPL_3_0_ONLY: SpdxLicense<Gpl3Tokens> = SpdxLicense ::new(
Box::new(license::licenses::Gpl3_0Only),
"GNU General Public License".to_string(),
10
);
}
lazy_static! {
pub static ref EPL_2_0: SpdxLicense<NoTokens> = SpdxLicense ::new(
Box::new(license::licenses::Epl2_0),
"Eclipse Public License - v 2.0".to_string(),
10
);
}
lazy_static! {
pub static ref MPL_2_0: SpdxLicense<NoTokens> = SpdxLicense ::new(
Box::new(license::licenses::Mpl2_0),
"Mozilla Public License, v. 2.0".to_string(),
10
);
}