Skip to main content

relay_knowledge/domain/operations/software/
export.rs

1use serde::{Deserialize, Serialize};
2
3/// Interoperability profiles supported by the software ontology exporter.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
5pub enum SoftwareExportProfile {
6    #[serde(rename = "spdx-3")]
7    Spdx3,
8    #[serde(rename = "cyclonedx-1.7")]
9    Cyclonedx17,
10    #[serde(rename = "prov-o")]
11    ProvO,
12}
13
14impl SoftwareExportProfile {
15    pub const fn as_str(self) -> &'static str {
16        match self {
17            Self::Spdx3 => "spdx-3",
18            Self::Cyclonedx17 => "cyclonedx-1.7",
19            Self::ProvO => "prov-o",
20        }
21    }
22
23    pub fn parse(value: &str) -> Option<Self> {
24        match value {
25            "spdx-3" => Some(Self::Spdx3),
26            "cyclonedx-1.7" => Some(Self::Cyclonedx17),
27            "prov-o" => Some(Self::ProvO),
28            _ => None,
29        }
30    }
31
32    pub const fn media_type(self) -> &'static str {
33        match self {
34            Self::Spdx3 | Self::ProvO => "application/ld+json",
35            Self::Cyclonedx17 => "application/vnd.cyclonedx+json; version=1.7",
36        }
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::SoftwareExportProfile;
43
44    #[test]
45    fn profile_names_round_trip_without_aliases() {
46        for profile in [
47            SoftwareExportProfile::Spdx3,
48            SoftwareExportProfile::Cyclonedx17,
49            SoftwareExportProfile::ProvO,
50        ] {
51            assert_eq!(
52                SoftwareExportProfile::parse(profile.as_str()),
53                Some(profile)
54            );
55            assert_eq!(
56                serde_json::to_value(profile).expect("profile should serialize"),
57                serde_json::json!(profile.as_str())
58            );
59            assert_eq!(
60                serde_json::from_value::<SoftwareExportProfile>(serde_json::json!(
61                    profile.as_str()
62                ))
63                .expect("profile should deserialize"),
64                profile
65            );
66        }
67        assert_eq!(SoftwareExportProfile::parse("spdx"), None);
68    }
69}