Skip to content

Commit

Permalink
Merge pull request #9 from tweag/homepage
Browse files Browse the repository at this point in the history
Parse and propagate the meta homepage
  • Loading branch information
Erin van der Veen authored Jan 5, 2024
2 parents 988a0f7 + cd9ae52 commit 73c4ad7
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 10 deletions.
48 changes: 45 additions & 3 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub(crate) struct ModelComponent {
pub(crate) r#ref: String,
pub(crate) version: String,
pub(crate) description: String,
pub(crate) external_references: Vec<ModelExternalReference>,
}

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

#[derive(Debug)]
pub(crate) struct ModelExternalReference {
pub(crate) url: String,
pub(crate) r#type: ModelExternalReferenceType,
}

#[derive(Debug)]
pub(crate) enum ModelExternalReferenceType {
Website,
}

#[derive(Debug)]
pub(crate) struct ModelDependency {
pub(crate) r#ref: String,
Expand All @@ -40,15 +52,45 @@ impl Into<String> for ModelType {
}
}

impl Into<String> for ModelExternalReferenceType {
fn into(self) -> String {
match self {
ModelExternalReferenceType::Website => "website".to_owned(),
}
}
}

impl From<ModelComponent> for cyclonedx::Component {
// TODO: Error
fn from(model_component: ModelComponent) -> Self {
cyclonedx::ComponentBuilder::default()
let mut builder = cyclonedx::ComponentBuilder::default();
let mut builder = builder
.type_(model_component.r#type)
.name(model_component.name)
.bom_ref(model_component.r#ref)
.version(model_component.version)
.description(model_component.description)
.description(model_component.description);

if !model_component.version.is_empty() {
builder = builder.version(model_component.version.clone());
}

let external_references: Vec<cyclonedx::ExternalReference> = model_component
.external_references
.into_iter()
.map(Into::into)
.collect();

builder.external_references(external_references);

builder.build().unwrap()
}
}

impl From<ModelExternalReference> for cyclonedx::ExternalReference {
fn from(model_external_reference: ModelExternalReference) -> Self {
cyclonedx::ExternalReferenceBuilder::default()
.url(model_external_reference.url)
.type_(model_external_reference.r#type)
.build()
.unwrap()
}
Expand Down
31 changes: 24 additions & 7 deletions src/nixtract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use serde::Deserialize;

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

#[derive(Deserialize, Debug)]
pub(crate) struct Nixtract {
Expand Down Expand Up @@ -43,6 +46,7 @@ pub(crate) struct NixtractNixpkgsMetadata {
pub(crate) version: String,
pub(crate) broken: bool,
pub(crate) license: String,
pub(crate) homepage: String,
}

#[derive(Deserialize, Debug)]
Expand All @@ -57,12 +61,25 @@ impl From<Nixtract> for Model {
let components: Vec<ModelComponent> = nixtract
.entries
.iter()
.map(|entry| ModelComponent {
r#type: ModelType::Application,
name: entry.parsed_name.name.clone(),
r#ref: entry.output_path.clone(),
version: entry.nixpkgs_metadata.version.clone(),
description: entry.nixpkgs_metadata.description.clone(),
.map(|entry| {
let external_references = {
let mut acc = vec![];
if !entry.nixpkgs_metadata.homepage.is_empty() {
acc.push(ModelExternalReference {
url: entry.nixpkgs_metadata.homepage.clone(),
r#type: ModelExternalReferenceType::Website,
})
}
acc
};
ModelComponent {
r#type: ModelType::Application,
name: entry.parsed_name.name.clone(),
r#ref: entry.output_path.clone(),
version: entry.nixpkgs_metadata.version.clone(),
description: entry.nixpkgs_metadata.description.clone(),
external_references,
}
})
.collect();

Expand Down
Loading

0 comments on commit 73c4ad7

Please sign in to comment.