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
//! 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};

/// Generic element in a FHIR resource.
///
/// 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())
///     )
/// );
/// // ...
/// ```
#[derive(Clone, PartialEq)]
pub struct Element<const R: FhirRelease> {
    map: indexmap::IndexMap<String, Value<R>>,
}

impl<const R: FhirRelease> Element<R> {
    /// Create a new element.
    pub fn new() -> Self {
        Self {
            map: indexmap::IndexMap::new(),
        }
    }

    /// Create a new element wit preallocated capacity.
    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>>;

    fn deref(&self) -> &Self::Target {
        &self.map
    }
}

impl<const R: FhirRelease> DerefMut for Element<R> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.map
    }
}

impl<const R: FhirRelease> FromIterator<(String, Value<R>)> for Element<R> {
    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>>;
    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>>;
    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>>;
    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(i64),
    Decimal(String),
    String(String),
}