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
//! Resource
//!
//! <https://build.fhir.org/json.html>
//!
//! JSON Representation of Resources
//!
//! ```text
//! {
//! "resourceType" : "[Resource Type]",
//! "resourceDefinition" : "(see below)",
//! // from Source: property0
//! "property1" : "<[primitive]>", // short description
//! "property2" : { [Datatype] }, // short description
//! "property3" : { // Short Description
//! "propertyA" : { CodeableConcept }, // Short Description (Example)
//! },
//! "property4" : [{ // Short Description
//! "propertyB" : { Reference(ResourceType) } // R! Short Description
//! }]
//! }
//! ```
//!
use ::serde::{Deserialize, Serialize};
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Resource {
/// # resourceType
///
/// ## Description
///
/// The `resourceType` attribute specifies the type of FHIR resource being
/// represented. It is a mandatory element that identifies which resource
/// schema and constraints apply to the JSON document in FHIR R5.
///
/// ## Purpose
///
/// The `resourceType` serves several critical functions:
///
/// - Identifies the specific FHIR resource type for parsers and processors
/// - Determines which validation rules and constraints apply
/// - Enables proper routing and processing in FHIR systems
/// - Provides context for interpreting the resource's data elements
/// - Supports polymorphism in FHIR resource handling
///
/// ## Usage
///
/// The `resourceType` must be included in every FHIR resource as the first
/// element. It should be used:
///
/// - At the root level of every FHIR resource JSON document
/// - When validating resources against their appropriate
/// StructureDefinitions
/// - In API endpoints to determine resource-specific processing logic
/// - For content negotiation and resource type filtering
///
/// ## Data Type
///
/// **code** - A string that must exactly match one of the defined FHIR
/// resource types. The value is:
///
/// - Case-sensitive
/// - Must be an exact match to a valid FHIR R5 resource type name
/// - Follows PascalCase naming convention (e.g., "Patient", "Observation",
/// "DiagnosticReport")
///
/// ## Constraints
///
/// - **Required**: Yes - Must be present in every FHIR resource
/// - **Cardinality**: 1..1 (exactly one occurrence)
/// - **Fixed Position**: Must be the first element in the JSON object
/// - **Valid Values**: Must be one of the 150+ defined FHIR R5 resource
/// types
/// - **Case Sensitivity**: Exact case match required
///
/// ## Examples
///
/// See the accompanying `example.json` file for a complete Observation
/// resource demonstrating the use of the `resourceType` attribute.
///
/// ## Related Keys
///
/// - `meta.profile` - Specifies which profile(s) the resource conforms to
/// - `id` - Unique identifier for the resource instance
/// - `meta` - Metadata about the resource
/// - All resource-specific elements depend on the `resourceType` for their
/// validity
///
/// ## Specification Reference
///
/// Based on FHIR R5 specification. For complete details and the full list
/// of valid resource types, refer to the official FHIR R5 documentation for
/// resource definitions.
///
pub resource_type: String,
pub resource_definition: String,
pub properties: Vec<crate::r5::todo::property::Property>,
}
#[cfg(test)]
mod tests {
use super::*;
type T = Resource;
#[test]
fn test_default() {
let actual = T::default();
let expect = T {
resource_type: String::default(),
resource_definition: String::default(),
properties: vec![],
};
assert_eq!(actual, expect);
}
mod serde_json {
use super::*;
use ::serde_json::json;
#[test]
fn test_serde_json_from_value() {
let json = json!(
{
"resourceType": "",
"resourceDefinition": "",
"properties": []
}
);
let actual: Resource = ::serde_json::from_value(json).expect("from_value");
let expect: T = T::default();
assert_eq!(actual, expect);
}
#[test]
fn test_serde_json_to_value() {
let actual: ::serde_json::Value =
::serde_json::to_value(T::default()).expect("to_value");
let expect: ::serde_json::Value = json!(
{
"resourceType": "",
"resourceDefinition": "",
"properties": []
}
);
assert_eq!(actual, expect);
}
}
}