rh-codegen 0.1.0-beta.1

Code generation library for creating Rust types from FHIR StructureDefinitions
Documentation
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//! FHIR Crate Generation
//!
//! This module provides functionality to generate complete Rust crates from FHIR packages,
//! including Cargo.toml, lib.rs, README.md, and proper module structure.

use std::fs;
use std::path::Path;

use anyhow::Result;
use chrono::Local;

// use crate::quality::{run_quality_checks, QualityConfig};

/// Parameters for crate generation
#[derive(Debug, Clone)]
pub struct CrateGenerationParams<'a> {
    /// Output directory for the crate
    pub output: &'a Path,
    /// Package name (e.g., "hl7.fhir.r4.core")
    pub package: &'a str,
    /// Package version (e.g., "4.0.1")
    pub version: &'a str,
    /// Canonical URL from package.json
    pub canonical_url: &'a str,
    /// Author from package.json
    pub author: &'a str,
    /// Description from package.json
    pub description: &'a str,
    /// Command that was invoked to generate this crate
    pub command_invoked: &'a str,
    /// Optional override for the generated crate name (e.g., "rh-hl7-fhir-r4-core").
    /// When None, the name is auto-derived from the FHIR package name.
    pub crate_name: Option<&'a str>,
}

/// Statistics about generated crate content
#[derive(Debug, Clone)]
pub struct CrateStatistics {
    /// Number of generated structs
    pub num_structs: usize,
    /// Number of generated enums
    pub num_enums: usize,
    /// Total number of types
    pub total_types: usize,
    /// Canonical URL
    pub canonical_url: String,
}

/// Generate a complete Rust crate structure with idiomatic directory organization
pub fn generate_crate_structure(params: CrateGenerationParams) -> Result<()> {
    // Create the src directory structure
    let src_dir = params.output.join("src");
    fs::create_dir_all(&src_dir)?;

    // Create subdirectories
    let resource_dir = src_dir.join("resources");
    let datatypes_dir = src_dir.join("datatypes");
    let extensions_dir = src_dir.join("extensions");
    let primitives_dir = src_dir.join("primitives");
    let traits_dir = src_dir.join("traits");
    let bindings_dir = src_dir.join("bindings");
    let profiles_dir = src_dir.join("profiles");

    fs::create_dir_all(&resource_dir)?;
    fs::create_dir_all(&datatypes_dir)?;
    fs::create_dir_all(&extensions_dir)?;
    fs::create_dir_all(&primitives_dir)?;
    fs::create_dir_all(&traits_dir)?;
    fs::create_dir_all(&bindings_dir)?;
    fs::create_dir_all(&profiles_dir)?;

    // Generate statistics by counting organized files (if any exist from organized generation)
    let stats = generate_crate_statistics_from_organized_dirs(
        &resource_dir,
        &datatypes_dir,
        &extensions_dir,
        &primitives_dir,
    )?;

    // Generate Cargo.toml
    let cargo_toml_content = generate_cargo_toml(
        params.package,
        params.version,
        params.output,
        params.crate_name,
    );
    let cargo_toml_path = params.output.join("Cargo.toml");
    fs::write(&cargo_toml_path, cargo_toml_content)?;

    // Generate lib.rs with new structure
    let lib_rs_content = generate_lib_rs_idiomatic()?;
    let lib_rs_path = src_dir.join("lib.rs");
    fs::write(&lib_rs_path, lib_rs_content)?;

    // Generate macros.rs with FHIR primitive macros
    let macros_content = include_str!("../macros.rs");
    let macros_path = src_dir.join("macros.rs");
    fs::write(&macros_path, macros_content)?;

    // Generate validation.rs module with ValidatableResource trait
    let validation_content =
        crate::generators::ValidationTraitGenerator::generate_validation_module();
    let validation_path = src_dir.join("validation.rs");
    fs::write(&validation_path, validation_content)?;

    // Generate prelude.rs module with commonly used trait re-exports
    let prelude_content = generate_prelude_module();
    let prelude_path = src_dir.join("prelude.rs");
    fs::write(&prelude_path, prelude_content)?;

    // Generate mod.rs files for each module
    generate_module_files(
        &resource_dir,
        &datatypes_dir,
        &extensions_dir,
        &primitives_dir,
        &traits_dir,
        &bindings_dir,
        &profiles_dir,
    )?;

    // Generate README.md
    let readme_content = generate_readme_md(
        params.package,
        params.version,
        params.canonical_url,
        params.author,
        params.description,
        params.command_invoked,
        &stats,
        params.crate_name,
    );
    let readme_path = params.output.join("README.md");
    fs::write(&readme_path, readme_content)?;

    // Run quality checks as a final step
    //let quality_config = QualityConfig::default();
    //run_quality_checks(params.output, &quality_config)
    //   .map_err(|e| anyhow::anyhow!("Quality checks failed: {e}"))?;

    Ok(())
}

