csaf-rs 0.5.1

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

fn create_missing_external_reference_error(doc_category: &CsafDocumentCategory) -> ValidationError {
    ValidationError {
        message: format!(
            "Document with category '{doc_category}' must have at least one reference with category 'external'"
        ),
        instance_path: "/document/references".to_string(),
    }
}

const PROFILE_TEST_CONFIG: DocumentCategoryTestConfig = DocumentCategoryTestConfig::new().shared(&[
    CsafDocumentCategory::CsafInformationalAdvisory,
    CsafDocumentCategory::CsafSecurityIncidentResponse,
]);

/// 6.1.27.2 Document References
///
/// This test only applies to documents with `/document/category` with value `csaf_informational_advisory`
/// or `csaf_security_incident_response`.
///
/// Documents with these categories must have at least one entry in `/document/references` with an external reference.
pub fn test_6_1_27_02_document_references(doc: &impl CsafTrait) -> Result<(), Vec<ValidationError>> {
    let doc_category = doc.get_document().get_category();

    if !PROFILE_TEST_CONFIG.matches_category_with_csaf_version(doc.get_document().get_csaf_version(), &doc_category) {
        return Ok(()); // ToDo generate skipped https://github.com/csaf-rs/csaf/issues/409
    }

    // check if there is a document reference with category 'external'
    let mut found_external_reference = false;
    if let Some(references) = doc.get_document().get_references() {
        for reference in references {
            if CategoryOfReference::External == reference.get_category() {
                found_external_reference = true;
                break;
            };
        }
    }

    // if there isn't a reference with category 'external', return an error
    if !found_external_reference {
        return Err(vec![create_missing_external_reference_error(&doc_category)]);
    }

    Ok(())
}

crate::test_validation::impl_validator!(ValidatorForTest6_1_27_2, test_6_1_27_02_document_references);

#[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_1_27_02() {
        let case_informational_advisory = Err(vec![create_missing_external_reference_error(
            &CsafDocumentCategory::CsafInformationalAdvisory,
        )]);

        TESTS_2_0.test_6_1_27_2.expect(case_informational_advisory.clone());
        TESTS_2_1.test_6_1_27_2.expect(case_informational_advisory);
    }
}