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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Import management utilities
//!
//! This module handles the determination of import paths for FHIR types
//! and collection of custom types for import generation.
use crate::generators::naming_manager::NamingManager;
use crate::generators::type_registry::TypeRegistry;
use crate::rust_types::{RustStruct, RustType};
use std::collections::HashSet;
/// Utility struct for managing imports and type classifications
pub struct ImportManager;
impl ImportManager {
/// Collect all custom types referenced by a struct and add them to the imports set
pub fn collect_custom_types_from_struct(
rust_struct: &RustStruct,
imports: &mut HashSet<String>,
structs_in_file: &HashSet<String>,
) {
// Add import for base type if present
if let Some(base_def) = &rust_struct.base_definition {
let base_type = base_def.split('/').next_back().unwrap_or(base_def);
// Only add import if it's not a primitive type, not the current struct, and not in the same file
if !Self::is_primitive_type(base_type)
&& base_type != rust_struct.name
&& !structs_in_file.contains(base_type)
{
// For base definitions, ensure proper struct name casing but only for
// names that are clearly in all lowercase (like "vitalsigns")
let proper_base_type = if base_type
.chars()
.all(|c| c.is_lowercase() || c.is_numeric())
{
// Convert all-lowercase names to PascalCase (e.g., "vitalsigns" -> "Vitalsigns")
crate::naming::Naming::capitalize_first(base_type)
} else {
// Keep names that already have proper casing (e.g., "BackboneElement")
base_type.to_string()
};
let import_path = Self::get_import_path_for_type(&proper_base_type);
imports.insert(import_path);
}
}
// Collect custom types from all fields
for field in &rust_struct.fields {
Self::collect_custom_types_from_type(
&field.field_type,
imports,
&rust_struct.name,
structs_in_file,
);
}
}
/// Collect all custom types referenced by a trait and add them to the imports set
pub fn collect_custom_types_from_trait(
rust_trait: &crate::rust_types::RustTrait,
imports: &mut HashSet<String>,
) {
// Add imports for super traits
for super_trait in &rust_trait.super_traits {
if !Self::is_primitive_type(super_trait) {
// For super traits that are FHIR trait types (Accessors, Mutators, Existence)
// or FHIR resources, import from the traits module
let import_path = if Self::is_fhir_trait_type(super_trait) {
// For trait types like "DomainResourceAccessors", extract the base name
// and use it for the module path: crate::traits::domain_resource::DomainResourceAccessors
let base_name = if super_trait.ends_with("Accessors") {
super_trait.strip_suffix("Accessors").unwrap()
} else if super_trait.ends_with("Mutators") {
super_trait.strip_suffix("Mutators").unwrap()
} else if super_trait.ends_with("Existence") {
super_trait.strip_suffix("Existence").unwrap()
} else {
super_trait
};
format!(
"crate::traits::{}::{}",
crate::naming::Naming::to_snake_case(base_name),
super_trait
)
} else if Self::is_fhir_resource_type(super_trait) {
format!(
"crate::traits::{}::{}",
crate::naming::Naming::to_snake_case(super_trait),
super_trait
)
} else {
Self::get_import_path_for_type(super_trait)
};
imports.insert(import_path);
}
}
// Collect custom types from all trait methods
for method in &rust_trait.methods {
// Collect types from return type
if let Some(return_type) = &method.return_type {
Self::collect_custom_types_from_type(
return_type,
imports,
&rust_trait.name,
&HashSet::new(), // Traits don't have "structs in file"
);
}
// Collect types from parameters (if any)
for param in &method.params {
Self::collect_custom_types_from_type(
¶m.param_type,
imports,
&rust_trait.name,
&HashSet::new(),
);
}
}
}
/// Recursively collect custom types from a RustType
pub fn collect_custom_types_from_type(
rust_type: &RustType,
imports: &mut HashSet<String>,
current_struct_name: &str,
structs_in_file: &HashSet<String>,
) {
match rust_type {
RustType::Custom(type_name) => {
// Handle FHIR primitive types (like StringType, BooleanType) - these need imports
if Self::is_fhir_primitive_type(type_name) {
let import_path = Self::get_fhir_primitive_import_path(type_name);
imports.insert(import_path);
}
// Only add import for other types if it's not a Rust primitive type, not the current struct, and not in the same file
else if !Self::is_primitive_type(type_name)
&& type_name != current_struct_name
&& !structs_in_file.contains(type_name)
{
// Check if the type actually exists in the TypeRegistry before generating an import
if TypeRegistry::get_classification(type_name).is_some() {
// Get the correct import path for this type
let import_path = Self::get_import_path_for_type(type_name);
imports.insert(import_path);
}
// If the type doesn't exist in the registry, don't generate an import
// This prevents importing non-existent nested structures
}
}
RustType::Option(inner) => {
Self::collect_custom_types_from_type(
inner,
imports,
current_struct_name,
structs_in_file,
);
}
RustType::Vec(inner) => {
Self::collect_custom_types_from_type(
inner,
imports,
current_struct_name,
structs_in_file,
);
}
RustType::Box(inner) => {
Self::collect_custom_types_from_type(
inner,
imports,
current_struct_name,
structs_in_file,
);
}
RustType::Slice(inner) => {
Self::collect_custom_types_from_type(
inner,
imports,
current_struct_name,
structs_in_file,
);
}
RustType::Reference(name) => {
// Handle FHIR primitive types (like StringType, BooleanType) - these need imports
if Self::is_fhir_primitive_type(name) {
let import_path = Self::get_fhir_primitive_import_path(name);
imports.insert(import_path);
}
// Only add import for other types if it's not a Rust primitive type, not the current struct, and not in the same file
else if !Self::is_primitive_type(name)
&& name != current_struct_name
&& !structs_in_file.contains(name)
{
// Check if the type actually exists in the TypeRegistry before generating an import
if TypeRegistry::get_classification(name).is_some() {
let import_path = Self::get_import_path_for_type(name);
imports.insert(import_path);
}
// If the type doesn't exist in the registry, don't generate an import
// This prevents importing non-existent nested structures
}
}
// Primitive types don't need imports
RustType::String | RustType::Integer | RustType::Boolean | RustType::Float => {}
}
}
/// Determine the correct import path for a given type name
pub fn get_import_path_for_type(type_name: &str) -> String {
// First try the TypeRegistry for accurate classification based on StructureDefinition
TypeRegistry::get_import_path_for_type(type_name)
}
/// Check if a type is a FHIR resource type
pub fn is_fhir_resource_type(type_name: &str) -> bool {
NamingManager::is_fhir_resource(type_name)
}
/// Check if a type is a FHIR trait type (ends with Accessors, Mutators, or Existence)
pub fn is_fhir_trait_type(type_name: &str) -> bool {
type_name.ends_with("Accessors")
|| type_name.ends_with("Mutators")
|| type_name.ends_with("Existence")
}
/// Check if a type name represents a known FHIR data type
pub fn is_fhir_datatype(type_name: &str) -> bool {
NamingManager::is_fhir_datatype(type_name)
}
/// Check if a type is a FHIR primitive type
pub fn is_fhir_primitive_type(type_name: &str) -> bool {
// FHIR primitive types that have extensions - matching actual generated type names
matches!(
type_name,
"StringType"
| "BooleanType"
| "IntegerType"
| "DecimalType"
| "UriType"
| "UrlType"
| "CanonicalType"
| "OidType"
| "UuidType"
| "InstantType"
| "DateType"
| "DateTimeType"
| "TimeType"
| "CodeType"
| "IdType"
| "MarkdownType"
| "Base64BinaryType"
| "UnsignedIntType"
| "PositiveIntType"
| "XhtmlType"
// Also handle the non-Type variants that appear in trait methods
| "String"
| "Boolean"
| "Integer"
| "Decimal"
| "Uri"
| "Url"
| "Canonical"
| "Oid"
| "Uuid"
| "Instant"
| "Date"
| "DateTime"
| "Time"
| "Code"
| "Id"
| "Markdown"
| "Base64Binary"
| "UnsignedInt"
| "PositiveInt"
| "Xhtml"
)
}
/// Get the import path for a FHIR primitive type
pub fn get_fhir_primitive_import_path(type_name: &str) -> String {
let module_name = match type_name {
"StringType" => "string",
"BooleanType" => "boolean",
"IntegerType" => "integer",
"DecimalType" => "decimal",
"UriType" => "uri",
"UrlType" => "url",
"CanonicalType" => "canonical",
"OidType" => "oid",
"UuidType" => "uuid",
"InstantType" => "instant",
"DateType" => "date",
"DateTimeType" => "date_time",
"TimeType" => "time",
"CodeType" => "code",
"IdType" => "id",
"MarkdownType" => "markdown",
"Base64BinaryType" => "base64binary",
"UnsignedIntType" => "unsigned_int",
"PositiveIntType" => "positive_int",
"XhtmlType" => "xhtml",
_ => "unknown",
};
format!("crate::primitives::{module_name}::{type_name}")
}
/// Check if a type is a generated trait
pub fn is_generated_trait(type_name: &str) -> bool {
// Traits are typically generated for base types or common interfaces
let lower_name = type_name.to_lowercase();
lower_name.ends_with("trait")
|| lower_name.ends_with("accessors")
|| lower_name.ends_with("mutators")
|| lower_name.ends_with("helpers")
|| matches!(
lower_name.as_str(),
"resourcetrait"
| "domainresourcetrait"
| "backboneelementtrait"
| "elementtrait"
| "metadataresourcetrait"
| "resourceaccessors"
| "domainresourceaccessors"
| "backboneelementaccessors"
| "elementaccessors"
| "metadataresourceaccessors"
)
}
/// Check if a type name represents a primitive Rust type
pub fn is_primitive_type(type_name: &str) -> bool {
matches!(
type_name,
"String"
| "&str"
| "str"
| "i32"
| "u32"
| "i64"
| "u64"
| "f32"
| "f64"
| "bool"
| "usize"
| "isize"
| "Self" // Built-in Rust keyword, should not be imported
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_import_classification() {
// Test resource classification
assert!(ImportManager::is_fhir_resource_type("DomainResource"));
assert!(ImportManager::is_fhir_resource_type("Patient"));
assert!(ImportManager::is_fhir_resource_type("ActivityDefinition"));
assert!(!ImportManager::is_fhir_resource_type("Identifier"));
// Test datatype classification
assert!(ImportManager::is_fhir_datatype("Identifier"));
assert!(ImportManager::is_fhir_datatype("CodeableConcept"));
assert!(ImportManager::is_fhir_datatype("Reference"));
assert!(!ImportManager::is_fhir_datatype("DomainResource"));
// Test import path generation
assert_eq!(
ImportManager::get_import_path_for_type("DomainResource"),
"crate::resources::domain_resource::DomainResource"
);
assert_eq!(
ImportManager::get_import_path_for_type("Identifier"),
"crate::datatypes::identifier::Identifier"
);
assert_eq!(
ImportManager::get_import_path_for_type("PublicationStatus"),
"crate::bindings::publication_status::PublicationStatus"
);
}
#[test]
fn test_primitive_type_detection() {
assert!(ImportManager::is_primitive_type("String"));
assert!(ImportManager::is_primitive_type("i32"));
assert!(ImportManager::is_primitive_type("bool"));
assert!(!ImportManager::is_primitive_type("Patient"));
assert!(!ImportManager::is_primitive_type("Identifier"));
}
#[test]
fn test_nested_structure_detection() {
// Test nested structure import path detection with longest prefix matching
assert_eq!(
ImportManager::get_import_path_for_type("EvidenceVariableCharacteristic"),
"crate::resources::evidence_variable::EvidenceVariableCharacteristic"
);
assert_eq!(
ImportManager::get_import_path_for_type("MeasureReportGroup"),
"crate::resources::measure_report::MeasureReportGroup"
);
assert_eq!(
ImportManager::get_import_path_for_type("AccountCoverage"),
"crate::resources::account::AccountCoverage"
);
assert_eq!(
ImportManager::get_import_path_for_type("AccountGuarantor"),
"crate::resources::account::AccountGuarantor"
);
assert_eq!(
ImportManager::get_import_path_for_type("BundleEntry"),
"crate::resources::bundle::BundleEntry"
);
assert_eq!(
ImportManager::get_import_path_for_type("ImplementationGuideGlobal"),
"crate::resources::implementation_guide::ImplementationGuideGlobal"
);
// Test ConditionStage specifically - this is the failing case
assert_eq!(
ImportManager::get_import_path_for_type("ConditionStage"),
"crate::resources::condition::ConditionStage"
);
// Test that non-nested structures still work correctly
assert_eq!(
ImportManager::get_import_path_for_type("Patient"),
"crate::resources::patient::Patient"
);
assert_eq!(
ImportManager::get_import_path_for_type("Identifier"),
"crate::datatypes::identifier::Identifier"
);
}
}