use serde::{Deserialize, Serialize};
use strum_macros::{EnumIter, EnumString, IntoStaticStr};
#[derive(
Debug, Clone, PartialEq, Eq, Hash, EnumIter, IntoStaticStr, EnumString, Serialize, Deserialize,
)]
#[serde(into = "&'static str", try_from = "String")]
pub enum License {
#[strum(serialize = "Apache-2.0", ascii_case_insensitive)]
Apache2_0,
#[strum(serialize = "MIT", ascii_case_insensitive)]
Mit,
#[strum(serialize = "CDDL-1.0", ascii_case_insensitive)]
Cddl1_0,
#[strum(serialize = "EPL-2.0", ascii_case_insensitive)]
Epl2_0,
#[strum(serialize = "GPL-2.0", ascii_case_insensitive)]
Gpl2_0,
#[strum(serialize = "GPL-3.0", ascii_case_insensitive)]
Gpl3_0,
#[strum(serialize = "LGPL-2.1", ascii_case_insensitive)]
Lgpl2_1,
#[strum(serialize = "LGPL-3.0", ascii_case_insensitive)]
Lgpl3_0,
#[strum(serialize = "LGPL-2.0", ascii_case_insensitive)]
Lgpl2_0,
#[strum(serialize = "MPL-2.0", ascii_case_insensitive)]
Mpl2_0,
#[strum(serialize = "BSD-2-Clause", ascii_case_insensitive)]
Bsd2Clause,
#[strum(serialize = "BSD-3-Clause", ascii_case_insensitive)]
Bsd3Clause,
}
impl License {
pub const fn name(&self) -> &str {
match self {
Self::Apache2_0 => "Apache License, Version 2.0",
Self::Mit => "The MIT License",
Self::Cddl1_0 => "Common Development and Distribution License 1.0",
Self::Epl2_0 => "Eclipse Public License version 2.0",
Self::Gpl2_0 => "GNU General Public License version 2",
Self::Gpl3_0 => "GNU General Public License version 3",
Self::Lgpl2_1 => "GNU Lesser General Public License version 2.1",
Self::Lgpl3_0 => "GNU Lesser General Public License version 3",
Self::Lgpl2_0 => "GNU Library General Public License version 2",
Self::Mpl2_0 => "Mozilla Public License 2.0",
Self::Bsd2Clause => "The 2-Clause BSD License",
Self::Bsd3Clause => "The 3-Clause BSD License",
}
}
pub const fn spdx_id(&self) -> &'static str {
match self {
Self::Apache2_0 => "Apache-2.0",
Self::Mit => "MIT",
Self::Cddl1_0 => "CDDL-1.0",
Self::Epl2_0 => "EPL-2.0",
Self::Gpl2_0 => "GPL-2.0",
Self::Gpl3_0 => "GPL-3.0",
Self::Lgpl2_1 => "LGPL-2.1",
Self::Lgpl3_0 => "LGPL-3.0",
Self::Lgpl2_0 => "LGPL-2.0",
Self::Mpl2_0 => "MPL-2.0",
Self::Bsd2Clause => "BSD-2-Clause",
Self::Bsd3Clause => "BSD-3-Clause",
}
}
}
impl std::fmt::Display for License {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.spdx_id())
}
}
impl TryFrom<String> for License {
type Error = String;
fn try_from(s: String) -> Result<Self, Self::Error> {
s.parse::<Self>()
.map_err(|_| format!("'{}' is not a supported SPDX license identifier", s))
}
}