cyclonedx_rust/component/
external_reference.rs

1use serde::{Deserialize, Serialize};
2use yaserde_derive::{YaDeserialize, YaSerialize};
3
4#[derive(Clone, PartialEq, Debug, Serialize, Deserialize, YaSerialize, YaDeserialize)]
5#[serde(rename = "reference")]
6#[yaserde(rename = "reference")]
7#[yaserde(
8    prefix = "ns",
9    default_namespace = "ns",
10    namespace = "ns: http://cyclonedx.org/schema/bom/1.2"
11)]
12pub struct ExternalReference {
13    #[serde(rename = "type")]
14    #[yaserde(rename = "type", attribute)]
15    pub ref_type: ExternalReferenceType,
16
17    #[yaserde(prefix = "ns")]
18    pub url: String,
19    #[yaserde(prefix = "ns")]
20    pub comment: Option<String>,
21}
22
23impl ExternalReference {
24    pub fn new(
25        ref_type: ExternalReferenceType,
26        url: String,
27        comment: Option<String>,
28    ) -> ExternalReference {
29        ExternalReference {
30            ref_type,
31            url,
32            comment,
33        }
34    }
35}
36#[derive(Clone, PartialEq, Debug, Serialize, Deserialize, YaSerialize, YaDeserialize)]
37#[serde(rename_all = "kebab-case")]
38pub enum ExternalReferenceType {
39    #[yaserde(rename = "vcs")]
40    Vcs,
41    #[yaserde(rename = "issue-tracker")]
42    IssueTracker,
43    #[yaserde(rename = "website")]
44    Website,
45    #[yaserde(rename = "advisories")]
46    Advisories,
47    #[yaserde(rename = "bom")]
48    Bom,
49    #[yaserde(rename = "mailing-list")]
50    MailingList,
51    #[yaserde(rename = "social")]
52    Social,
53    #[yaserde(rename = "chat")]
54    Chat,
55    #[yaserde(rename = "documentation")]
56    Documentation,
57    #[yaserde(rename = "support")]
58    Support,
59    #[yaserde(rename = "distribution")]
60    Distribution,
61    #[yaserde(rename = "license")]
62    License,
63    #[yaserde(rename = "build-metadata")]
64    BuildMeta,
65    #[yaserde(rename = "build-system")]
66    BuildSystem,
67    #[yaserde(rename = "other")]
68    Other,
69}
70
71impl Default for ExternalReferenceType {
72    fn default() -> Self {
73        ExternalReferenceType::Other
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80    use yaserde::ser::Config;
81
82    #[test]
83    fn print_xml() {
84        let expected = ExternalReference::new(
85            ExternalReferenceType::Documentation,
86            "http://example.org/docs".to_string(),
87            Option::from("All component versions are documented here".to_string()),
88        );
89        let parsed = yaserde::ser::to_string_with_config(
90            &expected,
91            &Config {
92                perform_indent: false,
93                write_document_declaration: false,
94                indent_string: None,
95            },
96        )
97        .unwrap();
98
99        let actual: ExternalReference = yaserde::de::from_str(parsed.as_str()).unwrap();
100
101        assert_eq!(expected, actual);
102    }
103}