/// Generate Cargo.toml content for the FHIR crate
fn generate_cargo_toml(
    package: &str,
    version: &str,
    _output_dir: &Path,
    crate_name_override: Option<&str>,
) -> String {
    // Derive the package name: use override if provided, else convert FHIR package name
    let derived = package.replace(['.', '-'], "_");
    let crate_name = crate_name_override.unwrap_or(&derived);
    // Lib name must be a valid Rust identifier (no hyphens)
    let lib_name = crate_name.replace('-', "_");

    // Try to find rh-foundation crate path relative to output directory
    let rh_foundation_path = if let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") {
        // We're in the rh-codegen crate, so rh-foundation is at ../rh-foundation
        let workspace_root = Path::new(&manifest_dir).parent().and_then(|p| p.parent());
        if let Some(root) = workspace_root {
            let foundation_path = root.join("crates/rh-foundation");
            if foundation_path.exists() {
                // Use absolute path for reliability
                foundation_path.display().to_string()
            } else {
                // Fallback to relative path assumption
                "../../rh/crates/rh-foundation".to_string()
            }
        } else {
            "../../rh/crates/rh-foundation".to_string()
        }
    } else {
        "../../rh/crates/rh-foundation".to_string()
    };

    format!(
        r#"[package]
name = "{crate_name}"
version = "0.1.0"
edition = "2021"
description = "Generated FHIR types from {package} package version {version}"
authors = ["FHIR Code Generator"]
license = "MIT OR Apache-2.0"

[dependencies]
serde = {{ version = "1.0", features = ["derive"] }}
serde_json = "1.0"
phf = {{ version = "0.11", features = ["macros"] }}
once_cell = "1.19"
rh-foundation = {{ path = "{rh_foundation_path}" }}

[lib]
name = "{lib_name}"
path = "src/lib.rs"
"#
    )
}

/// Generate lib.rs content with idiomatic module structure
fn generate_lib_rs_idiomatic() -> Result<String> {
    let lib_content = r#"//! Generated FHIR Rust bindings
//!
//! This crate contains Rust types and traits for FHIR resources and data types.
//! It includes macros for primitive field generation and maintains FHIR compliance.

// Allow clippy lint for derivable Default implementations
//
// TODO: Future optimization - derive Default when possible instead of manual impl
//
// Currently, we generate explicit Default implementations for all structs.
// Many of these could use #[derive(Default)] instead, which would be more idiomatic.
//
// Pros of deriving Default:
// - More idiomatic Rust code
// - Less generated code (no manual impl blocks)
// - Clearer intent (all fields use Default::default())
//
// Cons of current approach (manual impl):
// - Clippy warns about 1,100+ derivable implementations
// - More verbose generated code
//
// Pros of current approach:
// - Explicit and predictable behavior
// - Handles mixed initialization patterns consistently
// - Simpler code generation logic
//
// To implement derive-based approach would require:
// 1. Analyze all field types to ensure they implement Default
// 2. Detect required fields with non-Default initializations (String::new(), Vec::new(), etc.)
// 3. Add "Default" to struct derives only when ALL fields can use Default::default()
// 4. Skip manual impl generation for those structs
//
#![allow(clippy::derivable_impls)]

pub mod macros;
pub mod metadata;
pub mod primitives;
pub mod datatypes;
pub mod extensions;
pub mod resources;
pub mod profiles;
pub mod traits;
pub mod bindings;
pub mod validation;
pub mod prelude;

pub use serde::{Deserialize, Serialize};
"#;

    Ok(lib_content.to_string())
}
/// Generate mod.rs files for each module directory
pub fn generate_module_files(
    resource_dir: &Path,
    datatypes_dir: &Path,
    extensions_dir: &Path,
    primitives_dir: &Path,
    traits_dir: &Path,
    bindings_dir: &Path,
    profiles_dir: &Path,
) -> Result<()> {
    // Generate resource/mod.rs
    let resource_mod_content = generate_mod_rs_for_directory(resource_dir, "FHIR resource types")?;
    fs::write(resource_dir.join("mod.rs"), resource_mod_content)?;

    // Generate datatypes/mod.rs
    let datatypes_mod_content = generate_mod_rs_for_directory(datatypes_dir, "FHIR data types")?;
    fs::write(datatypes_dir.join("mod.rs"), datatypes_mod_content)?;

    // Generate extensions/mod.rs
    let extensions_mod_content =
        generate_mod_rs_for_directory(extensions_dir, "FHIR extension types")?;
    fs::write(extensions_dir.join("mod.rs"), extensions_mod_content)?;

    // Generate primitives/mod.rs
    let primitives_mod_content =
        generate_mod_rs_for_directory(primitives_dir, "FHIR primitive types")?;
    fs::write(primitives_dir.join("mod.rs"), primitives_mod_content)?;

    // Generate traits/mod.rs (placeholder for now, but don't overwrite existing content)
    let traits_mod_path = traits_dir.join("mod.rs");
    if !traits_mod_path.exists() || fs::read_to_string(&traits_mod_path)?.len() < 500 {
        // If file is small, it's likely just placeholder
        let traits_mod_content = generate_traits_mod_rs()?;
        fs::write(traits_mod_path, traits_mod_content)?;
    }

    // Generate bindings/mod.rs for ValueSet enums
    let bindings_mod_content =
        generate_mod_rs_for_directory(bindings_dir, "FHIR ValueSet bindings and enums")?;
    fs::write(bindings_dir.join("mod.rs"), bindings_mod_content)?;

    // Generate profiles/mod.rs for FHIR profiles
    let profiles_mod_content =
        generate_mod_rs_for_directory(profiles_dir, "FHIR profiles derived from core resources")?;
    fs::write(profiles_dir.join("mod.rs"), profiles_mod_content)?;

    Ok(())
}

