fhir_sdk/
version.rs

1//! Marker types for FHIR versions. Mainly for use in the client (but also
2//! extension traits).
3#![cfg_attr(
4	not(feature = "builders"),
5	allow(
6		dead_code,
7		unused_imports,
8		unused_macros,
9		reason = "Many impls missing without builders"
10	)
11)]
12
13use std::{
14	fmt::{Debug, Display},
15	str::FromStr,
16};
17
18use fhir_model::for_all_versions;
19use serde::{Serialize, de::DeserializeOwned};
20
21use crate::{
22	extensions::{BundleEntryExt, BundleExt, GenericResource, ParametersExt, ReferenceExt},
23	utils::Sealed,
24};
25
26/// FHIR version STU3, "3.0".
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
28pub struct FhirStu3;
29/// FHIR version R4B, "4.3".
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
31pub struct FhirR4B;
32/// FHIR version R5, "5.0".
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
34pub struct FhirR5;
35
36#[cfg(feature = "r5")]
37/// Default FHIR version (e.g. in client).
38pub type DefaultVersion = FhirR5;
39#[cfg(all(not(feature = "r5"), feature = "r4b"))]
40/// Default FHIR version (e.g. in client).
41pub type DefaultVersion = FhirR4B;
42#[cfg(all(not(feature = "r5"), not(feature = "r4b"), feature = "stu3"))]
43/// Default FHIR version (e.g. in client).
44pub type DefaultVersion = FhirStu3;
45
46/// Internal macro to convert the module version identifier to the FHIR version
47/// type.
48macro_rules! fhir_version {
49	(stu3) => {
50		$crate::version::FhirStu3
51	};
52	(r4b) => {
53		$crate::version::FhirR4B
54	};
55	(r5) => {
56		$crate::version::FhirR5
57	};
58}
59pub(crate) use fhir_version;
60
61/// FHIR version type "marker", but with additional information. Only implemented if "builders"
62/// feature is activated.
63pub trait FhirVersion: Sealed + Unpin + Send + Sync + 'static {
64	/// FHIR version string.
65	const VERSION: &'static str;
66	/// JSON mime type used by this version.
67	const MIME_TYPE: &'static str;
68
69	/// `ResourceType` of this version.
70	type ResourceType: Serialize
71		+ DeserializeOwned
72		+ Debug
73		+ FromStr
74		+ AsRef<str>
75		+ Display
76		+ Clone
77		+ Copy
78		+ PartialEq
79		+ Eq
80		+ Unpin
81		+ Send
82		+ Sync;
83	/// Generic `Resource` enum of this version.
84	type Resource: GenericResource
85		+ Serialize
86		+ DeserializeOwned
87		+ Debug
88		+ Clone
89		+ PartialEq
90		+ Unpin
91		+ Send
92		+ Sync;
93
94	/// `Bundle` resource.
95	type Bundle: BundleExt<Entry: BundleEntryExt<Resource = Self::Resource>>
96		+ Serialize
97		+ DeserializeOwned
98		+ Debug
99		+ Clone
100		+ PartialEq
101		+ Unpin
102		+ Send
103		+ Sync;
104	/// `CapabilityStatement` resource.
105	type CapabilityStatement: Serialize
106		+ DeserializeOwned
107		+ Debug
108		+ Clone
109		+ PartialEq
110		+ Unpin
111		+ Send
112		+ Sync;
113	/// `OperationOutcome` resource.
114	type OperationOutcome: Serialize
115		+ DeserializeOwned
116		+ Debug
117		+ Clone
118		+ PartialEq
119		+ Unpin
120		+ Send
121		+ Sync;
122	/// `Parameters` resource.
123	type Parameters: ParametersExt
124		+ Serialize
125		+ DeserializeOwned
126		+ Debug
127		+ Clone
128		+ PartialEq
129		+ Unpin
130		+ Send
131		+ Sync;
132
133	/// `Reference` type.
134	type Reference: ReferenceExt
135		+ Serialize
136		+ DeserializeOwned
137		+ Debug
138		+ Clone
139		+ PartialEq
140		+ Unpin
141		+ Send
142		+ Sync;
143
144	/// `SearchComparator` type.
145	type SearchComparator: Serialize
146		+ DeserializeOwned
147		+ Debug
148		+ FromStr
149		+ AsRef<str>
150		+ Display
151		+ Clone
152		+ Copy
153		+ PartialEq
154		+ Eq
155		+ Unpin
156		+ Send
157		+ Sync;
158}
159
160impl Sealed for FhirStu3 {}
161impl Sealed for FhirR4B {}
162impl Sealed for FhirR5 {}
163
164/// Implement `FhirVersion` for all supported versions.
165macro_rules! impl_fhir_version {
166	($version:ident) => {
167		use fhir_model::$version;
168
169		impl FhirVersion for fhir_version!($version) {
170			const VERSION: &'static str = $version::VERSION;
171			const MIME_TYPE: &'static str = $version::JSON_MIME_TYPE;
172
173			type ResourceType = $version::resources::ResourceType;
174			type Resource = $version::resources::Resource;
175
176			type Bundle = $version::resources::Bundle;
177			type CapabilityStatement = $version::resources::CapabilityStatement;
178			type OperationOutcome = $version::resources::OperationOutcome;
179			type Parameters = $version::resources::Parameters;
180
181			type Reference = $version::types::Reference;
182
183			type SearchComparator = $version::codes::SearchComparator;
184		}
185	};
186}
187#[cfg(feature = "builders")] // Depends on trait implementations that use the builders.
188for_all_versions!(impl_fhir_version);