fhirbolt_element/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
//! Generic element model.
//!
//! As deserialization differs slightly between FHIR releases,
//! `Element` is generic over a FHIR release.
//!
//! # Example
//! ```
//! use fhirbolt::FhirReleases;
//! use fhirbolt::element::{Element, Value, Primitive};
//!
//! let mut element = Element::<{ FhirReleases:: R4B }>::new();
//! element.insert(
//! "resourceType".to_string(),
//! Value::Primitive(
//! Primitive::String("Observation".to_string())
//! )
//! );
//! // ...
//! ```
use std::{
fmt,
ops::{Deref, DerefMut},
};
pub use fhirbolt_shared::{FhirRelease, FhirReleases};
/// Macro for creating [`Element`].
///
/// # Examples
///
/// ```rust
/// use fhirbolt::FhirReleases;
/// use fhirbolt::element::{Element, Value, Primitive};
///
/// let element: Element<{ FhirReleases::R4 }> = Element! {
/// "value" => Value::Primitive(Primitive::String("123".into())),
/// };
/// ```
#[macro_export]
macro_rules! Element {
{$($k: expr => $v: expr),* $(,)?} => {
fhirbolt_element::Element::from([$(($k, $v),)*])
};
}
/// Generic element in a FHIR resource.
///
/// As deserialization differs slightly between FHIR releases,
/// `Element` is generic over a FHIR release.
///
/// It is recommended to use the `Element!` macro for creating
/// new element.
///
/// # Example
/// ## With macro
/// ```rust
/// use fhirbolt::FhirReleases;
/// use fhirbolt::element::{Element, Value, Primitive};
///
/// let element: Element<{ FhirReleases::R4 }> = Element! {
/// "value" => Value::Primitive(Primitive::String("123".into())),
/// };
/// ```
///
/// ## Without macro
/// ```rust
/// use fhirbolt::FhirReleases;
/// use fhirbolt::element::{Element, Value, Primitive};
///
/// let mut element = Element::<{ FhirReleases:: R4B }>::new();
/// element.insert(
/// "value".to_string(),
/// Value::Primitive(
/// Primitive::String("123".to_string())
/// )
/// );
/// // ...
/// ```
#[derive(Default, Clone, PartialEq)]
pub struct Element<const R: FhirRelease> {
map: indexmap::IndexMap<String, Value<R>>,
}
impl<const R: FhirRelease> Element<R> {
/// Create a new element.
#[inline]
pub fn new() -> Self {
Self {
map: indexmap::IndexMap::new(),
}
}
/// Create a new element wit preallocated capacity.
#[inline]
pub fn with_capacity(n: usize) -> Self {
Self {
map: indexmap::IndexMap::with_capacity(n),
}
}
}
impl<const R: FhirRelease> Deref for Element<R> {
type Target = indexmap::IndexMap<String, Value<R>>;
#[inline]
fn deref(&self) -> &Self::Target {
&self.map
}
}
impl<const R: FhirRelease> DerefMut for Element<R> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.map
}
}
impl<const R: FhirRelease, const N: usize> From<[(String, Value<R>); N]> for Element<R> {
fn from(arr: [(String, Value<R>); N]) -> Self {
Element {
map: indexmap::IndexMap::from(arr),
}
}
}
impl<const R: FhirRelease, const N: usize> From<[(&str, Value<R>); N]> for Element<R> {
fn from(arr: [(&str, Value<R>); N]) -> Self {
Element::from_iter(arr.map(|(k, v)| (k.into(), v)))
}
}
impl<const R: FhirRelease> FromIterator<(String, Value<R>)> for Element<R> {
#[inline]
fn from_iter<I: IntoIterator<Item = (String, Value<R>)>>(iter: I) -> Self {
Element {
map: indexmap::IndexMap::from_iter(iter),
}
}
}
impl<'a, const R: FhirRelease> IntoIterator for &'a Element<R> {
type Item = (&'a String, &'a Value<R>);
type IntoIter = indexmap::map::Iter<'a, String, Value<R>>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, const R: FhirRelease> IntoIterator for &'a mut Element<R> {
type Item = (&'a String, &'a mut Value<R>);
type IntoIter = indexmap::map::IterMut<'a, String, Value<R>>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
impl<const R: FhirRelease> IntoIterator for Element<R> {
type Item = (String, Value<R>);
type IntoIter = indexmap::map::IntoIter<String, Value<R>>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
indexmap::IndexMap::into_iter(self.map)
}
}
impl<const R: FhirRelease> fmt::Debug for Element<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
let mut s = f.debug_struct(&format!("Element<{}>", R));
for (key, value) in self {
s.field(key, value);
}
s.finish()
}
}
/// Generic value in a FHIR resource.
#[derive(Clone, PartialEq)]
pub enum Value<const R: FhirRelease> {
Element(Element<R>),
Sequence(Vec<Element<R>>),
Primitive(Primitive),
}
impl<const R: FhirRelease> fmt::Debug for Value<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match self {
Value::Element(e) => e.fmt(f),
Value::Sequence(s) => s.fmt(f),
Value::Primitive(p) => write!(f, "{:?}", p),
}
}
}
/// Primitive value in a FHIR resource.
#[derive(Clone, Debug, PartialEq)]
pub enum Primitive {
Bool(bool),
Integer(i32),
Integer64(i64),
Decimal(String),
String(String),
}