/// Generate mod.rs content for a specific directory
fn generate_mod_rs_for_directory(dir: &Path, description: &str) -> Result<String> {
    let mut content = String::new();
    content.push_str(&format!("//! {description}\n\n"));

    // Get all .rs files in the directory
    let mut rs_files = Vec::new();
    if dir.exists() {
        for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.is_file() && path.extension().is_some_and(|ext| ext == "rs") {
                if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
                    if stem != "mod" {
                        rs_files.push(stem.to_string());
                    }
                }
            }
        }
    }

    // Sort module names for consistency
    rs_files.sort();

    // Generate module declarations
    for module_name in &rs_files {
        content.push_str(&format!("pub mod {module_name};\n"));
    }

    // Note: No glob re-exports to avoid ambiguous re-export warnings
    // Individual types can be imported explicitly when needed

    Ok(content)
}

/// Generate traits/mod.rs with placeholder content
fn generate_traits_mod_rs() -> Result<String> {
    let content = r#"//! FHIR traits for common functionality
//!
//! This module contains traits that define common interfaces for FHIR types.

// Placeholder traits - these would be generated based on FHIR structure definitions

/// Trait for types that have extensions
pub trait HasExtensions {
    /// Get the extensions for this type
    fn extensions(&self) -> &[crate::datatypes::extension::Extension];
}

/// Trait for FHIR resources
pub trait Resource {
    /// Get the resource type name
    fn resource_type(&self) -> &'static str;
    
    /// Get the logical id of this resource
    fn id(&self) -> Option<&str>;
    
    /// Get the metadata about this resource
    fn meta(&self) -> Option<&crate::datatypes::meta::Meta>;
}

/// Trait for domain resources (resources that can have narrative)
pub trait DomainResource: Resource + HasExtensions {
    /// Get the narrative text for this domain resource
    fn narrative(&self) -> Option<&crate::datatypes::narrative::Narrative>;
}
"#;

    Ok(content.to_string())
}

/// Generate statistics from organized directories
fn generate_crate_statistics_from_organized_dirs(
    resource_dir: &Path,
    datatypes_dir: &Path,
    extensions_dir: &Path,
    primitives_dir: &Path,
) -> Result<CrateStatistics> {
    let mut num_structs = 0;
    let num_enums = 0; // Enums would be handled separately

    // Count files in each directory
    for dir in [resource_dir, datatypes_dir, extensions_dir, primitives_dir] {
        if dir.exists() {
            for entry in fs::read_dir(dir)? {
                let entry = entry?;
                let path = entry.path();
                if path.is_file() && path.extension().is_some_and(|ext| ext == "rs") {
                    if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
                        if stem != "mod" {
                            num_structs += 1;

                            // Count additional structs within the file (nested types)
                            if let Ok(content) = fs::read_to_string(&path) {
                                num_structs +=
                                    content.matches("pub struct ").count().saturating_sub(1);
                            }
                        }
                    }
                }
            }
        }
    }

    let total_types = num_structs + num_enums;

    Ok(CrateStatistics {
        num_structs,
        num_enums,
        total_types,
        canonical_url: "Unknown".to_string(),
    })
}

