knowledge-base-crud 0.1.0

Filesystem CRUD operations for a file-based knowledge base
Documentation
use knowledge_base_crud::KnowledgeBase;
use knowledge_base_models::{EntityId, EntityTypeId, PropertyId, ReferenceId};
use std::fs;
use std::path::{Path, PathBuf};

fn fixture() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("../..").join("fixtures/valid/minimal")
}

#[test]
fn reads_every_supported_resource_without_normalizing_it() {
    let root = fixture();
    let knowledge_base = KnowledgeBase::new(&root);

    let entity_id = "Q1".parse::<EntityId>().expect("valid entity identifier");
    let type_id = "T1".parse::<EntityTypeId>().expect("valid entity type identifier");
    let property_id = "P1".parse::<PropertyId>().expect("valid property identifier");
    let reference_id = "R1".parse::<ReferenceId>().expect("valid reference identifier");

    assert_eq!(
        knowledge_base.entities().read(&entity_id).expect("read entity"),
        fs::read_to_string(root.join("entities/Q1.yaml")).unwrap()
    );
    assert_eq!(
        knowledge_base.entity_types().read(&type_id).expect("read type"),
        fs::read_to_string(root.join("entity_types/T1.yaml")).unwrap()
    );
    assert_eq!(
        knowledge_base.properties().read(&property_id).expect("read property"),
        fs::read_to_string(root.join("properties/P1.yaml")).unwrap()
    );
    assert_eq!(
        knowledge_base.references().read(&reference_id).expect("read reference"),
        fs::read_to_string(root.join("references/R1.yaml")).unwrap()
    );
    assert_eq!(
        knowledge_base.entity_contexts().read(&entity_id).expect("read context"),
        fs::read_to_string(root.join("entity_context/Q1.md")).unwrap()
    );
}

#[test]
fn missing_resource_error_contains_the_resolved_path() {
    let root = fixture();
    let knowledge_base = KnowledgeBase::new(&root);
    let id = "Q999".parse::<EntityId>().expect("valid entity identifier");

    let error = knowledge_base.entities().read(&id).expect_err("resource should be missing");

    assert!(matches!(
        &error,
        knowledge_base_crud::Error::Read { path, .. }
            if path == &root.join("entities/Q999.yaml")
    ));
    assert!(error.to_string().contains("Q999.yaml"));
}