Skip to content

Commit

Permalink
feat: support cyclonedx 1.5 - ValidationResult management
Browse files Browse the repository at this point in the history
Signed-off-by: mrizzi <[email protected]>
  • Loading branch information
mrizzi authored and dejanb committed Sep 5, 2024
1 parent 7e0502d commit 9f02a74
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions bombastic/model/src/data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::fmt::Formatter;

use cyclonedx_bom::errors::JsonReadError;
use cyclonedx_bom::prelude::Validate;
use cyclonedx_bom::prelude::{Validate, ValidationResult};
use cyclonedx_bom::validation::ValidationErrorsKind;
use std::collections::HashSet;
use std::fmt::Formatter;
use tracing::{info_span, instrument};

#[derive(Debug)]
Expand Down Expand Up @@ -80,14 +81,13 @@ impl SBOM {
match result.passed() {
true => return Ok(SBOM::CycloneDX(bom)),
false => {
let all_reasons = result
.errors()
let all_reasons = Self::get_validation_error_messages(result.clone())
.into_iter()
// Ignore normalizedstring errors
// until https://github.com/CycloneDX/cyclonedx-rust-cargo/issues/737 is fixed
.filter(|(reason, _)| {
.filter(|reason| {
reason != "NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
})
.map(|(reason, _)| reason)
.collect::<Vec<String>>()
.join(", ");
if all_reasons.is_empty() {
Expand Down Expand Up @@ -118,6 +118,32 @@ impl SBOM {
Err(err)
}

fn get_validation_error_messages(validation_result: ValidationResult) -> HashSet<String> {
let mut result = HashSet::<String>::new();
validation_result.errors().for_each(|(_, error_kind)| match error_kind {
ValidationErrorsKind::Struct(value) => {
result.extend(Self::get_validation_error_messages(value));
}
ValidationErrorsKind::List(value) => value.into_values().for_each(|validation_result| {
result.extend(Self::get_validation_error_messages(validation_result));
}),
ValidationErrorsKind::Field(value) => {
value.into_iter().for_each(|error| {
result.insert(error.message);
});
}
ValidationErrorsKind::Enum(value) => {
result.insert(value.message);
}
ValidationErrorsKind::Custom(value) => {
value.into_iter().for_each(|error| {
result.insert(error.message);
});
}
});
result
}

pub fn type_str(&self) -> String {
match self {
#[cfg(feature = "spdx-rs")]
Expand Down

0 comments on commit 9f02a74

Please sign in to comment.