cyclonedx_rust/common/
license.rs1use crate::common::attached_text::AttachedTextType;
2use derive_builder::Builder;
3use serde::{Deserialize, Serialize};
4use yaserde_derive::{YaDeserialize, YaSerialize};
5
6#[derive(
7 Clone, Default, Builder, PartialEq, Debug, Serialize, Deserialize, YaSerialize, YaDeserialize,
8)]
9#[serde(rename = "licenses")]
10#[yaserde(rename = "licenses")]
11#[yaserde(
12 prefix = "ns",
13 default_namespace = "ns",
14 namespace = "ns: http://cyclonedx.org/schema/bom/1.2"
15)]
16pub struct Licenses {
17 #[yaserde(prefix = "ns")]
18 pub license: Vec<LicenseType>,
19 #[yaserde(prefix = "ns")]
20 pub expression: Option<String>,
21}
22
23#[derive(
24 Default, Clone, Builder, PartialEq, Debug, Serialize, Deserialize, YaSerialize, YaDeserialize,
25)]
26#[yaserde(
27 prefix = "ns",
28 default_namespace = "ns",
29 namespace = "ns: http://cyclonedx.org/schema/bom/1.2"
30)]
31pub struct LicenseType {
32 #[yaserde(prefix = "ns")]
33 pub id: Option<String>,
34 #[yaserde(prefix = "ns")]
35 pub name: Option<String>,
36 pub text: Option<AttachedTextType>,
37 #[yaserde(prefix = "ns")]
38 pub url: Option<String>,
39}
40
41impl LicenseType {
42 pub fn new(
43 id: Option<String>,
44 name: Option<String>,
45 text: Option<AttachedTextType>,
46 url: Option<String>,
47 ) -> LicenseType {
48 LicenseType {
49 id,
50 name,
51 text,
52 url,
53 }
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60 use crate::common::attached_text::*;
61 use yaserde::ser::Config;
62
63 #[test]
64 pub fn print_expression_license_xml() {
65 let licenses: Licenses = LicensesBuilder::default()
66 .license(Vec::new())
67 .expression(Option::from(
68 "EPL-2.0 OR GPL-2.0-with-classpath-exception".to_string(),
69 ))
70 .build()
71 .unwrap();
72
73 let expected = r#"<licenses xmlns="http://cyclonedx.org/schema/bom/1.2"><expression>EPL-2.0 OR GPL-2.0-with-classpath-exception</expression></licenses>"#;
74 let actual = yaserde::ser::to_string_with_config(
75 &licenses,
76 &Config {
77 perform_indent: false,
78 write_document_declaration: false,
79 indent_string: None,
80 },
81 )
82 .unwrap();
83
84 assert_eq!(expected.to_string(), actual);
85 }
86
87 #[test]
88 pub fn print_license_xml() {
89 let expected: Licenses = LicensesBuilder::default()
90 .license(vec![LicenseTypeBuilder::default()
91 .id(Option::from("Apache-2.0".to_string()))
92 .name(None)
93 .text(Option::from(
94 AttachedTextTypeBuilder::default()
95 .content_type(Option::from("text/plain".to_string()))
96 .encoding(Option::from(BomEncoding::Base64))
97 .value("base64_value".to_string())
98 .build()
99 .unwrap(),
100 ))
101 .url(Option::from(
102 "https://www.apache.org/licenses/LICENSE-2.0.txt".to_string(),
103 ))
104 .build()
105 .unwrap()])
106 .expression(None)
107 .build()
108 .unwrap();
109
110 let parsed = yaserde::ser::to_string_with_config(
111 &expected,
112 &Config {
113 perform_indent: false,
114 write_document_declaration: false,
115 indent_string: None,
116 },
117 )
118 .unwrap();
119
120 let actual: Licenses = yaserde::de::from_str(parsed.as_str()).unwrap();
121 assert_eq!(expected, actual);
122 }
123}