csaf-rs 0.5.1

A parser for the CSAF standard written in Rust
use crate::csaf_traits::{BranchTrait, CategoryOfTheBranch, CsafTrait, ProductTreeTrait};
use crate::validation::ValidationError;
use regex::Regex;
use std::sync::LazyLock;

fn create_product_version_range_without_vers_error(version_range: &str, path: &str) -> ValidationError {
    ValidationError {
        message: format!("Product version range {version_range} does not match vers syntax"),
        instance_path: format!("{path}/name"),
    }
}

static VERS_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^vers:[a-z.\-+][a-z0-9.\-+]*/.+").unwrap());

/// 6.2.18 Product Version Range without vers
///
/// Tests that in the product tree, all branches with the category `product_version_range` use vers
/// in their `name` property.
pub fn test_6_2_18_product_version_range_without_vers(doc: &impl CsafTrait) -> Result<(), Vec<ValidationError>> {
    let mut errors: Option<Vec<ValidationError>> = None;

    if let Some(product_tree) = doc.get_product_tree() {
        product_tree.visit_all_branches(&mut |branch, path| {
            if branch.get_category() == CategoryOfTheBranch::ProductVersionRange
                && !VERS_REGEX.is_match(branch.get_name())
            {
                errors
                    .get_or_insert_default()
                    .push(create_product_version_range_without_vers_error(branch.get_name(), path));
            }
        });
    }

    errors.map_or(Ok(()), Err)
}

crate::test_validation::impl_validator!(ValidatorForTest6_2_18, test_6_2_18_product_version_range_without_vers);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::csaf2_0::testcases::TESTS_2_0;
    use crate::csaf2_1::testcases::TESTS_2_1;

    #[test]
    fn test_test_6_2_18() {
        let case_01 = Err(vec![create_product_version_range_without_vers_error(
            ">4.2",
            "/product_tree/branches/0/branches/0/branches/0",
        )]);

        // Both CSAF 2.0 and 2.1 have 2 test cases
        TESTS_2_0.test_6_2_18.expect(case_01.clone(), Ok(()));
        TESTS_2_1.test_6_2_18.expect(case_01, Ok(()));
    }
}