Skip to content

Commit

Permalink
Include dependencies field of cyclonedx spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Erin van der Veen committed Dec 19, 2023
1 parent e74038c commit 1663ef1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 28 deletions.
34 changes: 29 additions & 5 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde_cyclonedx::cyclonedx::v_1_5 as cyclonedx;
#[derive(Debug)]
pub(crate) struct Model {
pub(crate) components: Vec<ModelComponent>,
pub(crate) dependencies: Vec<ModelDependency>,
}

#[derive(Debug)]
Expand All @@ -25,6 +26,12 @@ pub(crate) enum ModelType {
Application,
}

#[derive(Debug)]
pub(crate) struct ModelDependency {
pub(crate) r#ref: String,
pub(crate) depends_on: Vec<String>,
}

impl Into<String> for ModelType {
fn into(self) -> String {
match self {
Expand All @@ -47,21 +54,38 @@ impl From<ModelComponent> for cyclonedx::Component {
}
}

impl From<ModelDependency> for cyclonedx::Dependency {
fn from(model_dependency: ModelDependency) -> Self {
let depends_on: Vec<serde_json::Value> = model_dependency
.depends_on
.into_iter()
.map(Into::into)
.collect();

cyclonedx::DependencyBuilder::default()
.ref_(model_dependency.r#ref)
.depends_on(depends_on)
.build()
.unwrap()
}
}

impl From<Model> for cyclonedx::CycloneDx {
// TODO: Error
fn from(model: Model) -> Self {
let components: Vec<cyclonedx::Component> = model
.components
.into_iter()
.map(|component| component.into())
.collect();
let components: Vec<cyclonedx::Component> =
model.components.into_iter().map(Into::into).collect();

let dependencies: Vec<cyclonedx::Dependency> =
model.dependencies.into_iter().map(Into::into).collect();

cyclonedx::CycloneDxBuilder::default()
.bom_format("CycloneDX")
.spec_version("1.5")
.version(1)
.serial_number(format!("urn:uuid:{}", uuid::Uuid::new_v4()))
.components(components)
.dependencies(dependencies)
.build()
.unwrap()
}
Expand Down
28 changes: 22 additions & 6 deletions src/nixtract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use serde::Deserialize;

use crate::model::{Model, ModelComponent, ModelType};
use crate::model::{Model, ModelComponent, ModelDependency, ModelType};

#[derive(Deserialize, Debug)]
pub(crate) struct Nixtract {
Expand Down Expand Up @@ -56,16 +56,32 @@ impl From<Nixtract> for Model {
fn from(nixtract: Nixtract) -> Self {
let components: Vec<ModelComponent> = nixtract
.entries
.into_iter()
.iter()
.map(|entry| ModelComponent {
r#type: ModelType::Application,
name: entry.parsed_name.name,
name: entry.parsed_name.name.clone(),
r#ref: entry.output_path.clone(),
version: entry.nixpkgs_metadata.version.clone(),
description: entry.nixpkgs_metadata.description.clone(),
})
.collect();

let dependencies: Vec<ModelDependency> = nixtract
.entries
.into_iter()
.map(|entry| ModelDependency {
r#ref: entry.output_path,
version: entry.nixpkgs_metadata.version,
description: entry.nixpkgs_metadata.description,
depends_on: entry
.build_inputs
.into_iter()
.filter_map(|bi| bi.output_path)
.collect(),
})
.collect();

Model { components }
Model {
components,
dependencies,
}
}
}
Loading

0 comments on commit 1663ef1

Please sign in to comment.