vos_core/schema/license/
mod.rs

1use super::*;
2
3#[derive(Clone, Debug, Serialize, Deserialize)]
4pub enum ProjectLicense {
5    MIT,
6    Apache2,
7    Custom { license: String, text: String, link: Option<Url> },
8}
9
10impl Default for ProjectLicense {
11    fn default() -> Self {
12        ProjectLicense::MIT
13    }
14}
15
16impl FromStr for ProjectLicense {
17    type Err = Infallible;
18
19    fn from_str(s: &str) -> Result<Self, Self::Err> {
20        let mut normed = String::with_capacity(s.len());
21        for char in s.chars() {
22            match char {
23                ' ' | '-' | '_' => continue,
24                c => normed.push(c.to_ascii_lowercase()),
25            }
26        }
27        let out = match s.to_lowercase().as_str() {
28            "mit" => ProjectLicense::MIT,
29            "apache2" | "apache2.0" => ProjectLicense::Apache2,
30            _ => ProjectLicense::Custom { license: s.to_string(), text: "".to_string(), link: None },
31        };
32        Ok(out)
33    }
34}
35
36impl ProjectLicense {
37    pub fn parse(license: &str, url: Option<Url>, content: impl Into<String>) -> ProjectLicense {
38        let mut out = Self::from_str(license).unwrap();
39        if let ProjectLicense::Custom { text, link, .. } = &mut out {
40            *link = url;
41            *text = content.into();
42        }
43        out
44    }
45}