Skip to content

Commit

Permalink
refactor: create Format::dump fn
Browse files Browse the repository at this point in the history
  • Loading branch information
louib committed Nov 11, 2023
1 parent 53b3b3b commit 850aee4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 28 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ edition = "2021"
[dependencies]
clap = { version = "4", features = ["derive"] }

thiserror = "1"

serde_json = "1.0"
serde_yaml = "0.9"
serde = { version = "1.0", features = ["derive"] }
Expand Down
13 changes: 13 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
#[error("the data for key `{0}` is not available")]
InvalidFormat(String),

#[error("{0} is not supported yet.")]
UnsupportedFormat(String),

#[error("{0}")]
UnknownError(String),
}
36 changes: 8 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use clap::Parser;

mod consts;
mod cyclone_dx;
mod errors;
mod logger;
mod nix;
mod sbom;
Expand Down Expand Up @@ -93,37 +94,16 @@ fn main() -> Result<std::process::ExitCode, Box<dyn std::error::Error>> {
log::info!("{} nodes in the package graph", package_graph.len());

log::info!("Creating the SBOM");
match output_format {
crate::sbom::Format::CycloneDX => {
let output = crate::cyclone_dx::dump(&package_graph, &serialization_format);
match output {
Ok(o) => {
println!("{}", &o);
}
Err(e) => {
eprintln!("{}", e.to_string());
return Ok(std::process::ExitCode::FAILURE);
}
};
}
crate::sbom::Format::SPDX => {
eprintln!(
"{} is not supported yet.",
crate::sbom::Format::SPDX.to_pretty_name()
);

let sbom_dump = match output_format.dump(&serialization_format, &package_graph) {
Ok(d) => d,
Err(e) => {
eprintln!("{}", e.to_string());
return Ok(std::process::ExitCode::FAILURE);
}
crate::sbom::Format::PrettyPrint => {
let display_options = crate::nix::DisplayOptions {
print_stdenv: false,
print_exclude_list: vec![],
};
println!(
"{}",
crate::nix::pretty_print_package_graph(&package_graph, 0, &display_options,)
);
}
};

println!("{}", sbom_dump);

Ok(std::process::ExitCode::SUCCESS)
}
30 changes: 30 additions & 0 deletions src/sbom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,36 @@ impl Format {
crate::sbom::Format::PrettyPrint => crate::sbom::SerializationFormat::XML,
}
}

pub fn dump(
&self,
serialization_format: &SerializationFormat,
package_graph: &crate::nix::PackageGraph,
) -> Result<String, Box<dyn std::error::Error>> {
match self {
crate::sbom::Format::CycloneDX => {
return match crate::cyclone_dx::dump(&package_graph, &serialization_format) {
Ok(d) => Ok(d),
Err(s) => Err(Box::new(crate::errors::Error::UnknownError(s))),
};
}
crate::sbom::Format::SPDX => Err(Box::new(crate::errors::Error::UnsupportedFormat(
"spdx".to_string(),
))),
crate::sbom::Format::PrettyPrint => {
let display_options = crate::nix::DisplayOptions {
print_stdenv: false,
print_exclude_list: vec![],
};

return Ok(crate::nix::pretty_print_package_graph(
&package_graph,
0,
&display_options,
));
}
}
}
}

impl Default for Format {
Expand Down

0 comments on commit 850aee4

Please sign in to comment.