fhirbolt/
lib.rs

1//! # Fhirbolt
2//! Fhirbolt is a library that enables you to work with FHIR resources in Rust.
3//! This library includes FHIR data types and methods for (de)serializing these from and to JSON and XML.
4//!
5//! More elaborate features like validation (including cardinality and slicing) or full FHIRPath evaluation
6//! might be added eventually.
7//!
8//! Currenlty supported FHIR releases: R4, R4B and R5
9//!
10//! The Rust crate supports two working modes:
11//! 1. a generic [element] model
12//! 2. working with fully typed model structs.
13//!
14//! ## Installation
15//! Add `fhirbolt` to your Cargo.toml.
16//! You can select which FHIR release to include model structs for by speficifying them as Cargo features.
17//!
18//! ```toml
19//! [dependencies]
20//! fhirbolt = { version = "0.2", features = ["r4b"] }
21//! ```
22//! By default, no FHIR release is included.
23//!
24//! ## Example
25//! ```
26//! // The `Resource` type is an enum that contains all possible FHIR resources.
27//! // If the resource type is known in advance, you could also use a concrete resource type
28//! // (like e.g. `fhirbolt::model::r4b::resources::Observation`).
29//! use fhirbolt::model::r4b::{
30//!     Resource,
31//!     resources::{Observation, ObservationValue},
32//!     types::{Code, CodeableConcept, Coding, String as FhirString},
33//! };
34//! use fhirbolt::serde::{DeserializationConfig, DeserializationMode};
35//!
36//! // The type of `s` is `&str`
37//! let s = r#"{
38//!     "resourceType": "Observation",
39//!     "status": "final",
40//!     "code": {
41//!         "text": "some code"
42//!     },
43//!     "valueString": "some value"
44//! }"#;
45//!
46//! let r: Resource = fhirbolt::json::from_str(s, None).unwrap();
47//!
48//! match r {
49//!     Resource::Observation(ref o) => println!("deserialized observation: {:?}", r),
50//!     _ => (),
51//! }
52//!
53//! // Use Default::default() or constructing new resources by yourself
54//! let o = Observation {
55//!     status: "final".into(),
56//!     code: Box::new(CodeableConcept {
57//!         text: Some("some code".into()),
58//!         ..Default::default()
59//!     }),
60//!     value: Some(ObservationValue::String("some value".into())),
61//!     ..Default::default()
62//! };
63//! ```
64//!
65//! You can pass a [`DeserializationConfig`](crate::serde::DeserializationConfig) to configure the deserialization behavior.
66
67pub use fhirbolt_element::{FhirRelease, FhirReleases};
68
69#[cfg(feature = "fhirbolt-model")]
70pub mod model {
71    //! Generated structs for FHIR resources.
72    //!
73    //! You can select which FHIR release to include by speficifying them as Cargo features.
74    //! ```toml
75    //! [dependencies]
76    //! fhirbolt = { version = "0.1", features = ["r4", "r4b"] }
77    //! ```
78    //! By default, no FHIR release is included.
79
80    pub use fhirbolt_model::*;
81}
82
83pub mod element {
84    //! Generic element model.
85    //!
86    //! As deserialization differs slightly between FHIR releases,
87    //! `Element` is generic over a FHIR release.
88    //!
89    //! # Example
90    //! ```
91    //! use fhirbolt::FhirReleases;
92    //! use fhirbolt::element::{Element, Value, Primitive};
93    //!
94    //! let mut element = Element::<{ FhirReleases:: R4B }>::new();
95    //! element.insert(
96    //!     "resourceType".to_string(),
97    //!     Value::Primitive(
98    //!         Primitive::String("Observation".to_string())
99    //!     )
100    //! );
101    //!
102    //! // This can be simplified by using the Element! macro provided
103    //! let element: Element::<{ FhirReleases:: R4B }> = Element! {
104    //!     "resourceType" => Value::Primitive(Primitive::String("Observation".to_string()))
105    //! };
106    //! ```
107
108    pub use fhirbolt_element::*;
109
110    #[doc(no_inline)]
111    pub use crate::serde::element::{from_element, to_element, Error, Result};
112}
113
114pub mod serde {
115    //! (De)serialize FHIR resources from and to JSON and XML.
116    //!
117    //! # Example
118    //! ```
119    //! // The `Resource` type is an enum that contains all possible FHIR resources.
120    //! // If the resource type is known in advance, you could also use a concrete resource type
121    //! // (like e.g. `fhirbolt::model::r4b::resources::Observation`).
122    //! use fhirbolt::model::r4b::Resource;
123    //!
124    //! // The type of `s` is `&str`
125    //! let s = r#"{
126    //!     "resourceType": "Observation",
127    //!     "status": "final",
128    //!     "code": {
129    //!         "text": "some code"
130    //!     },
131    //!     "valueString": "some value"
132    //! }"#;
133    //!
134    //! let r: Resource = fhirbolt::json::from_str(s, None).unwrap();
135    //! println!("{:?}", r);
136    //! ```
137
138    pub use fhirbolt_serde::*;
139}
140
141#[doc(no_inline)]
142pub use serde::{json, xml};