csaf-rs 0.5.0

A parser for the CSAF standard written in Rust
use crate::csaf::types::csaf_document_category::CsafDocumentCategory;
use crate::csaf_traits::{CsafTrait, DocumentTrait, ProductTrait, ProductTreeTrait};
use crate::validation::ValidationError;
use crate::validations::utils::document_category_test_config::DocumentCategoryTestConfig;

fn create_unused_product_id_error(product_id: &str, path: &str) -> ValidationError {
    ValidationError {
        message: format!("Product ID '{product_id}' is defined but not referenced in the document"),
        instance_path: format!("{path}/product_id"),
    }
}

const SKIP_TEST_CONFIG: DocumentCategoryTestConfig =
    DocumentCategoryTestConfig::new().shared(&[CsafDocumentCategory::CsafInformationalAdvisory]);

/// 6.2.1 Unused Definition of Product ID
///
/// All defined product IDs need to be referenced at least once in the document.
pub fn test_6_2_01_unused_definition_of_product_id(doc: &impl CsafTrait) -> Result<(), Vec<ValidationError>> {
    let mut errors: Option<Vec<ValidationError>> = None;

    // Skips the test for profile "Informational Advisory"
    if SKIP_TEST_CONFIG.matches_category(&doc.get_document().get_category()) {
        return Ok(());
    }

    // Get all references to product IDs in the document
    let references: std::collections::HashSet<String> = doc.get_all_product_references_ids().into_iter().collect();

    // Visit all product id definitions and check if they are referenced
    if let Some(tree) = doc.get_product_tree() {
        tree.visit_all_products(&mut |fpn, path| {
            if !references.contains(fpn.get_product_id()) {
                errors
                    .get_or_insert_default()
                    .push(create_unused_product_id_error(fpn.get_product_id(), path));
            }
        });
    }

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

crate::test_validation::impl_validator!(ValidatorForTest6_2_1, test_6_2_01_unused_definition_of_product_id);

#[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_01() {
        let case_01 = Err(vec![create_unused_product_id_error(
            "CSAFPID-9080700",
            "/product_tree/full_product_names/0",
        )]);

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