Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Commit

Permalink
feat: add support for yaml spdx files
Browse files Browse the repository at this point in the history
Signed-off-by: Mikko Murto <mikko.murto@hhpartners.fi>
  • Loading branch information
mmurto committed Apr 14, 2022
1 parent 625d934 commit 3d9fe82
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 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.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ hex = "0.4"
regex = "1"
anyhow = "1"
serde_json = "1"
serde_yaml = "0.8"

[dev-dependencies]
pretty_assertions = "1"
13 changes: 10 additions & 3 deletions src/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ pub fn hash256_for_path<P: AsRef<Path>>(path: P) -> anyhow::Result<String> {
Ok(hex::encode_upper(hash))
}

/// Deserialize [`SPDX`] from a file path.
/// Deserialize [`SPDX`] from a file path. Accepts JSON and YAML.
pub fn deserialize_spdx<P: AsRef<Path>>(path_to_spdx: P) -> anyhow::Result<SPDX> {
let file_contents = read_to_string(path_to_spdx)?;
Ok(serde_json::from_str::<SPDX>(&file_contents)?)
let file_contents = read_to_string(&path_to_spdx)?;
match path_to_spdx.as_ref().extension() {
Some(extension) => match extension.to_str() {
Some("json") => Ok(serde_json::from_str::<SPDX>(&file_contents)?),
Some("yml") | Some("yaml") => Ok(serde_yaml::from_str::<SPDX>(&file_contents)?),
_ => Err(anyhow::anyhow!("invalid file extension")),
},
None => Err(anyhow::anyhow!("invalid file extension")),
}
}

/// Serialize [`SPDX`] to a file path.
Expand Down

0 comments on commit 3d9fe82

Please sign in to comment.