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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//! # FHIR Models.
//!
//! This is a sub-crate of [`fhir-sdk`](https://crates.io/crates/fhir-sdk). Please take a look at the main crate for
//! more documentation.

mod date_time;
mod error;
#[cfg(feature = "r4b")]
pub mod r4b;
#[cfg(feature = "r5")]
pub mod r5;
#[cfg(feature = "stu3")]
pub mod stu3;

use std::ops::{Deref, DerefMut};

use base64::prelude::{Engine, BASE64_STANDARD};
use serde::{Deserialize, Serialize};
pub use time;

pub use self::{date_time::*, error::*};

/// Parsed parts of a FHIR reference. Can be one of local reference, relative
/// reference or absolute reference. The absolute reference is unchecked and can
/// be anything, it is used as fallback.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParsedReference<'a> {
	/// Local reference, the resource is to be found in the `contained` field.
	Local {
		/// Local ID of the resource.
		id: &'a str,
	},
	/// Relative reference, the resource is on the same FHIR server.
	Relative {
		/// Resource type.
		resource_type: &'a str,
		/// Resource ID.
		id: &'a str,
		/// Targeted version ID if set.
		version_id: Option<&'a str>,
	},
	/// Absolute reference, the resource can be anywhere, might not even be FHIR
	/// server. Might not be a URL, but a URI like a `urn:uuid:...`.
	Absolute {
		/// Raw URL string.
		url: &'a str,
		/// Assumed resource type part if exists. Is just the positional URL
		/// segment, could be wrong.
		resource_type: Option<&'a str>,
		/// Assumed resource ID part if exists. Is just the positional URL
		/// segment, could be wrong.
		id: Option<&'a str>,
	},
}

impl<'a> ParsedReference<'a> {
	/// Parse the reference from a reference string.
	#[must_use]
	pub fn new(reference: &'a str) -> Self {
		if reference.starts_with('#') {
			return Self::Local { id: reference.split_at(1).1 };
		}

		let Some((resource_type, id, version_id, is_absolute)) = Self::parse_segments(reference)
		else {
			return Self::Absolute { url: reference, resource_type: None, id: None };
		};

		if is_absolute {
			Self::Absolute { url: reference, resource_type: Some(resource_type), id: Some(id) }
		} else {
			Self::Relative { resource_type, id, version_id }
		}
	}

	/// Helper function to split the reference segments if possible.
	/// Returns resource type, id, version id and if there is more segments if
	/// possible.
	fn parse_segments(reference: &'a str) -> Option<(&'a str, &'a str, Option<&'a str>, bool)> {
		let mut segments = reference.rsplit('/');
		let id_or_version = segments.next()?;
		let history_or_type = segments.next()?;
		Some(if history_or_type == "_history" {
			let id = segments.next()?;
			let resource_type = segments.next()?;
			(resource_type, id, Some(id_or_version), segments.next().is_some())
		} else {
			(history_or_type, id_or_version, None, segments.next().is_some())
		})
	}

	/// Get the resource type that this reference points to as string reference.
	/// Might not be present/visible in absolute URLs or local references. In
	/// absolute URLs, it might also return garbage, as the URL might have
	/// enough segments, but the segment was not an actual resource type. Take
	/// care when parsing to `ResourceType`.
	#[must_use]
	pub fn resource_type(&self) -> Option<&'a str> {
		match self {
			Self::Local { .. } => None,
			Self::Relative { resource_type, .. } => Some(resource_type),
			Self::Absolute { resource_type, .. } => *resource_type,
		}
	}

	/// Get the resource ID that this reference points to. Might not be
	/// present/visible in absolute URLs. In absolute URLs, it might also return
	/// garbage, as the URL might have enough segments, but the segment was not
	/// an actual id. Take care when using it.
	#[must_use]
	pub fn id(&self) -> Option<&'a str> {
		match self {
			Self::Local { id } => Some(id),
			Self::Relative { id, .. } => Some(id),
			Self::Absolute { id, .. } => *id,
		}
	}
}

/// FHIR `integer64` type. Wraps an i64, but serializes and deserializes as
/// string.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Integer64(pub i64);

impl Serialize for Integer64 {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: serde::Serializer,
	{
		self.0.to_string().serialize(serializer)
	}
}

impl<'de> Deserialize<'de> for Integer64 {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where
		D: serde::Deserializer<'de>,
	{
		let s = String::deserialize(deserializer)?;
		let i = s.parse().map_err(serde::de::Error::custom)?;
		Ok(Self(i))
	}
}

/// FHIR `base64Binary` type. Wraps binary data and serializes to base64
/// strings.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct Base64Binary(pub Vec<u8>);

impl Serialize for Base64Binary {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: serde::Serializer,
	{
		let s = BASE64_STANDARD.encode(&self.0);
		s.serialize(serializer)
	}
}

impl<'de> Deserialize<'de> for Base64Binary {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where
		D: serde::Deserializer<'de>,
	{
		let mut s = String::deserialize(deserializer)?;
		s.retain(|c| !c.is_whitespace());
		let bytes = BASE64_STANDARD.decode(s).map_err(serde::de::Error::custom)?;
		Ok(Self(bytes))
	}
}

/// Deref and From implementations for wrappers.
macro_rules! wrapper_impls {
	($wrapper:ident, $inner_type:ty) => {
		impl Deref for $wrapper {
			type Target = $inner_type;

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

		impl DerefMut for $wrapper {
			fn deref_mut(&mut self) -> &mut Self::Target {
				&mut self.0
			}
		}

		impl From<$inner_type> for $wrapper {
			fn from(inner: $inner_type) -> Self {
				Self(inner)
			}
		}

		impl From<$wrapper> for $inner_type {
			fn from(wrapper: $wrapper) -> $inner_type {
				wrapper.0
			}
		}
	};
}

wrapper_impls!(Integer64, i64);
wrapper_impls!(Base64Binary, Vec<u8>);
wrapper_impls!(Time, time::Time);
wrapper_impls!(Instant, time::OffsetDateTime);