Skip to main content

entrenar/config/cli/types/
license.rs

1//! License type for CLI commands.
2
3/// License for CLI
4#[derive(Debug, Clone, Copy, PartialEq, Default)]
5pub enum LicenseArg {
6    #[default]
7    CcBy4,
8    CcBySa4,
9    Cc0,
10    Mit,
11    Apache2,
12    Gpl3,
13    Bsd3,
14}
15
16impl std::str::FromStr for LicenseArg {
17    type Err = String;
18
19    fn from_str(s: &str) -> Result<Self, Self::Err> {
20        match s.to_lowercase().replace(['-', '.'], "").as_str() {
21            "ccby4" | "ccby40" => Ok(LicenseArg::CcBy4),
22            "ccbysa4" | "ccbysa40" => Ok(LicenseArg::CcBySa4),
23            "cc0" => Ok(LicenseArg::Cc0),
24            "mit" => Ok(LicenseArg::Mit),
25            "apache2" | "apache20" => Ok(LicenseArg::Apache2),
26            "gpl3" | "gplv3" => Ok(LicenseArg::Gpl3),
27            "bsd3" | "bsd3clause" => Ok(LicenseArg::Bsd3),
28            _ => Err(format!(
29                "Unknown license: {s}. Valid licenses: CC-BY-4.0, CC-BY-SA-4.0, CC0, MIT, Apache-2.0, GPL-3.0, BSD-3-Clause"
30            )),
31        }
32    }
33}
34
35impl std::fmt::Display for LicenseArg {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        match self {
38            LicenseArg::CcBy4 => write!(f, "CC-BY-4.0"),
39            LicenseArg::CcBySa4 => write!(f, "CC-BY-SA-4.0"),
40            LicenseArg::Cc0 => write!(f, "CC0"),
41            LicenseArg::Mit => write!(f, "MIT"),
42            LicenseArg::Apache2 => write!(f, "Apache-2.0"),
43            LicenseArg::Gpl3 => write!(f, "GPL-3.0"),
44            LicenseArg::Bsd3 => write!(f, "BSD-3-Clause"),
45        }
46    }
47}