Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose more Details of Purl and Cpe for writing #381

Merged
merged 5 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions cyclonedx-bom/src/external_models/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ impl Validate for Purl {
}
}

impl FromStr for Purl {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.to_string()))
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Uri(pub(crate) String);

Expand Down Expand Up @@ -110,10 +117,12 @@ pub enum UriError {

#[cfg(test)]
mod test {
use super::*;
use crate::validation::FailureReason;
use pretty_assertions::assert_eq;

use crate::validation::FailureReason;

use super::*;

#[test]
fn valid_purls_should_pass_validation() {
let validation_result = Purl("pkg:cargo/cyclonedx-bom@0.3.1".to_string())
Expand Down
12 changes: 12 additions & 0 deletions cyclonedx-bom/src/models/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use once_cell::sync::Lazy;
use regex::Regex;
use std::str::FromStr;

use crate::models::attached_text::AttachedText;
use crate::models::code::{Commits, Patches};
Expand Down Expand Up @@ -436,6 +437,17 @@ impl Validate for Swid {

#[derive(Debug, PartialEq, Eq)]
pub struct Cpe(pub(crate) String);

impl FromStr for Cpe {
type Err = ValidationError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let result = Cpe(s.to_string());
result.validate()?;
Ok(result)
}
}

impl Validate for Cpe {
fn validate_with_context(
&self,
Expand Down