Skip to content

Commit 6b2fd1c

Browse files
committed
Add support for serde traits to cyclonedx-bom types
Signed-off-by: Naxdy <naxdy@naxdy.org>
1 parent d5c35fd commit 6b2fd1c

43 files changed

Lines changed: 274 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cyclonedx-bom/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ strum = { version = "0.28.0", features = ["derive"] }
3636
insta = { version = "1.33.0", features = ["glob", "json"] }
3737
pretty_assertions = "1.4.0"
3838
test-utils = {path = "test-utils"}
39+
40+
[features]
41+
default = []
42+
serde = ["ordered-float/serde"]

cyclonedx-bom/src/external_models/date_time.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
use std::convert::TryFrom;
2020

21+
#[cfg(feature = "serde")]
22+
use serde::{Deserialize, Serialize};
2123
use thiserror::Error;
2224
use time::{format_description::well_known::Iso8601, OffsetDateTime};
2325

@@ -39,6 +41,7 @@ use crate::validation::ValidationError;
3941
/// assert_eq!(date_time.to_string(), timestamp);
4042
/// ```
4143
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
44+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4245
pub struct DateTime(pub(crate) String);
4346

4447
pub fn validate_date_time(date_time: &DateTime) -> Result<(), ValidationError> {

cyclonedx-bom/src/external_models/normalized_string.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
*/
1818

1919
use crate::validation::ValidationError;
20+
#[cfg(feature = "serde")]
21+
use serde::{Deserialize, Serialize};
2022
use std::fmt::Display;
2123
use std::ops::Deref;
2224

2325
/// A string that does not contain carriage return, line feed, or tab characters
2426
///
2527
/// Defined via the [XML schema](https://www.w3.org/TR/xmlschema-2/#normalizedString)
2628
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
29+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2730
pub struct NormalizedString(pub(crate) String);
2831

2932
impl NormalizedString {

cyclonedx-bom/src/external_models/spdx.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
use std::convert::TryFrom;
2020

21+
#[cfg(feature = "serde")]
22+
use serde::{Deserialize, Serialize};
2123
use spdx::{Expression, ParseMode};
2224
use thiserror::Error;
2325

@@ -37,6 +39,7 @@ use crate::{models::bom::BomReference, validation::ValidationError};
3739
/// # Ok::<(), SpdxIdentifierError>(())
3840
/// ```
3941
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
42+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4043
pub struct SpdxIdentifier(pub(crate) String);
4144

4245
impl SpdxIdentifier {
@@ -111,6 +114,7 @@ pub enum SpdxIdentifierError {
111114
/// # Ok::<(), SpdxExpressionError>(())
112115
/// ```
113116
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
117+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
114118
pub struct SpdxExpression {
115119
pub(crate) bom_ref: Option<BomReference>,
116120
pub(crate) expression: String,

cyclonedx-bom/src/external_models/uri.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use std::{convert::TryFrom, str::FromStr};
2020

2121
use fluent_uri::UriRef as Url;
2222
use purl::{GenericPurl, GenericPurlBuilder};
23+
#[cfg(feature = "serde")]
24+
use serde::{Deserialize, Serialize};
2325
use thiserror::Error;
2426

2527
use crate::validation::ValidationError;
@@ -32,6 +34,7 @@ pub fn validate_purl(purl: &Purl) -> Result<(), ValidationError> {
3234
}
3335

3436
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
37+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3538
pub struct Purl(pub(crate) String);
3639

3740
impl Purl {
@@ -72,6 +75,7 @@ pub fn validate_uri(uri: &Uri) -> Result<(), ValidationError> {
7275
}
7376

7477
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
78+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7579
pub struct Uri(pub(crate) String);
7680

7781
impl Uri {

cyclonedx-bom/src/models/advisory.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ use crate::external_models::normalized_string::validate_normalized_string;
2020
use crate::external_models::uri::validate_uri;
2121
use crate::external_models::{normalized_string::NormalizedString, uri::Uri};
2222
use crate::validation::{Validate, ValidationContext, ValidationResult};
23+
#[cfg(feature = "serde")]
24+
use serde::{Deserialize, Serialize};
2325

2426
use super::bom::SpecVersion;
2527

2628
/// Represents an advisory, a notification of a threat to a component, service, or system.
2729
///
2830
/// Defined via the [XML schema](https://cyclonedx.org/docs/1.4/xml/#type_advisoryType)
2931
#[derive(Clone, Debug, PartialEq, Eq)]
32+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3033
pub struct Advisory {
3134
pub title: Option<NormalizedString>,
3235
pub url: Uri,
@@ -58,6 +61,7 @@ impl Validate for Advisory {
5861
}
5962

6063
#[derive(Clone, Debug, PartialEq, Eq)]
64+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6165
pub struct Advisories(pub Vec<Advisory>);
6266

6367
impl Validate for Advisories {

cyclonedx-bom/src/models/annotation.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ use crate::{
2727
prelude::{Validate, ValidationResult},
2828
validation::ValidationContext,
2929
};
30+
#[cfg(feature = "serde")]
31+
use serde::{Deserialize, Serialize};
3032

3133
use super::bom::SpecVersion;
3234

3335
#[derive(Clone, Debug, PartialEq, Eq)]
36+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3437
pub struct Annotations(pub Vec<Annotation>);
3538

3639
impl Validate for Annotations {
@@ -44,6 +47,7 @@ impl Validate for Annotations {
4447
}
4548

4649
#[derive(Clone, Debug, PartialEq, Eq)]
50+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4751
pub struct Annotation {
4852
pub bom_ref: Option<String>,
4953
pub subjects: Vec<String>,
@@ -64,6 +68,7 @@ impl Validate for Annotation {
6468

6569
/// Represents an Annotator: organization, individual, component or service.
6670
#[derive(Clone, Debug, PartialEq, Eq)]
71+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6772
pub enum Annotator {
6873
Organization(OrganizationalEntity),
6974
Individual(OrganizationalContact),

cyclonedx-bom/src/models/attached_text.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ use crate::{
2222
external_models::normalized_string::{validate_normalized_string, NormalizedString},
2323
validation::{Validate, ValidationContext, ValidationError, ValidationResult},
2424
};
25+
#[cfg(feature = "serde")]
26+
use serde::{Deserialize, Serialize};
2527

2628
use super::bom::SpecVersion;
2729

2830
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
31+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2932
pub struct AttachedText {
3033
pub content_type: Option<NormalizedString>,
3134
pub encoding: Option<Encoding>,
@@ -82,6 +85,8 @@ pub fn validate_encoding(encoding: &Encoding) -> Result<(), ValidationError> {
8285
}
8386

8487
#[derive(Clone, Debug, PartialEq, Eq, strum::Display, Hash)]
88+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
89+
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
8590
#[strum(serialize_all = "kebab-case")]
8691
pub enum Encoding {
8792
Base64,

cyclonedx-bom/src/models/attachment.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ use crate::{
2020
prelude::{Validate, ValidationResult},
2121
validation::{ValidationContext, ValidationError},
2222
};
23+
#[cfg(feature = "serde")]
24+
use serde::{Deserialize, Serialize};
2325

2426
use super::bom::SpecVersion;
2527

2628
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
29+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2730
pub struct Attachment {
2831
pub content: String,
2932
pub content_type: Option<String>,

0 commit comments

Comments
 (0)