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