Skip to main content

oseda_cli/
license.rs

1use serde::{Deserialize, Serialize};
2use strum_macros::{EnumIter, EnumString, IntoStaticStr};
3
4/// OSI approved license categorized as (popular / strong community)
5#[derive(
6    Debug, Clone, PartialEq, Eq, Hash, EnumIter, IntoStaticStr, EnumString, Serialize, Deserialize,
7)]
8#[serde(into = "&'static str", try_from = "String")]
9pub enum License {
10    // strum serialize is compatible with serde trait here
11    // this will basically allow a complex internal license representation
12    // but with ease of de/serialization with just the spdx id
13    #[strum(serialize = "Apache-2.0", ascii_case_insensitive)]
14    Apache2_0,
15    #[strum(serialize = "MIT", ascii_case_insensitive)]
16    Mit,
17    #[strum(serialize = "CDDL-1.0", ascii_case_insensitive)]
18    Cddl1_0,
19    #[strum(serialize = "EPL-2.0", ascii_case_insensitive)]
20    Epl2_0,
21    #[strum(serialize = "GPL-2.0", ascii_case_insensitive)]
22    Gpl2_0,
23    #[strum(serialize = "GPL-3.0", ascii_case_insensitive)]
24    Gpl3_0,
25    #[strum(serialize = "LGPL-2.1", ascii_case_insensitive)]
26    Lgpl2_1,
27    #[strum(serialize = "LGPL-3.0", ascii_case_insensitive)]
28    Lgpl3_0,
29    #[strum(serialize = "LGPL-2.0", ascii_case_insensitive)]
30    Lgpl2_0,
31    #[strum(serialize = "MPL-2.0", ascii_case_insensitive)]
32    Mpl2_0,
33    #[strum(serialize = "BSD-2-Clause", ascii_case_insensitive)]
34    Bsd2Clause,
35    #[strum(serialize = "BSD-3-Clause", ascii_case_insensitive)]
36    Bsd3Clause,
37}
38
39impl License {
40    /// Get the full name of license. I'm not sure if this really ever be useful, but i figured its best to include it
41    pub const fn name(&self) -> &str {
42        match self {
43            Self::Apache2_0 => "Apache License, Version 2.0",
44            Self::Mit => "The MIT License",
45            Self::Cddl1_0 => "Common Development and Distribution License 1.0",
46            Self::Epl2_0 => "Eclipse Public License version 2.0",
47            Self::Gpl2_0 => "GNU General Public License version 2",
48            Self::Gpl3_0 => "GNU General Public License version 3",
49            Self::Lgpl2_1 => "GNU Lesser General Public License version 2.1",
50            Self::Lgpl3_0 => "GNU Lesser General Public License version 3",
51            Self::Lgpl2_0 => "GNU Library General Public License version 2",
52            Self::Mpl2_0 => "Mozilla Public License 2.0",
53            Self::Bsd2Clause => "The 2-Clause BSD License",
54            Self::Bsd3Clause => "The 3-Clause BSD License",
55        }
56    }
57
58    /// Get the SPDX id associated with a license.
59    /// Internally, `oseda check` will recognize one of these strings
60    pub const fn spdx_id(&self) -> &'static str {
61        match self {
62            Self::Apache2_0 => "Apache-2.0",
63            Self::Mit => "MIT",
64            Self::Cddl1_0 => "CDDL-1.0",
65            Self::Epl2_0 => "EPL-2.0",
66            Self::Gpl2_0 => "GPL-2.0",
67            Self::Gpl3_0 => "GPL-3.0",
68            Self::Lgpl2_1 => "LGPL-2.1",
69            Self::Lgpl3_0 => "LGPL-3.0",
70            Self::Lgpl2_0 => "LGPL-2.0",
71            Self::Mpl2_0 => "MPL-2.0",
72            Self::Bsd2Clause => "BSD-2-Clause",
73            Self::Bsd3Clause => "BSD-3-Clause",
74        }
75    }
76}
77
78impl std::fmt::Display for License {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        write!(f, "{}", self.spdx_id())
81    }
82}
83
84// useful for oseda check attempting to parse a license spdx id
85impl TryFrom<String> for License {
86    type Error = String;
87
88    fn try_from(s: String) -> Result<Self, Self::Error> {
89        s.parse::<Self>()
90            .map_err(|_| format!("'{}' is not a supported SPDX license identifier", s))
91    }
92}