1+ use crate :: config:: Describe ;
12/*
23 * This file is part of CycloneDX Rust Cargo.
34 *
1617 * SPDX-License-Identifier: Apache-2.0
1718 */
1819use crate :: config:: FilenamePattern ;
19- use crate :: config:: Pattern ;
2020use crate :: config:: PlatformSuffix ;
2121use crate :: config:: SbomConfig ;
2222use crate :: config:: { IncludedDependencies , ParseMode } ;
@@ -689,13 +689,12 @@ pub struct GeneratedSbom {
689689impl 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