fhir_rs/datatype/primitive/
mod.rs

1mod datetime;
2mod xhtml;
3
4pub use datetime::*;
5pub use xhtml::*;
6
7use crate::prelude::*;
8use std::fmt::{Display, Formatter};
9use std::str::FromStr;
10use fhir_derive::Element;
11
12#[derive(Element, Primitive, Debug, Clone)]
13pub struct StringDt {
14    /// xml:id (or equivalent in JSON)
15    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
16    pub id: Option<String>,
17    /// Additional content defined by implementations
18    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
19    pub extension: Option<Vec<Extension>>,
20    /// Primitive value for string
21    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
22    pub value: Option<String>,
23}
24
25#[derive(Element, Primitive, Debug, Clone)]
26pub struct IdDt {
27    /// xml:id (or equivalent in JSON)
28    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
29    pub id: Option<String>,
30    /// Additional content defined by implementations
31    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
32    pub extension: Option<Vec<Extension>>,
33    /// Primitive value for id
34    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
35    pub value: Option<Id>,
36}
37
38#[derive(Element, Primitive, Debug, Clone)]
39pub struct Base64BinaryDt {
40    /// xml:id (or equivalent in JSON)
41    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
42    pub id: Option<String>,
43    /// Additional content defined by implementations
44    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
45    pub extension: Option<Vec<Extension>>,
46    /// Primitive value for base64Binary
47    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
48    pub value: Option<Base64Binary>,
49}
50
51#[derive(Element, Primitive, Debug, Clone)]
52pub struct MarkdownDt {
53    /// xml:id (or equivalent in JSON)
54    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
55    pub id: Option<String>,
56    /// Additional content defined by implementations
57    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
58    pub extension: Option<Vec<Extension>>,
59    /// Primitive value for markdown
60    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
61    pub value: Option<Markdown>,
62}
63
64#[derive(Element, Primitive, Debug, Clone)]
65pub struct UriDt {
66    /// xml:id (or equivalent in JSON)
67    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
68    pub id: Option<String>,
69    /// Additional content defined by implementations
70    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
71    pub extension: Option<Vec<Extension>>,
72    /// Primitive value for uri
73    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
74    pub value: Option<Uri>,
75}
76
77#[derive(Element, Primitive, Debug, Clone)]
78pub struct OidDt {
79    /// xml:id (or equivalent in JSON)
80    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
81    pub id: Option<String>,
82    /// Additional content defined by implementations
83    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
84    pub extension: Option<Vec<Extension>>,
85    /// Primitive value for oid
86    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
87    pub value: Option<Oid>,
88}
89
90#[derive(Element, Primitive, Debug, Clone)]
91pub struct CanonicalDt {
92    /// xml:id (or equivalent in JSON)
93    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
94    pub id: Option<String>,
95    /// Additional content defined by implementations
96    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
97    pub extension: Option<Vec<Extension>>,
98    /// Primitive value for canonical
99    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
100    pub value: Option<Canonical>,
101}
102
103#[derive(Element, Primitive, Debug, Clone)]
104pub struct CodeDt {
105    /// xml:id (or equivalent in JSON)
106    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
107    pub id: Option<String>,
108    /// Additional content defined by implementations
109    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
110    pub extension: Option<Vec<Extension>>,
111    /// Primitive value for code
112    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
113    pub value: Option<Code>,
114}
115
116/// 布尔类型
117///
118/// true | false
119#[derive(Element, Primitive, Clone, Debug)]
120pub struct BooleanDt {
121    /// xml:id (or equivalent in JSON)
122    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
123    pub id: Option<String>,
124    /// Additional content defined by implementations
125    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
126    pub extension: Option<Vec<Extension>>,
127    /// Primitive value for boolean
128    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
129    pub value: Option<Boolean>,
130}
131
132/// 正整数类型
133///
134/// 1..2,147,483,647
135#[derive(Element, Primitive, Debug, Clone)]
136pub struct PositiveIntDt {
137    /// xml:id (or equivalent in JSON)
138    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
139    pub id: Option<String>,
140    /// Additional content defined by implementations
141    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
142    pub extension: Option<Vec<Extension>>,
143    /// Primitive value for positiveInt
144    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
145    pub value: Option<PositiveInt>,
146}
147
148
149#[derive(Element, Primitive, Clone, Debug)]
150pub struct IntegerDt {
151    /// xml:id (or equivalent in JSON)
152    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
153    pub id: Option<String>,
154    /// Additional content defined by implementations
155    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
156    pub extension: Option<Vec<Extension>>,
157    /// Primitive value for integer
158    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
159    pub value: Option<Integer>,
160}
161
162#[derive(Element, Primitive, Debug, Clone)]
163pub struct Integer64Dt {
164    /// xml:id (or equivalent in JSON)
165    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
166    pub id: Option<String>,
167    /// Additional content defined by implementations
168    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
169    pub extension: Option<Vec<Extension>>,
170    /// Primitive value for integer64
171    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
172    pub value: Option<Integer64>,
173}
174
175#[derive(Element, Primitive, Debug, Clone)]
176pub struct DecimalDt {
177    /// xml:id (or equivalent in JSON)
178    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
179    pub id: Option<String>,
180    /// Additional content defined by implementations
181    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
182    pub extension: Option<Vec<Extension>>,
183    /// Primitive value for decimal
184    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
185    pub value: Option<Decimal>,
186}
187
188#[derive(Element, Primitive, Debug, Clone)]
189pub struct UnsignedIntDt {
190    /// xml:id (or equivalent in JSON)
191    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
192    pub id: Option<String>,
193    /// Additional content defined by implementations
194    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
195    pub extension: Option<Vec<Extension>>,
196    /// Primitive value for unsignedInt
197    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
198    pub value: Option<UnsignedInt>,
199}
200
201#[derive(Element, Primitive, Debug, Clone)]
202pub struct UrlDt {
203    /// xml:id (or equivalent in JSON)
204    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
205    pub id: Option<String>,
206    /// Additional content defined by implementations
207    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
208    pub extension: Option<Vec<Extension>>,
209    /// Primitive value for url
210    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
211    pub value: Option<Url>,
212}
213
214#[derive(Element, Primitive, Debug, Clone)]
215pub struct UuidDt {
216    /// xml:id (or equivalent in JSON)
217    #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
218    pub id: Option<String>,
219    /// Additional content defined by implementations
220    #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
221    pub extension: Option<Vec<Extension>>,
222    /// Primitive value for uuid
223    #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
224    pub value: Option<Uuid>,
225}
226
227macro_rules! impl_from_str {
228    ($primitive:ident, $from:ident $($cast:tt)*) => {
229        impl FromStr for $primitive {
230            type Err = FhirError;
231
232            fn from_str(s: &str) -> std::prelude::v1::Result<Self, Self::Err> {
233                let val: $from = FromStr::from_str(s)?;
234                Ok(Self {
235                    id: None,
236                    extension: None,
237                    value: Some(val $($cast)*),
238                })
239            }
240        }
241    }
242}
243
244impl_from_str!(StringDt, String);
245impl_from_str!(UriDt, Uri);
246impl_from_str!(UrlDt, Url);
247impl_from_str!(CanonicalDt, Canonical);
248impl_from_str!(UuidDt, Uuid);
249impl_from_str!(OidDt, Oid);
250impl_from_str!(IdDt, Id);
251impl_from_str!(CodeDt, Code);
252impl_from_str!(MarkdownDt, Markdown);
253impl_from_str!(Base64BinaryDt, Base64Binary);
254impl_from_str!(XhtmlDt, Xhtml);
255
256impl_from_str!(PositiveIntDt, PositiveInt);
257impl_from_str!(IntegerDt, Integer);
258impl_from_str!(Integer64Dt, Integer64);
259impl_from_str!(UnsignedIntDt, UnsignedInt);
260impl_from_str!(DecimalDt, Decimal);
261
262impl_from_str!(BooleanDt, Boolean);
263
264impl_from_str!(InstantDt, Instant);
265impl_from_str!(DateTimeDt, DateTime);
266impl_from_str!(DateDt, Date);
267impl_from_str!(TimeDt, Time);
268
269macro_rules! primitive_from_impl {
270    ($primitive:ident) => {
271        impl From<&str> for $primitive {
272            fn from(value: &str) -> Self {
273                let value = FromStr::from_str(value).unwrap();
274                $primitive {
275                    id: None,
276                    extension: None,
277                    value: Some(value),
278                }
279            }
280        }
281    };
282
283    ($primitive:ident, $from:ident $($cast:tt)*) => {
284        impl From<$from> for $primitive {
285            fn from(value: $from) -> Self {
286                $primitive {
287                    id: None,
288                    extension: None,
289                    value: Some(value $($cast)*),
290                }
291            }
292        }
293    }
294}
295
296primitive_from_impl!(StringDt);
297primitive_from_impl!(UriDt);
298primitive_from_impl!(UrlDt);
299primitive_from_impl!(CanonicalDt);
300primitive_from_impl!(UuidDt);
301primitive_from_impl!(OidDt);
302primitive_from_impl!(IdDt);
303primitive_from_impl!(CodeDt);
304primitive_from_impl!(MarkdownDt);
305primitive_from_impl!(Base64BinaryDt);
306primitive_from_impl!(XhtmlDt);
307primitive_from_impl!(BooleanDt, bool);
308primitive_from_impl!(PositiveIntDt, u8 as usize);
309primitive_from_impl!(PositiveIntDt, u16 as usize);
310primitive_from_impl!(PositiveIntDt, u32 as usize);
311primitive_from_impl!(PositiveIntDt, i8 as usize);
312primitive_from_impl!(PositiveIntDt, i16 as usize);
313primitive_from_impl!(PositiveIntDt, i32 as usize);
314primitive_from_impl!(IntegerDt, u8 as isize);
315primitive_from_impl!(IntegerDt, u16 as isize);
316primitive_from_impl!(IntegerDt, u32 as isize);
317primitive_from_impl!(IntegerDt, i8 as isize);
318primitive_from_impl!(IntegerDt, i16 as isize);
319primitive_from_impl!(IntegerDt, i32 as isize);
320primitive_from_impl!(Integer64Dt, i8 as i64);
321primitive_from_impl!(Integer64Dt, i16 as i64);
322primitive_from_impl!(Integer64Dt, i32 as i64);
323primitive_from_impl!(Integer64Dt, i64);
324
325primitive_from_impl!(InstantDt, Instant);
326primitive_from_impl!(DateTimeDt, DateTime);
327primitive_from_impl!(DateDt, Date);
328primitive_from_impl!(TimeDt, Time);
329
330#[cfg(test)]
331mod tests {
332    use std::str::FromStr;
333
334    use crate::prelude::*;
335
336    #[test]
337    fn test_string() {
338        let name = StringDt::new("zhangsan");
339        assert_eq!(name.value.unwrap(), "zhangsan".to_string())
340    }
341
342    #[test]
343    fn test_int() {
344        let size = PositiveIntDt::new(356usize);
345        assert_eq!(size.value.unwrap(), 356)
346    }
347
348    #[test]
349    fn test_bool() {
350        let gender = BooleanDt::new(false);
351        assert_eq!(gender.value.unwrap(), false)
352    }
353    
354    #[test]
355    fn test_date() -> Result<()> {
356        let d1 = DateDt::from_str("2009")?;
357        tracing::debug!("Date: {}", d1.to_string());
358        let d1 = DateDt::from_str("2009-12")?;
359        tracing::debug!("Date: {}", d1);
360        let d1 = DateDt::from_str("2009-12-23")?;
361        tracing::debug!("Date: {}", d1);
362
363        let t1 = TimeDt::from_str("23:12:45")?;
364        tracing::debug!("Time: {:?}", t1);
365        let t1 = TimeDt::from_str("23:12:45.234")?;
366        tracing::debug!("Time: {}", t1);
367        let t1 = TimeDt::from_str("23:12:60.040")?;
368        tracing::debug!("Time: {}", t1);
369
370        tracing::debug!("=======================");
371
372        let dt1 = DateTimeDt::from_str("2009")?;
373        tracing::debug!("DateTime: {}", dt1.to_string());
374        let dt1 = DateTimeDt::from_str("2009-12")?;
375        tracing::debug!("DateTime: {:?}", dt1);
376        let dt1 = DateTimeDt::from_str("2009-12-23")?;
377        tracing::debug!("DateTime: {}", dt1);
378        let dt1 = DateTimeDt::from_str("2009-12-23T23:12:45Z")?;
379        tracing::debug!("DateTime: {:?}", dt1);
380        let dt1 = DateTimeDt::from_str("2009-12-23T23:12:45.234Z")?;
381        tracing::debug!("DateTime: {}", dt1);
382        let dt1 = DateTimeDt::from_str("2009-12-23T23:12:45-06:00")?;
383        tracing::debug!("DateTime: {}", dt1);
384
385        tracing::debug!("=======================");
386
387        let instant1 = InstantDt::from_str("2009-12-23T23:12:45Z")?;
388        tracing::debug!("Instant: {}", instant1);
389        let instant1 = InstantDt::from_str("2009-12-23T23:12:45.234Z")?;
390        tracing::debug!("Instant: {}", instant1);
391        let instant1 = InstantDt::from_str("2009-12-23T23:12:45-06:00")?;
392        tracing::debug!("Instant: {}", instant1);
393        let instant1 = InstantDt::from_str("2009-12-23T23:12:45.456-06:00")?;
394        tracing::debug!("Instant: {}", instant1);
395        Ok(())
396    }
397
398}