Skip to content

Commit 680cb46

Browse files
committed
Update the configuration structs and the conversion from the CLI to config structs to match the new CLI
Signed-off-by: Sergey "Shnatsel" Davidoff <[email protected]>
1 parent 045d6a8 commit 680cb46

File tree

3 files changed

+48
-91
lines changed

3 files changed

+48
-91
lines changed

cargo-cyclonedx/src/cli.rs

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use cargo_cyclonedx::{
22
config::{
3-
CdxExtension, CustomPrefix, Describe, Features, IncludedDependencies, LicenseParserOptions,
4-
OutputOptions, ParseMode, PlatformSuffix, Prefix, PrefixError, SbomConfig, Target,
3+
CdxExtension, Describe, Features, FilenameOverride, FilenameOverrideError, FilenamePattern,
4+
IncludedDependencies, LicenseParserOptions, OutputOptions, ParseMode, PlatformSuffix,
5+
SbomConfig, Target,
56
},
67
format::Format,
78
platform::host_platform,
@@ -104,15 +105,6 @@ impl Args {
104105
_ => None,
105106
};
106107

107-
let prefix = match (self.output_pattern, &self.output_prefix) {
108-
(Some(pattern), _) => Some(Prefix::Pattern(pattern)),
109-
(_, Some(prefix)) => {
110-
let prefix = CustomPrefix::new(prefix)?;
111-
Some(Prefix::Custom(prefix))
112-
}
113-
(_, _) => None,
114-
};
115-
116108
let features =
117109
if !self.all_features && !self.no_default_features && self.features.is_empty() {
118110
None
@@ -155,27 +147,18 @@ impl Args {
155147
false => PlatformSuffix::NotIncluded,
156148
};
157149

158-
// according to the CycloneDX spec, the file has either be called 'bom.xml'
159-
// or include the .cdx extension:
160-
// https://cyclonedx.org/specification/overview/#recognized-file-patterns
161-
if self.target_in_filename {
162-
cdx_extension = Some(CdxExtension::Included)
163-
}
164-
// Ditto for any kind of prefix or anything not named 'bom'
165-
if prefix.is_some() {
166-
cdx_extension = Some(CdxExtension::Included)
150+
let filename_pattern = match self.filename_override {
151+
Some(string) => {
152+
let name_override = FilenameOverride::new(string)?;
153+
FilenamePattern::Custom(name_override);
154+
}
155+
None => FilenamePattern::CrateName,
167156
};
168157

169-
let output_options =
170-
if cdx_extension.is_none() && prefix.is_none() && !self.target_in_filename {
171-
None
172-
} else {
173-
Some(OutputOptions {
174-
cdx_extension: cdx_extension.unwrap_or_default(),
175-
prefix: prefix.unwrap_or_default(),
176-
platform_suffix,
177-
})
178-
};
158+
Some(OutputOptions {
159+
filename: filename_pattern,
160+
platform_suffix,
161+
});
179162

180163
let license_parser = Some(LicenseParserOptions {
181164
mode: match self.license_strict {
@@ -202,7 +185,7 @@ impl Args {
202185
#[derive(Error, Debug, PartialEq, Eq)]
203186
pub enum ArgsError {
204187
#[error("Invalid prefix from CLI")]
205-
CustomPrefixError(#[from] PrefixError),
188+
CustomPrefixError(#[from] FilenameOverrideError),
206189
}
207190

208191
#[cfg(test)]

cargo-cyclonedx/src/config.rs

Lines changed: 24 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -93,39 +93,12 @@ impl FromStr for IncludedDependencies {
9393
}
9494
}
9595

96-
#[derive(Debug, Clone, PartialEq, Eq)]
96+
#[derive(Debug, Default, Clone, PartialEq, Eq)]
9797
pub struct OutputOptions {
98-
pub cdx_extension: CdxExtension,
99-
pub prefix: Prefix,
98+
pub filename: FilenamePattern,
10099
pub platform_suffix: PlatformSuffix,
101100
}
102101

103-
impl Default for OutputOptions {
104-
fn default() -> Self {
105-
Self {
106-
cdx_extension: CdxExtension::default(),
107-
prefix: Prefix::Pattern(Pattern::Bom),
108-
platform_suffix: PlatformSuffix::default(),
109-
}
110-
}
111-
}
112-
113-
#[derive(Debug, Default, Clone, PartialEq, Eq)]
114-
pub enum CdxExtension {
115-
Included,
116-
#[default]
117-
NotIncluded,
118-
}
119-
120-
impl CdxExtension {
121-
pub fn extension(&self) -> String {
122-
match &self {
123-
CdxExtension::Included => ".cdx".to_string(),
124-
CdxExtension::NotIncluded => "".to_string(),
125-
}
126-
}
127-
}
128-
129102
#[derive(Debug, Default, Clone, PartialEq, Eq)]
130103
pub struct Features {
131104
pub all_features: bool,
@@ -150,14 +123,14 @@ impl Target {
150123
}
151124

152125
#[derive(Debug, Clone, PartialEq, Eq)]
153-
pub enum Prefix {
154-
Pattern(Pattern),
155-
Custom(CustomPrefix),
126+
pub enum FilenamePattern {
127+
CrateName,
128+
Custom(FilenameOverride),
156129
}
157130

158-
impl Default for Prefix {
131+
impl Default for FilenamePattern {
159132
fn default() -> Self {
160-
Self::Pattern(Pattern::default())
133+
Self::CrateName
161134
}
162135
}
163136

@@ -187,14 +160,14 @@ impl FromStr for Pattern {
187160
}
188161

189162
#[derive(Debug, Clone, PartialEq, Eq)]
190-
pub struct CustomPrefix(String);
163+
pub struct FilenameOverride(String);
191164

192-
impl CustomPrefix {
193-
pub fn new(custom_prefix: impl Into<String>) -> Result<Self, PrefixError> {
165+
impl FilenameOverride {
166+
pub fn new(custom_prefix: impl Into<String>) -> Result<Self, FilenameOverrideError> {
194167
let prefix = custom_prefix.into();
195168

196169
if prefix.contains(std::path::MAIN_SEPARATOR) {
197-
Err(PrefixError::CustomPrefixError(
170+
Err(FilenameOverrideError::TheOne(
198171
std::path::MAIN_SEPARATOR.to_string(),
199172
))
200173
} else {
@@ -203,16 +176,16 @@ impl CustomPrefix {
203176
}
204177
}
205178

206-
impl ToString for CustomPrefix {
179+
impl ToString for FilenameOverride {
207180
fn to_string(&self) -> String {
208181
self.0.clone()
209182
}
210183
}
211184

212185
#[derive(Error, Debug, PartialEq, Eq)]
213-
pub enum PrefixError {
186+
pub enum FilenameOverrideError {
214187
#[error("Illegal characters in custom prefix string: {0}")]
215-
CustomPrefixError(String),
188+
TheOne(String),
216189
}
217190

218191
#[derive(Debug, Default, Clone, PartialEq, Eq)]
@@ -285,25 +258,25 @@ mod test {
285258
use super::*;
286259

287260
#[test]
288-
fn it_should_error_for_a_prefix_with_a_path_separator() {
289-
let prefix = format!("directory{}prefix", std::path::MAIN_SEPARATOR);
261+
fn it_should_error_for_a_filename_with_a_path_separator() {
262+
let filename = format!("directory{}filename", std::path::MAIN_SEPARATOR);
290263

291-
let actual = CustomPrefix::new(prefix)
292-
.expect_err("Should not have been able to create CustomPrefix with path separator");
264+
let actual = FilenameOverride::new(filename)
265+
.expect_err("Should not have been able to create Customfilename with path separator");
293266

294-
let expected = PrefixError::CustomPrefixError(std::path::MAIN_SEPARATOR.to_string());
267+
let expected = FilenameOverrideError::TheOne(std::path::MAIN_SEPARATOR.to_string());
295268

296269
assert_eq!(actual, expected);
297270
}
298271

299272
#[test]
300-
fn it_should_create_a_custom_prefix_from_a_valid_string() {
301-
let prefix = "customprefix".to_string();
273+
fn it_should_create_a_custom_filename_from_a_valid_string() {
274+
let filename = "customfilename".to_string();
302275

303-
let actual = CustomPrefix::new(prefix.clone())
304-
.expect("Should have been able to create CustomPrefix");
276+
let actual = FilenameOverride::new(filename.clone())
277+
.expect("Should have been able to create Customfilename");
305278

306-
let expected = CustomPrefix(prefix);
279+
let expected = FilenameOverride(filename);
307280

308281
assert_eq!(actual, expected);
309282
}

cargo-cyclonedx/src/generator.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*
1616
* SPDX-License-Identifier: Apache-2.0
1717
*/
18+
use crate::config::FilenamePattern;
1819
use crate::config::Pattern;
1920
use crate::config::PlatformSuffix;
20-
use crate::config::Prefix;
2121
use crate::config::SbomConfig;
2222
use crate::config::{IncludedDependencies, ParseMode};
2323
use crate::format::Format;
@@ -690,11 +690,12 @@ 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> {
692692
match self.sbom_config.output_options().prefix {
693-
Prefix::Pattern(Pattern::Bom | Pattern::Package) | Prefix::Custom(_) => {
693+
FilenamePattern::Pattern(Pattern::Bom | Pattern::Package)
694+
| FilenamePattern::Custom(_) => {
694695
let path = self.manifest_path.with_file_name(self.filename(None, &[]));
695696
Self::write_to_file(self.bom, &path, &self.sbom_config)
696697
}
697-
Prefix::Pattern(pattern @ (Pattern::Binary | Pattern::CargoTarget)) => {
698+
FilenamePattern::Pattern(pattern @ (Pattern::Binary | Pattern::CargoTarget)) => {
698699
for (sbom, target_kind) in
699700
Self::per_artifact_sboms(&self.bom, &self.target_kinds, pattern)
700701
{
@@ -786,17 +787,17 @@ impl GeneratedSbom {
786787
fn filename(&self, binary_name: Option<&str>, target_kind: &[String]) -> String {
787788
let output_options = self.sbom_config.output_options();
788789
let prefix = match &output_options.prefix {
789-
Prefix::Pattern(Pattern::Bom) => "bom".to_string(),
790-
Prefix::Pattern(Pattern::Package) => self.package_name.clone(),
791-
Prefix::Pattern(Pattern::Binary) => binary_name.unwrap().to_owned(),
792-
Prefix::Pattern(Pattern::CargoTarget) => binary_name.unwrap().to_owned(),
793-
Prefix::Custom(c) => c.to_string(),
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(),
794795
};
795796

796797
let target_kind_suffix = if !target_kind.is_empty() {
797798
debug_assert!(matches!(
798799
&output_options.prefix,
799-
Prefix::Pattern(Pattern::Binary | Pattern::CargoTarget)
800+
FilenamePattern::Pattern(Pattern::Binary | Pattern::CargoTarget)
800801
));
801802
format!("_{}", target_kind.join("-"))
802803
} else {

0 commit comments

Comments
 (0)