Skip to main content

haste_reflect/
primitives.rs

1use crate::traits::MetaValue;
2use std::any::Any;
3
4/// 067  public static final String `FP_String` = <http://hl7.org/fhirpath/System.String>;
5/// 068  public static final String `FP_Boolean` = <http://hl7.org/fhirpath/System.Boolean>;
6/// 069  public static final String `FP_Integer` = <http://hl7.org/fhirpath/System.Integer>;
7/// 070  public static final String `FP_Decimal` = <http://hl7.org/fhirpath/System.Decimal>;
8/// 071  public static final String `FP_Quantity` = <http://hl7.org/fhirpath/System.Quantity>;
9/// 072  public static final String `FP_DateTime` = <http://hl7.org/fhirpath/System.DateTime>;
10/// 073  public static final String `FP_Time` = <http://hl7.org/fhirpath/System.Time>;
11impl MetaValue for i64 {
12    fn fields(&self) -> Vec<&'static str> {
13        vec![]
14    }
15
16    fn get_field<'a>(&'a self, _field: &str) -> Option<&'a dyn MetaValue> {
17        None
18    }
19
20    fn get_index(&self, _index: usize) -> Option<&dyn MetaValue> {
21        None
22    }
23
24    fn get_field_mut<'a>(&'a mut self, _field: &str) -> Option<&'a mut dyn MetaValue> {
25        None
26    }
27
28    fn get_index_mut(&mut self, _index: usize) -> Option<&mut dyn MetaValue> {
29        None
30    }
31
32    fn fhir_type(&self) -> &'static str {
33        "http://hl7.org/fhirpath/System.Integer"
34    }
35
36    fn as_any(&self) -> &dyn Any {
37        self
38    }
39
40    fn flatten(&self) -> Vec<&dyn MetaValue> {
41        vec![self]
42    }
43    fn is_many(&self) -> bool {
44        false
45    }
46}
47
48impl MetaValue for u64 {
49    fn fields(&self) -> Vec<&'static str> {
50        vec![]
51    }
52
53    fn get_field<'a>(&'a self, _field: &str) -> Option<&'a dyn MetaValue> {
54        None
55    }
56
57    fn get_index(&self, _index: usize) -> Option<&dyn MetaValue> {
58        None
59    }
60
61    fn get_field_mut<'a>(&'a mut self, _field: &str) -> Option<&'a mut dyn MetaValue> {
62        None
63    }
64
65    fn get_index_mut(&mut self, _index: usize) -> Option<&mut dyn MetaValue> {
66        None
67    }
68
69    fn fhir_type(&self) -> &'static str {
70        "http://hl7.org/fhirpath/System.Integer"
71    }
72
73    fn as_any(&self) -> &dyn Any {
74        self
75    }
76
77    fn flatten(&self) -> Vec<&dyn MetaValue> {
78        vec![self]
79    }
80    fn is_many(&self) -> bool {
81        false
82    }
83}
84
85impl MetaValue for f64 {
86    fn fields(&self) -> Vec<&'static str> {
87        vec![]
88    }
89
90    fn get_field<'a>(&'a self, _field: &str) -> Option<&'a dyn MetaValue> {
91        None
92    }
93
94    fn get_index(&self, _index: usize) -> Option<&dyn MetaValue> {
95        None
96    }
97
98    fn get_field_mut<'a>(&'a mut self, _field: &str) -> Option<&'a mut dyn MetaValue> {
99        None
100    }
101
102    fn get_index_mut(&mut self, _index: usize) -> Option<&mut dyn MetaValue> {
103        None
104    }
105
106    fn fhir_type(&self) -> &'static str {
107        "http://hl7.org/fhirpath/System.Decimal"
108    }
109
110    fn as_any(&self) -> &dyn Any {
111        self
112    }
113
114    fn flatten(&self) -> Vec<&dyn MetaValue> {
115        vec![self]
116    }
117    fn is_many(&self) -> bool {
118        false
119    }
120}
121
122impl MetaValue for bool {
123    fn fields(&self) -> Vec<&'static str> {
124        vec![]
125    }
126
127    fn get_field<'a>(&'a self, _field: &str) -> Option<&'a dyn MetaValue> {
128        None
129    }
130
131    fn get_index(&self, _index: usize) -> Option<&dyn MetaValue> {
132        None
133    }
134
135    fn get_field_mut<'a>(&'a mut self, _field: &str) -> Option<&'a mut dyn MetaValue> {
136        None
137    }
138
139    fn get_index_mut(&mut self, _index: usize) -> Option<&mut dyn MetaValue> {
140        None
141    }
142
143    fn fhir_type(&self) -> &'static str {
144        "http://hl7.org/fhirpath/System.Boolean"
145    }
146
147    fn as_any(&self) -> &dyn Any {
148        self
149    }
150
151    fn flatten(&self) -> Vec<&dyn MetaValue> {
152        vec![self]
153    }
154    fn is_many(&self) -> bool {
155        false
156    }
157}
158
159impl MetaValue for String {
160    fn fields(&self) -> Vec<&'static str> {
161        vec![]
162    }
163
164    fn get_field<'a>(&'a self, _field: &str) -> Option<&'a dyn MetaValue> {
165        None
166    }
167
168    fn get_index(&self, _index: usize) -> Option<&dyn MetaValue> {
169        None
170    }
171
172    fn get_field_mut<'a>(&'a mut self, _field: &str) -> Option<&'a mut dyn MetaValue> {
173        None
174    }
175
176    fn get_index_mut(&mut self, _index: usize) -> Option<&mut dyn MetaValue> {
177        None
178    }
179
180    fn fhir_type(&self) -> &'static str {
181        "http://hl7.org/fhirpath/System.String"
182    }
183
184    fn as_any(&self) -> &dyn Any {
185        self
186    }
187
188    fn flatten(&self) -> Vec<&dyn MetaValue> {
189        vec![self]
190    }
191    fn is_many(&self) -> bool {
192        false
193    }
194}
195
196impl MetaValue for &'static str {
197    fn fields(&self) -> Vec<&'static str> {
198        vec![]
199    }
200
201    fn get_field<'a>(&'a self, _field: &str) -> Option<&'a dyn MetaValue> {
202        None
203    }
204
205    fn get_index(&self, _index: usize) -> Option<&dyn MetaValue> {
206        None
207    }
208
209    fn get_field_mut<'a>(&'a mut self, _field: &str) -> Option<&'a mut dyn MetaValue> {
210        None
211    }
212
213    fn get_index_mut(&mut self, _index: usize) -> Option<&mut dyn MetaValue> {
214        None
215    }
216
217    fn fhir_type(&self) -> &'static str {
218        "http://hl7.org/fhirpath/System.String"
219    }
220
221    fn as_any(&self) -> &dyn Any {
222        self
223    }
224
225    fn flatten(&self) -> Vec<&dyn MetaValue> {
226        vec![self]
227    }
228
229    fn is_many(&self) -> bool {
230        false
231    }
232}
233
234impl<T> MetaValue for Vec<T>
235where
236    T: MetaValue,
237{
238    fn fields(&self) -> Vec<&'static str> {
239        if let Some(first) = self.first() {
240            first.fields()
241        } else {
242            vec![]
243        }
244    }
245
246    fn get_field<'a>(&'a self, _field: &str) -> Option<&'a dyn MetaValue> {
247        None
248    }
249
250    fn get_index(&self, index: usize) -> Option<&dyn MetaValue> {
251        if self.get(index).is_some() {
252            let k: &dyn MetaValue = &self[index];
253            Some(k)
254        } else {
255            None
256        }
257    }
258
259    fn get_field_mut<'a>(&'a mut self, _field: &str) -> Option<&'a mut dyn MetaValue> {
260        None
261    }
262
263    fn get_index_mut(&mut self, index: usize) -> Option<&mut dyn MetaValue> {
264        if self.get(index).is_some() {
265            let k: &mut dyn MetaValue = &mut self[index];
266            Some(k)
267        } else {
268            None
269        }
270    }
271
272    fn fhir_type(&self) -> &'static str {
273        if let Some(first) = self.first() {
274            first.fhir_type()
275        } else {
276            ""
277        }
278    }
279
280    fn as_any(&self) -> &dyn Any {
281        self
282    }
283
284    fn flatten(&self) -> Vec<&dyn MetaValue> {
285        self.iter().flat_map(|item| item.flatten()).collect()
286    }
287
288    fn is_many(&self) -> bool {
289        true
290    }
291}
292
293// Used for mutable access which requires setting optional fields.
294impl<T> MetaValue for Option<T>
295where
296    T: MetaValue,
297{
298    fn fields(&self) -> Vec<&'static str> {
299        match self {
300            Some(value) => value.fields(),
301            None => vec![],
302        }
303    }
304
305    fn get_field<'a>(&'a self, field: &str) -> Option<&'a dyn MetaValue> {
306        self.as_ref().and_then(|value| value.get_field(field))
307    }
308
309    fn fhir_type(&self) -> &'static str {
310        match self {
311            Some(value) => value.fhir_type(),
312            None => "",
313        }
314    }
315
316    fn as_any(&self) -> &dyn Any {
317        match self {
318            Some(value) => value.as_any(),
319            None => self,
320        }
321    }
322
323    fn flatten(&self) -> Vec<&dyn MetaValue> {
324        self.as_ref().map_or_else(Vec::new, |v| v.flatten())
325    }
326
327    fn get_index(&self, index: usize) -> Option<&dyn MetaValue> {
328        self.as_ref().and_then(|v| v.get_index(index))
329    }
330
331    fn get_field_mut<'a>(&'a mut self, field: &str) -> Option<&'a mut dyn MetaValue> {
332        self.as_mut().and_then(|value| value.get_field_mut(field))
333    }
334
335    fn get_index_mut(&mut self, index: usize) -> Option<&mut dyn MetaValue> {
336        self.as_mut().and_then(|v| v.get_index_mut(index))
337    }
338
339    fn is_many(&self) -> bool {
340        self.as_ref().is_some_and(super::traits::MetaValue::is_many)
341    }
342}
343
344impl<T> MetaValue for Box<T>
345where
346    T: MetaValue,
347{
348    fn fields(&self) -> Vec<&'static str> {
349        self.as_ref().fields()
350    }
351
352    fn get_field<'a>(&'a self, field: &str) -> Option<&'a dyn MetaValue> {
353        self.as_ref().get_field(field)
354    }
355
356    fn fhir_type(&self) -> &'static str {
357        self.as_ref().fhir_type()
358    }
359
360    fn as_any(&self) -> &dyn Any {
361        self
362    }
363
364    fn flatten(&self) -> Vec<&dyn MetaValue> {
365        self.as_ref().flatten()
366    }
367
368    fn get_index(&self, index: usize) -> Option<&dyn MetaValue> {
369        self.as_ref().get_index(index)
370    }
371
372    fn get_field_mut<'a>(&'a mut self, field: &str) -> Option<&'a mut dyn MetaValue> {
373        self.as_mut().get_field_mut(field)
374    }
375
376    fn get_index_mut(&mut self, index: usize) -> Option<&mut dyn MetaValue> {
377        self.as_mut().get_index_mut(index)
378    }
379    fn is_many(&self) -> bool {
380        self.as_ref().is_many()
381    }
382}