/// Generate README.md content with package information and statistics
#[allow(clippy::too_many_arguments)]
fn generate_readme_md(
    package: &str,
    version: &str,
    canonical_url: &str,
    author: &str,
    description: &str,
    command_invoked: &str,
    stats: &CrateStatistics,
    crate_name_override: Option<&str>,
) -> String {
    let derived = package.replace(['.', '-'], "_");
    // Use lib name form (underscores) for Rust `use` statements in README
    let crate_name = crate_name_override.unwrap_or(&derived).replace('-', "_");
    let mut content = String::new();

    content.push_str(&format!("# {crate_name}\n\n"));
    content.push_str(&format!("**Generated FHIR Types for {package}**\n\n"));
    content.push_str(&format!("This crate contains automatically generated Rust types for FHIR (Fast Healthcare Interoperability Resources) based on the `{package}` package.\n\n"));

    content.push_str("## Important Notice\n\n");
    content
        .push_str("**This crate was automatically generated using the RH codegen CLI tool.**\n\n");
    content.push_str(&format!(
        "- **Generator command**:\n```bash\n{command_invoked}\n```\n\n"
    ));
    content.push_str(&format!("- **Generation timestamp**: {}\n\n", Local::now()));

    content.push_str("## Package Information\n\n");

    content.push_str(&format!("* **Package Name** {package}\n"));
    content.push_str(&format!("* **Package Author** {author}\n"));
    content.push_str(&format!("* **Version** {version}\n"));
    content.push_str(&format!("* **Canonical URL** `{canonical_url}`\n\n"));

    content.push_str(&format!(
        "**Statistics: {} structs, {} enums, {} total types**\n\n",
        stats.num_structs, stats.num_enums, stats.total_types
    ));

    content.push_str(&format!("## Description\n\n{description}"));

    content.push_str("\n\n");

    content.push_str("## Features\n\n");
    content.push_str(
        "- **Complete FHIR type definitions** - All resources, datatypes, and primitives\n",
    );
    content.push_str(
        "- **Serde serialization** - Built-in JSON serialization/deserialization support\n",
    );
    content.push_str(
        "- **Type metadata** - Compile-time metadata for field types and path resolution\n",
    );
    content.push_str(
        "- **Idiomatic Rust** - Clean, organized module structure with proper naming conventions\n",
    );
    content.push_str("- **Zero-cost abstractions** - PHF (perfect hash function) maps for O(1) metadata lookups\n\n");

    content.push_str("## Usage\n\n");
    content.push_str("Add this crate to your `Cargo.toml`:\n\n");
    content.push_str("```toml\n");
    content.push_str("[dependencies]\n");
    content.push_str(&format!("{crate_name} = \"0.1.0\"\n"));
    content.push_str("```\n\n");

    content.push_str("### Deserializing FHIR Resources\n\n");
    content.push_str("```rust\n");
    content.push_str(&format!("use {crate_name}::resources::patient::Patient;\n"));
    content.push_str("use serde_json;\n\n");
    content.push_str("let json_data = r#\"{\\\"resourceType\\\": \\\"Patient\\\", \\\"id\\\": \\\"example\\\"}\"#;\n");
    content.push_str("let patient: Patient = serde_json::from_str(json_data)?;\n\n");
    content.push_str("println!(\"Patient ID: {}\", patient.id.unwrap_or_default());\n");
    content.push_str("```\n\n");

    content.push_str("### Creating Resources Programmatically\n\n");
    content.push_str("This crate provides two idiomatic ways to work with FHIR resources using builder traits:\n\n");

    content.push_str("#### Option 1: Resource Module with Re-exported Traits (Recommended)\n\n");
    content.push_str("Each resource module re-exports its associated traits for convenience:\n\n");
    content.push_str("```rust\n");
    content.push_str("// Import resource with its traits - all in one place!\n");
    content.push_str(&format!(
        "use {crate_name}::resources::patient::{{Patient, PatientMutators}};\n"
    ));
    content.push_str(&format!(
        "use {crate_name}::prelude::*;  // Gets base traits (ResourceMutators, etc.)\n"
    ));
    content.push_str(&format!(
        "use {crate_name}::datatypes::human_name::HumanName;\n\n"
    ));
    content.push_str("// Build a patient using the builder pattern\n");
    content.push_str("let patient = <Patient as PatientMutators>::new()\n");
    content.push_str("    .set_id(\"patient-123\".to_string())\n");
    content.push_str("    .set_active(true)\n");
    content.push_str("    .add_name(HumanName {\n");
    content.push_str("        family: Some(\"Doe\".to_string()),\n");
    content.push_str("        given: vec![\"John\".to_string()],\n");
    content.push_str("        ..Default::default()\n");
    content.push_str("    })\n");
    content.push_str("    .set_gender(Some(AdministrativeGender::Male))\n");
    content.push_str("    .set_birth_date(\"1990-01-15\".to_string());\n");
    content.push_str("```\n\n");

    content.push_str("#### Option 2: Prelude Module\n\n");
    content.push_str("For common base traits, use the prelude module:\n\n");
    content.push_str("```rust\n");
    content.push_str(&format!(
        "use {crate_name}::prelude::*;  // ValidatableResource, ResourceMutators, etc.\n"
    ));
    content.push_str(&format!(
        "use {crate_name}::resources::patient::{{Patient, PatientMutators}};\n\n"
    ));
    content.push_str("let patient = <Patient as PatientMutators>::new()\n");
    content.push_str("    .set_id(\"example\".to_string());\n");
    content.push_str("```\n\n");
    content.push_str("The prelude includes:\n");
    content.push_str("- `ValidatableResource` - Access invariants and validation rules\n");
    content.push_str("- `ResourceMutators` - Builder methods for all resources\n");
    content.push_str("- `DomainResourceMutators` - Builder methods for domain resources\n\n");

    content.push_str("#### Direct Struct Construction\n\n");
    content.push_str("You can also construct resources directly:\n\n");
    content.push_str("```rust\n");
    content.push_str(&format!("use {crate_name}::resources::patient::Patient;\n"));
    content.push_str(&format!(
        "use {crate_name}::datatypes::human_name::HumanName;\n"
    ));
    content.push_str(&format!(
        "use {crate_name}::bindings::administrative_gender::AdministrativeGender;\n\n"
    ));
    content.push_str("let patient = Patient {\n");
    content.push_str("    id: Some(\"patient-123\".to_string()),\n");
    content.push_str("    active: Some(true),\n");
    content.push_str("    name: vec![HumanName {\n");
    content.push_str("        family: Some(\"Doe\".to_string()),\n");
    content.push_str("        given: vec![\"John\".to_string()],\n");
    content.push_str("        ..Default::default()\n");
    content.push_str("    }],\n");
    content.push_str("    gender: Some(AdministrativeGender::Male),\n");
    content.push_str("    birth_date: Some(\"1990-01-15\".to_string()),\n");
    content.push_str("    ..Default::default()\n");
    content.push_str("};\n");
    content.push_str("```\n\n");

    content.push_str("### Using Type Metadata\n\n");
    content.push_str("This crate includes compile-time metadata for all FHIR types, enabling runtime type introspection and path resolution:\n\n");
    content.push_str("```rust\n");
    content.push_str(&format!("use {crate_name}::metadata::{{resolve_path, get_field_info, FhirFieldType, FhirPrimitiveType}};\n\n"));
    content.push_str("// Resolve nested paths to their FHIR types\n");
    content.push_str("if let Some(field_type) = resolve_path(\"Patient.birthDate\") {\n");
    content.push_str("    match field_type {\n");
    content.push_str("        FhirFieldType::Primitive(FhirPrimitiveType::Date) => {\n");
    content.push_str("            println!(\"birthDate is a FHIR date type\");\n");
    content.push_str("        }\n");
    content.push_str("        _ => {}\n");
    content.push_str("    }\n");
    content.push_str("}\n\n");
    content.push_str("// Resolve complex nested paths\n");
    content.push_str("if let Some(field_type) = resolve_path(\"Patient.name.given\") {\n");
    content.push_str("    match field_type {\n");
    content.push_str("        FhirFieldType::Primitive(FhirPrimitiveType::String) => {\n");
    content.push_str("            println!(\"name.given is a string array\");\n");
    content.push_str("        }\n");
    content.push_str("        _ => {}\n");
    content.push_str("    }\n");
    content.push_str("}\n\n");
    content.push_str("// Get field information directly\n");
    content.push_str("if let Some(field_info) = get_field_info(\"Patient\", \"active\") {\n");
    content.push_str("    println!(\"Min cardinality: {}\", field_info.min);\n");
    content.push_str("    println!(\"Max cardinality: {:?}\", field_info.max);\n");
    content.push_str("    println!(\"Is choice type: {}\", field_info.is_choice_type);\n");
    content.push_str("}\n");
    content.push_str("```\n\n");

    content.push_str("The metadata system enables:\n");
    content.push_str("- **Path resolution** - Navigate nested paths like `Patient.name.given`\n");
    content.push_str("- **Type introspection** - Determine field types at runtime\n");
    content.push_str("- **Cardinality information** - Min/max occurrence constraints\n");
    content.push_str("- **Choice type detection** - Identify polymorphic fields\n");
    content
        .push_str("- **Zero runtime cost** - All lookups use compile-time perfect hash maps\n\n");

    content.push_str("## Structure\n\n");
    content.push_str("This crate organizes FHIR types into logical modules:\n\n");
    content.push_str("- **resources/** - All FHIR resources (Patient, Observation, etc.)\n");
    content.push_str("- **profiles/** - FHIR profiles (Vitalsigns, BodyHeight, etc.)\n");
    content.push_str(
        "- **datatypes/** - Complex and primitive datatypes (HumanName, Address, etc.)\n",
    );
    content.push_str("- **bindings/** - ValueSet enumerations (AdministrativeGender, etc.)\n");
    content.push_str("- **primitives/** - Base primitive types (DateType, DateTimeType, etc.)\n");
    content.push_str("- **traits/** - Mutator, accessor, and existence traits for all types\n");
    content.push_str(
        "- **prelude.rs** - Commonly used traits (ValidatableResource, ResourceMutators, etc.)\n",
    );
    content.push_str("- **metadata.rs** - Type metadata and path resolution functions\n\n");

    content.push_str("## Regenerating This Crate\n\n");
    content.push_str("To regenerate this crate with updated FHIR definitions:\n\n");
    content.push_str("```bash\n");
    content.push_str(command_invoked);
    content.push_str("\n```\n\n");

    content.push_str("## License\n\n");
    content.push_str("This generated crate is provided under MIT OR Apache-2.0 license.\n\n");

    content.push_str("## Related Links\n\n");
    content.push_str("- [FHIR Specification](https://hl7.org/fhir/)\n");
    content.push_str("- [FHIR Package Registry](https://packages.fhir.org/)\n");
    content.push_str("- [RH Project](https://github.com/reasonhealth/rh)\n\n");
    content.push_str("---\n\n");
    content.push_str(&format!(
        "*Generated by RH codegen tool at {}*\n",
        Local::now()
    ));

    content
}

