Skip to content

Commit

Permalink
Merge pull request #9 from louib/add_license_support
Browse files Browse the repository at this point in the history
feat: add license support
  • Loading branch information
louib committed Aug 23, 2023
2 parents 11af448 + 3850a83 commit c78172f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
32 changes: 31 additions & 1 deletion src/cyclone_dx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use serde::{de::Deserialize, ser::Serialize};

use serde_cyclonedx::cyclonedx::v_1_4::{
Commit, CommitBuilder, Component, ComponentBuilder, ComponentPedigree, ComponentPedigreeBuilder,
CycloneDxBuilder, ExternalReference, ExternalReferenceBuilder, Metadata, ToolBuilder,
CycloneDxBuilder, ExternalReference, ExternalReferenceBuilder, License, LicenseBuilder, LicenseChoice,
Metadata, ToolBuilder,
};

const CURRENT_SPEC_VERSION: &str = "1.4";
Expand Down Expand Up @@ -118,5 +119,34 @@ pub fn dump_derivation(derivation_path: &str, package_node: &crate::nix::Package
);
}

let mut licenses: Vec<LicenseChoice> = vec![];
for license in &package_node.package.meta.get_licenses() {
match license {
crate::nix::PackageLicense::Name(n) => {
licenses.push(LicenseChoice {
expression: Some(n.to_string()),
license: None,
});
}
crate::nix::PackageLicense::Details(license_details) => {
let mut license_builder = LicenseBuilder::default();
match &license_details.spdx_id {
None => continue,
Some(id) => license_builder.id(id),
};
if let Some(full_name) = &license_details.full_name {
license_builder.name(full_name);
}
licenses.push(LicenseChoice {
expression: None,
license: Some(license_builder.build().unwrap()),
});
}
}
}
if licenses.len() != 0 {
component_builder.licenses(licenses);
}

Some(component_builder.build().unwrap())
}
11 changes: 9 additions & 2 deletions src/nix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,14 @@ pub struct PackageMeta {
pub license: Option<License>,
}
impl PackageMeta {
pub fn get_licenses(&self) -> Vec<LicenseDetails> {
vec![]
pub fn get_licenses(&self) -> Vec<PackageLicense> {
match &self.license {
Some(h) => match h {
License::One(license) => vec![license.clone()],
License::Many(licenses) => licenses.clone(),
},
None => vec![],
}
}
pub fn get_homepages(&self) -> Vec<String> {
match &self.homepage {
Expand Down Expand Up @@ -316,6 +322,7 @@ pub enum PackageLicense {
}

#[derive(Debug)]
#[derive(Default)]
#[derive(Clone)]
#[derive(Deserialize)]
pub struct LicenseDetails {
Expand Down

0 comments on commit c78172f

Please sign in to comment.