Skip to content

Commit

Permalink
Construct CycloneDX from internal model
Browse files Browse the repository at this point in the history
  • Loading branch information
Erin van der Veen committed Dec 12, 2023
1 parent 9166008 commit 8d2b45d
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 2 deletions.
249 changes: 248 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ edition = "2021"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde-cyclonedx = "0.8"
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ fn main() -> Result<(), io::Error> {
};

let model: Model = nixtract.into();
let cyclonedx = model.to_cyclonedx();
let json_out = serde_json::to_string(&cyclonedx).unwrap();

println!("{:#?}", model);
println!("{}", json_out);

Ok(())
}
39 changes: 39 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! This module contains Genealogos' internal representation of incomming data.
//! Since the initial target of Genealogos is CycloneDX, this model is largely based on their representation.
use serde_cyclonedx::cyclonedx::v_1_5 as cyclonedx;

#[derive(Debug)]
pub(crate) struct Model {
Expand All @@ -19,3 +20,41 @@ pub(crate) enum ModelType {
/// component.
Application,
}

impl Into<String> for ModelType {
fn into(self) -> String {
match self {
ModelType::Application => "application".to_owned(),
}
}
}

impl ModelComponent {
// TODO: Error
pub(crate) fn to_cyclonedx(self) -> cyclonedx::Component {
cyclonedx::ComponentBuilder::default()
.type_(self.r#type)
.name(self.name)
.build()
.unwrap()
}
}

impl Model {
// TODO: Error
pub(crate) fn to_cyclonedx(self) -> cyclonedx::CycloneDx {
let components: Vec<cyclonedx::Component> = self
.components
.into_iter()
.map(|component| component.to_cyclonedx())
.collect();

cyclonedx::CycloneDxBuilder::default()
.bom_format("CycloneDX")
.spec_version("1.5")
.version(1)
.components(components)
.build()
.unwrap()
}
}

0 comments on commit 8d2b45d

Please sign in to comment.