Skip to content

Commit 207b555

Browse files
committed
Add support for serde traits to cyclonedx-bom types
1 parent d5c35fd commit 207b555

43 files changed

Lines changed: 212 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use std::convert::TryFrom;
2020

21+
#[cfg(feature = "serde")] use serde::{Deserialize, Serialize};
2122
use thiserror::Error;
2223
use time::{format_description::well_known::Iso8601, OffsetDateTime};
2324

@@ -39,6 +40,7 @@ use crate::validation::ValidationError;
3940
/// assert_eq!(date_time.to_string(), timestamp);
4041
/// ```
4142
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
43+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4244
pub struct DateTime(pub(crate) String);
4345

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

cyclonedx-bom/src/external_models/normalized_string.rs

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

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

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

2931
impl NormalizedString {

cyclonedx-bom/src/external_models/spdx.rs

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

1919
use std::convert::TryFrom;
2020

21+
#[cfg(feature = "serde")] use serde::{Deserialize, Serialize};
2122
use spdx::{Expression, ParseMode};
2223
use thiserror::Error;
2324

@@ -37,6 +38,7 @@ use crate::{models::bom::BomReference, validation::ValidationError};
3738
/// # Ok::<(), SpdxIdentifierError>(())
3839
/// ```
3940
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
41+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4042
pub struct SpdxIdentifier(pub(crate) String);
4143

4244
impl SpdxIdentifier {
@@ -111,6 +113,7 @@ pub enum SpdxIdentifierError {
111113
/// # Ok::<(), SpdxExpressionError>(())
112114
/// ```
113115
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
116+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
114117
pub struct SpdxExpression {
115118
pub(crate) bom_ref: Option<BomReference>,
116119
pub(crate) expression: String,

cyclonedx-bom/src/external_models/uri.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use std::{convert::TryFrom, str::FromStr};
2020

2121
use fluent_uri::UriRef as Url;
22+
#[cfg(feature = "serde")] use serde::{Deserialize, Serialize};
2223
use purl::{GenericPurl, GenericPurlBuilder};
2324
use thiserror::Error;
2425

@@ -32,6 +33,7 @@ pub fn validate_purl(purl: &Purl) -> Result<(), ValidationError> {
3233
}
3334

3435
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
36+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3537
pub struct Purl(pub(crate) String);
3638

3739
impl Purl {
@@ -72,6 +74,7 @@ pub fn validate_uri(uri: &Uri) -> Result<(), ValidationError> {
7274
}
7375

7476
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
77+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7578
pub struct Uri(pub(crate) String);
7679

7780
impl Uri {

cyclonedx-bom/src/models/advisory.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ 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")] use serde::{Deserialize, Serialize};
2324

2425
use super::bom::SpecVersion;
2526

2627
/// Represents an advisory, a notification of a threat to a component, service, or system.
2728
///
2829
/// Defined via the [XML schema](https://cyclonedx.org/docs/1.4/xml/#type_advisoryType)
2930
#[derive(Clone, Debug, PartialEq, Eq)]
31+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3032
pub struct Advisory {
3133
pub title: Option<NormalizedString>,
3234
pub url: Uri,
@@ -58,6 +60,7 @@ impl Validate for Advisory {
5860
}
5961

6062
#[derive(Clone, Debug, PartialEq, Eq)]
63+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6164
pub struct Advisories(pub Vec<Advisory>);
6265

6366
impl Validate for Advisories {

cyclonedx-bom/src/models/annotation.rs

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

3132
use super::bom::SpecVersion;
3233

3334
#[derive(Clone, Debug, PartialEq, Eq)]
35+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3436
pub struct Annotations(pub Vec<Annotation>);
3537

3638
impl Validate for Annotations {
@@ -44,6 +46,7 @@ impl Validate for Annotations {
4446
}
4547

4648
#[derive(Clone, Debug, PartialEq, Eq)]
49+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4750
pub struct Annotation {
4851
pub bom_ref: Option<String>,
4952
pub subjects: Vec<String>,
@@ -64,6 +67,7 @@ impl Validate for Annotation {
6467

6568
/// Represents an Annotator: organization, individual, component or service.
6669
#[derive(Clone, Debug, PartialEq, Eq)]
70+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6771
pub enum Annotator {
6872
Organization(OrganizationalEntity),
6973
Individual(OrganizationalContact),

cyclonedx-bom/src/models/attached_text.rs

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

2627
use super::bom::SpecVersion;
2728

2829
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
30+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2931
pub struct AttachedText {
3032
pub content_type: Option<NormalizedString>,
3133
pub encoding: Option<Encoding>,
@@ -82,6 +84,7 @@ pub fn validate_encoding(encoding: &Encoding) -> Result<(), ValidationError> {
8284
}
8385

8486
#[derive(Clone, Debug, PartialEq, Eq, strum::Display, Hash)]
87+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8588
#[strum(serialize_all = "kebab-case")]
8689
pub enum Encoding {
8790
Base64,

cyclonedx-bom/src/models/attachment.rs

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

2425
use super::bom::SpecVersion;
2526

2627
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
28+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2729
pub struct Attachment {
2830
pub content: String,
2931
pub content_type: Option<String>,

0 commit comments

Comments
 (0)