run_view_definition

Function run_view_definition 

Source
pub fn run_view_definition(
    view_definition: SofViewDefinition,
    bundle: SofBundle,
    content_type: ContentType,
) -> Result<Vec<u8>, SofError>
Expand description

Execute a SQL-on-FHIR ViewDefinition transformation on a FHIR Bundle.

This is the main entry point for SQL-on-FHIR transformations. It processes a ViewDefinition against a Bundle of FHIR resources and produces output in the specified format. The function handles version compatibility, validation, FHIRPath evaluation, and output formatting.

§Arguments

  • view_definition - The ViewDefinition containing transformation logic
  • bundle - The Bundle containing FHIR resources to process
  • content_type - The desired output format

§Returns

  • Ok(Vec<u8>) - Formatted output bytes ready for writing to file or stdout
  • Err(SofError) - Detailed error information about what went wrong

§Validation

The function performs comprehensive validation:

  • FHIR version compatibility between ViewDefinition and Bundle
  • ViewDefinition structure and logic validation
  • FHIRPath expression syntax and evaluation
  • Output format compatibility

§Examples

use helios_sof::{SofViewDefinition, SofBundle, ContentType, run_view_definition};

// Create a simple ViewDefinition
let view_json = serde_json::json!({
    "resourceType": "ViewDefinition",
    "status": "active",
    "resource": "Patient",
    "select": [{
        "column": [{
            "name": "id",
            "path": "id"
        }]
    }]
});
let view_def: helios_fhir::r4::ViewDefinition = serde_json::from_value(view_json)?;

// Create a simple Bundle
let bundle_json = serde_json::json!({
    "resourceType": "Bundle",
    "type": "collection",
    "entry": []
});
let bundle: helios_fhir::r4::Bundle = serde_json::from_value(bundle_json)?;

let sof_view = SofViewDefinition::R4(view_def);
let sof_bundle = SofBundle::R4(bundle);

// Generate CSV with headers
let csv_output = run_view_definition(
    sof_view,
    sof_bundle,
    ContentType::CsvWithHeader
)?;

// Write to file or stdout
std::fs::write("output.csv", csv_output)?;

§Error Handling

Common error scenarios:

use helios_sof::{SofError, SofViewDefinition, SofBundle, ContentType, run_view_definition};

match run_view_definition(view, bundle, content_type) {
    Ok(output) => {
        println!("Success: {} bytes generated", output.len());
    },
    Err(SofError::InvalidViewDefinition(msg)) => {
        eprintln!("ViewDefinition error: {}", msg);
    },
    Err(SofError::FhirPathError(msg)) => {
        eprintln!("FHIRPath error: {}", msg);
    },
    Err(e) => {
        eprintln!("Other error: {}", e);
    }
}