Skip to main content

haste_health/commands/
fhirpath.rs

1use haste_fhir_model::r4::generated::{resources::Resource, terminology::IssueType};
2use haste_fhir_operation_error::OperationOutcomeError;
3
4use crate::utilities;
5
6fn parse_fhir_data() -> Result<Resource, OperationOutcomeError> {
7    let mut buffer = String::new();
8    utilities::read_stdin(&mut buffer)?;
9
10    let resource = serde_json::from_str::<Resource>(&buffer).map_err(|e| {
11        OperationOutcomeError::error(
12            IssueType::exception(),
13            format!(
14                "Failed to parse FHIR data must be a FHIR R4 Resource: {}",
15                e
16            ),
17        )
18    })?;
19
20    Ok(resource)
21}
22
23/// Runs the `fhir-path` command: evaluates `fhirpath` against a FHIR resource read from stdin.
24pub(crate) async fn run(fhirpath: &str) -> Result<(), OperationOutcomeError> {
25    let data = parse_fhir_data()?;
26    let engine = haste_fhirpath::FPEngine::new();
27
28    let result = engine.evaluate(fhirpath, vec![&data]).await.map_err(|e| {
29        OperationOutcomeError::error(
30            IssueType::exception(),
31            format!("Failed to evaluate FHIRPath: {}", e),
32        )
33    })?;
34
35    println!("{:#?}", result.iter().collect::<Vec<_>>());
36
37    Ok(())
38}