Skip to content

Commit 17fc264

Browse files
committed
If it compiles, it works!
Signed-off-by: Sergey "Shnatsel" Davidoff <[email protected]>
1 parent 148967b commit 17fc264

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

cargo-cyclonedx/src/cli.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cargo_cyclonedx::{
22
config::{
3-
CdxExtension, Describe, Features, FilenameOverride, FilenameOverrideError, FilenamePattern,
3+
Describe, Features, FilenameOverride, FilenameOverrideError, FilenamePattern,
44
IncludedDependencies, LicenseParserOptions, OutputOptions, ParseMode, PlatformSuffix,
55
SbomConfig, Target,
66
},
@@ -137,25 +137,20 @@ impl Args {
137137
Target::SingleTarget(target_string)
138138
});
139139

140-
let mut cdx_extension = match self.output_cdx {
141-
true => Some(CdxExtension::Included),
142-
false => None,
143-
};
144-
145140
let platform_suffix = match self.target_in_filename {
146141
true => PlatformSuffix::Included,
147142
false => PlatformSuffix::NotIncluded,
148143
};
149144

150-
let filename_pattern = match self.filename_override {
145+
let filename_pattern = match &self.filename_override {
151146
Some(string) => {
152147
let name_override = FilenameOverride::new(string)?;
153-
FilenamePattern::Custom(name_override);
148+
FilenamePattern::Custom(name_override)
154149
}
155150
None => FilenamePattern::CrateName,
156151
};
157152

158-
Some(OutputOptions {
153+
let output_options = Some(OutputOptions {
159154
filename: filename_pattern,
160155
platform_suffix,
161156
});

cargo-cyclonedx/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ pub enum ParseMode {
229229
}
230230

231231
/// What does the SBOM describe?
232-
#[derive(Clone, Debug, Default, PartialEq, Eq)]
232+
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
233233
pub enum Describe {
234234
/// The entire crate, with Cargo targets as subcomponents
235235
#[default]

cargo-cyclonedx/src/generator.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::config::Describe;
12
/*
23
* This file is part of CycloneDX Rust Cargo.
34
*
@@ -16,7 +17,6 @@
1617
* SPDX-License-Identifier: Apache-2.0
1718
*/
1819
use crate::config::FilenamePattern;
19-
use crate::config::Pattern;
2020
use crate::config::PlatformSuffix;
2121
use crate::config::SbomConfig;
2222
use crate::config::{IncludedDependencies, ParseMode};
@@ -689,13 +689,12 @@ pub struct GeneratedSbom {
689689
impl GeneratedSbom {
690690
/// Writes SBOM to either a JSON or XML file in the same folder as `Cargo.toml` manifest
691691
pub fn write_to_files(self) -> Result<(), SbomWriterError> {
692-
match self.sbom_config.output_options().prefix {
693-
FilenamePattern::Pattern(Pattern::Bom | Pattern::Package)
694-
| FilenamePattern::Custom(_) => {
692+
match self.sbom_config.describe.unwrap_or_default() {
693+
Describe::Crate => {
695694
let path = self.manifest_path.with_file_name(self.filename(None, &[]));
696695
Self::write_to_file(self.bom, &path, &self.sbom_config)
697696
}
698-
FilenamePattern::Pattern(pattern @ (Pattern::Binary | Pattern::CargoTarget)) => {
697+
pattern @ (Describe::Binaries | Describe::AllCargoTargets) => {
699698
for (sbom, target_kind) in
700699
Self::per_artifact_sboms(&self.bom, &self.target_kinds, pattern)
701700
{
@@ -744,7 +743,7 @@ impl GeneratedSbom {
744743
fn per_artifact_sboms<'a>(
745744
bom: &'a Bom,
746745
target_kinds: &'a TargetKinds,
747-
pattern: Pattern,
746+
describe: Describe,
748747
) -> impl Iterator<Item = (Bom, Vec<String>)> + 'a {
749748
let meta = bom.metadata.as_ref().unwrap();
750749
let crate_component = meta.component.as_ref().unwrap();
@@ -755,16 +754,16 @@ impl GeneratedSbom {
755754
.iter()
756755
.filter(move |component| {
757756
let target_kind = &target_kinds.0[component.bom_ref.as_ref().unwrap()];
758-
match pattern {
759-
Pattern::Binary => {
757+
match describe {
758+
Describe::Binaries => {
760759
// only record binary artifacts
761760
// TODO: refactor this to use an enum, coming Soon(tm) to cargo-metadata:
762761
// https://github.com/oli-obk/cargo_metadata/pull/258
763762
target_kind.contains(&"bin".to_owned())
764763
|| target_kind.contains(&"cdylib".to_owned())
765764
}
766-
Pattern::CargoTarget => true, // pass everything through
767-
Pattern::Bom | Pattern::Package => unreachable!(),
765+
Describe::AllCargoTargets => true, // pass everything through
766+
Describe::Crate => unreachable!(),
768767
}
769768
})
770769
.map(|component| {
@@ -786,18 +785,28 @@ impl GeneratedSbom {
786785

787786
fn filename(&self, binary_name: Option<&str>, target_kind: &[String]) -> String {
788787
let output_options = self.sbom_config.output_options();
789-
let prefix = match &output_options.prefix {
790-
FilenamePattern::Pattern(Pattern::Bom) => "bom".to_string(),
791-
FilenamePattern::Pattern(Pattern::Package) => self.package_name.clone(),
792-
FilenamePattern::Pattern(Pattern::Binary) => binary_name.unwrap().to_owned(),
793-
FilenamePattern::Pattern(Pattern::CargoTarget) => binary_name.unwrap().to_owned(),
794-
FilenamePattern::Custom(c) => c.to_string(),
788+
let describe = self.sbom_config.describe.clone().unwrap_or_default();
789+
790+
let mut prefix = match describe {
791+
Describe::Crate => self.package_name.clone(),
792+
Describe::Binaries => binary_name.unwrap().to_owned(),
793+
Describe::AllCargoTargets => binary_name.unwrap().to_owned(),
795794
};
795+
let mut extension = ".cdx";
796+
797+
// Handle overridden filename
798+
match output_options.filename {
799+
FilenamePattern::CrateName => (), // already handled above, nothing more to do
800+
FilenamePattern::Custom(name_override) => {
801+
prefix = name_override.to_string();
802+
extension = ""; // do not append the extension to allow writing to literally "bom.xml" as per spec
803+
}
804+
}
796805

797806
let target_kind_suffix = if !target_kind.is_empty() {
798807
debug_assert!(matches!(
799-
&output_options.prefix,
800-
FilenamePattern::Pattern(Pattern::Binary | Pattern::CargoTarget)
808+
describe,
809+
Describe::Binaries | Describe::AllCargoTargets
801810
));
802811
format!("_{}", target_kind.join("-"))
803812
} else {
@@ -817,7 +826,7 @@ impl GeneratedSbom {
817826
prefix,
818827
target_kind_suffix,
819828
platform_suffix,
820-
output_options.cdx_extension.extension(),
829+
extension,
821830
self.sbom_config.format()
822831
)
823832
}

0 commit comments

Comments
 (0)