/// Generate a prelude module with commonly used trait re-exports
fn generate_prelude_module() -> String {
    r#"//! Prelude module - commonly used traits for convenience
//!
//! This module re-exports the most commonly used traits for working with
//! FHIR resources. Import this module to avoid having to import individual
//! traits from the `traits` module.
//!
//! # Example
//!
//! ```ignore
//! use hl7_fhir_r4_core::prelude::*;
//! use hl7_fhir_r4_core::resources::patient::Patient;
//!
//! // All mutator traits are now in scope
//! let patient = <Patient as PatientMutators>::new()
//!     .set_id("example".to_string())
//!     .set_active(true);
//! ```

// Resource mutator traits - for building resources with method chaining
pub use crate::traits::resource::ResourceMutators;
pub use crate::traits::domain_resource::DomainResourceMutators;

// Note: Individual resource mutator traits (PatientMutators, ObservationMutators, etc.)
// are re-exported from their respective resource modules for convenience.
// For example: use hl7_fhir_r4_core::resources::patient::PatientMutators;

// Validation trait
pub use crate::validation::ValidatableResource;
"#
    .to_string()
}

/// Parse package.json to extract metadata for crate generation
pub fn parse_package_metadata(package_json_path: &Path) -> Result<(String, String, String)> {
    let package_json_content = fs::read_to_string(package_json_path)?;
    let package_json: serde_json::Value = serde_json::from_str(&package_json_content)?;

    let canonical = package_json
        .get("canonical")
        .and_then(|v| v.as_str())
        .unwrap_or("Unknown")
        .to_string();

    let author = package_json
        .get("author")
        .and_then(|v| v.as_str())
        .unwrap_or("FHIR Code Generator")
        .to_string();

    let description = package_json
        .get("description")
        .and_then(|v| v.as_str())
        .unwrap_or("Generated FHIR types crate.")
        .to_string();

    Ok((canonical, author, description))
}