Skip to content

Commit a966c52

Browse files
committed
Refactor BomError variant to use SpecVersion
This changes the `BomV13SerializationError` to `BomSerializationError` with added `SpecVersion` parameter. * add test to check conversion fails Signed-off-by: Sebastian Ziebell <[email protected]>
1 parent 5a15088 commit a966c52

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

cyclonedx-bom/src/errors.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* SPDX-License-Identifier: Apache-2.0
1717
*/
1818

19+
use crate::models::bom::SpecVersion;
20+
1921
#[derive(Debug, thiserror::Error)]
2022
#[non_exhaustive]
2123
pub enum BomError {
@@ -25,8 +27,8 @@ pub enum BomError {
2527
#[error("Failed to serialize BOM to XML: {0}")]
2628
XmlSerializationError(String),
2729

28-
#[error("Failed to serialize BOM to v1.3: {0}")]
29-
BomV13SerializationError(String),
30+
#[error("Failed to serialize BOM with version {0:?}: {1}")]
31+
BomSerializationError(SpecVersion, String),
3032

3133
#[error("Unsupported Spec Version '{0}'")]
3234
UnsupportedSpecVersion(String),

cyclonedx-bom/src/specs/v1_3/component.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use std::convert::TryFrom;
2020

2121
use crate::errors::BomError;
22-
use crate::errors::BomError::BomV13SerializationError;
22+
use crate::errors::BomError::BomSerializationError;
2323
use crate::utilities::try_convert_optional;
2424
use crate::{
2525
errors::XmlReadError,
@@ -166,7 +166,10 @@ impl TryFrom<models::component::Component> for Component {
166166

167167
fn try_from(other: models::component::Component) -> Result<Self, Self::Error> {
168168
match other.version {
169-
None => Err(BomV13SerializationError("version missing".to_string())),
169+
None => Err(BomSerializationError(
170+
models::bom::SpecVersion::V1_3,
171+
"version missing".to_string(),
172+
)),
170173
Some(version) => Ok(Self {
171174
component_type: other.component_type.to_string(),
172175
mime_type: other.mime_type.map(|m| MimeType(m.0)),
@@ -1171,6 +1174,7 @@ impl From<MimeType> for models::component::MimeType {
11711174
#[cfg(test)]
11721175
pub(crate) mod test {
11731176
use crate::{
1177+
models::bom::SpecVersion,
11741178
specs::v1_3::{
11751179
attached_text::test::{corresponding_attached_text, example_attached_text},
11761180
code::test::{
@@ -1463,4 +1467,16 @@ pub(crate) mod test {
14631467
let expected = example_components();
14641468
assert_eq!(actual, expected);
14651469
}
1470+
1471+
#[test]
1472+
fn it_should_fail_conversion_without_version_field() {
1473+
let mut component = corresponding_component();
1474+
component.version = None;
1475+
1476+
let result = Component::try_from(component);
1477+
assert!(matches!(
1478+
result,
1479+
Err(BomError::BomSerializationError(SpecVersion::V1_3, _))
1480+
));
1481+
}
14661482
}

0 commit comments

Comments
 (0)