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(
8            IssueType::Exception(None),
9            "Failed to read from stdin.".into(),
10        )
11    })?;
12    let resource = haste_fhir_serialization_json::from_str::<Resource>(&buffer).map_err(|e| {
13        OperationOutcomeError::error(
14            IssueType::Exception(None),
15            format!(
16                "Failed to parse FHIR data must be a FHIR R4 Resource: {}",
17                e
18            ),
19        )
20    })?;
21
22    Ok(resource)
23}
24
25pub fn fhirpath(fhirpath: &str) -> Result<(), OperationOutcomeError> {
26    let data = parse_fhir_data()?;
27    let engine = haste_fhirpath::FPEngine::new();
28
29    let result = engine.evaluate(fhirpath, vec![&data]).map_err(|e| {
30        OperationOutcomeError::error(
31            IssueType::Exception(None),
32            format!("Failed to evaluate FHIRPath: {}", e),
33        )
34    })?;
35
36    println!("{:#?}", result.iter().collect::<Vec<_>>());
37
38    Ok(())
39}