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 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
16 pub id: Option<String>,
17 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
19 pub extension: Option<Vec<Extension>>,
20 #[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 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
29 pub id: Option<String>,
30 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
32 pub extension: Option<Vec<Extension>>,
33 #[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 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
42 pub id: Option<String>,
43 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
45 pub extension: Option<Vec<Extension>>,
46 #[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 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
55 pub id: Option<String>,
56 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
58 pub extension: Option<Vec<Extension>>,
59 #[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 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
68 pub id: Option<String>,
69 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
71 pub extension: Option<Vec<Extension>>,
72 #[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 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
81 pub id: Option<String>,
82 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
84 pub extension: Option<Vec<Extension>>,
85 #[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 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
94 pub id: Option<String>,
95 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
97 pub extension: Option<Vec<Extension>>,
98 #[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 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
107 pub id: Option<String>,
108 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
110 pub extension: Option<Vec<Extension>>,
111 #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
113 pub value: Option<Code>,
114}
115
116#[derive(Element, Primitive, Clone, Debug)]
120pub struct BooleanDt {
121 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
123 pub id: Option<String>,
124 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
126 pub extension: Option<Vec<Extension>>,
127 #[fhir(name="value", min="0", max="1", summary=false, modifier=false, choice="")]
129 pub value: Option<Boolean>,
130}
131
132#[derive(Element, Primitive, Debug, Clone)]
136pub struct PositiveIntDt {
137 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
139 pub id: Option<String>,
140 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
142 pub extension: Option<Vec<Extension>>,
143 #[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 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
153 pub id: Option<String>,
154 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
156 pub extension: Option<Vec<Extension>>,
157 #[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 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
166 pub id: Option<String>,
167 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
169 pub extension: Option<Vec<Extension>>,
170 #[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 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
179 pub id: Option<String>,
180 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
182 pub extension: Option<Vec<Extension>>,
183 #[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 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
192 pub id: Option<String>,
193 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
195 pub extension: Option<Vec<Extension>>,
196 #[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 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
205 pub id: Option<String>,
206 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
208 pub extension: Option<Vec<Extension>>,
209 #[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 #[fhir(name="id", min="0", max="1", summary=false, modifier=false, choice="")]
218 pub id: Option<String>,
219 #[fhir(name="extension", min="0", max="*", summary=false, modifier=false, choice="")]
221 pub extension: Option<Vec<Extension>>,
222 #[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}