rh-codegen 0.2.0

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
use std::fs;
use std::path::Path;

use crate::generators::import_manager::ImportManager;
use crate::rust_types::RustTrait;
use crate::{CodegenError, CodegenResult};

use super::FileGenerator;

impl<'a> FileGenerator<'a> {
    pub fn generate_trait_to_file<P: AsRef<Path>>(
        &self,
        _structure_def: &crate::fhir_types::StructureDefinition,
        output_path: P,
        rust_trait: &RustTrait,
    ) -> CodegenResult<()> {
        let mut all_tokens = proc_macro2::TokenStream::new();

        let mut imports = std::collections::HashSet::new();
        ImportManager::collect_custom_types_from_trait(rust_trait, &mut imports);

        for import_path in imports {
            let import_stmt = format!("use {import_path};");
            let import_tokens: proc_macro2::TokenStream =
                import_stmt.parse().map_err(|e| CodegenError::Generation {
                    message: format!("Failed to parse import statement '{import_stmt}': {e}"),
                })?;
            all_tokens.extend(import_tokens);
        }

        let trait_tokens = self.token_generator.generate_trait(rust_trait);
        all_tokens.extend(trait_tokens);

        if std::env::var("DEBUG_TOKENS").is_ok() {
            eprintln!(
                "DEBUG: Generated tokens for trait '{}': {}",
                rust_trait.name, all_tokens
            );
        }

        let syntax_tree = syn::parse2(all_tokens).map_err(|e| CodegenError::Generation {
            message: format!(
                "Failed to parse generated trait tokens for '{}': {e}",
                rust_trait.name
            ),
        })?;

        let formatted_code = prettyplease::unparse(&syntax_tree);

        if output_path.as_ref().exists() {
            eprintln!(
                "Warning: Trait file '{}' already exists and will be overwritten.",
                output_path.as_ref().display()
            );
        }

        fs::write(output_path.as_ref(), formatted_code)?;

        Ok(())
    }

    pub fn generate_traits_to_file<P: AsRef<Path>>(
        &self,
        _structure_def: &crate::fhir_types::StructureDefinition,
        output_path: P,
        rust_traits: &[&RustTrait],
    ) -> CodegenResult<()> {
        let mut all_tokens = proc_macro2::TokenStream::new();

        let mut imports = std::collections::HashSet::new();
        for rust_trait in rust_traits {
            ImportManager::collect_custom_types_from_trait(rust_trait, &mut imports);
        }

        for import_path in imports {
            let import_stmt = format!("use {import_path};");
            let import_tokens: proc_macro2::TokenStream =
                import_stmt.parse().map_err(|e| CodegenError::Generation {
                    message: format!("Failed to parse import statement '{import_stmt}': {e}"),
                })?;
            all_tokens.extend(import_tokens);
        }

        for rust_trait in rust_traits {
            let trait_tokens = self.token_generator.generate_trait(rust_trait);
            all_tokens.extend(trait_tokens);
        }

        if std::env::var("DEBUG_TOKENS").is_ok() {
            let trait_names: Vec<&str> = rust_traits.iter().map(|t| t.name.as_str()).collect();
            eprintln!(
                "DEBUG: Generated tokens for traits [{}]: {}",
                trait_names.join(", "),
                all_tokens
            );
        }

        let syntax_tree = syn::parse2(all_tokens).map_err(|e| CodegenError::Generation {
            message: format!("Failed to parse generated trait tokens: {e}"),
        })?;

        let formatted_code = prettyplease::unparse(&syntax_tree);

        if output_path.as_ref().exists() {
            eprintln!(
                "Warning: Trait file '{}' already exists and will be overwritten.",
                output_path.as_ref().display()
            );
        }

        fs::write(output_path.as_ref(), formatted_code)?;

        Ok(())
    }

    pub fn generate_trait_file_from_trait<P: AsRef<Path>>(
        &self,
        rust_trait: &RustTrait,
        output_path: P,
    ) -> CodegenResult<()> {
        let mut all_tokens = proc_macro2::TokenStream::new();

        let mut imports = std::collections::HashSet::new();
        ImportManager::collect_custom_types_from_trait(rust_trait, &mut imports);

        for import_path in imports {
            let import_stmt = format!("use {import_path};");
            let import_tokens: proc_macro2::TokenStream =
                import_stmt.parse().map_err(|e| CodegenError::Generation {
                    message: format!("Failed to parse import statement '{import_stmt}': {e}"),
                })?;
            all_tokens.extend(import_tokens);
        }

        let trait_tokens = self.token_generator.generate_trait(rust_trait);
        all_tokens.extend(trait_tokens);

        let syntax_tree = syn::parse2(all_tokens).map_err(|e| CodegenError::Generation {
            message: format!("Failed to parse generated trait tokens: {e}"),
        })?;

        let formatted_code = prettyplease::unparse(&syntax_tree);

        if output_path.as_ref().exists() {
            eprintln!(
                "Warning: Trait file '{}' already exists and will be overwritten.",
                output_path.as_ref().display()
            );
        }

        fs::write(output_path.as_ref(), formatted_code)?;

        Ok(())
    }

    pub(crate) fn generate_trait_implementations(
        &self,
        structure_def: &crate::fhir_types::StructureDefinition,
    ) -> String {
        let trait_impl_generator =
            crate::generators::trait_impl_generator::TraitImplGenerator::new();
        let trait_impls = match trait_impl_generator.generate_trait_impls(structure_def) {
            Ok(impls) => impls,
            Err(e) => {
                eprintln!(
                    "Warning: Failed to generate trait implementations for {}: {}",
                    structure_def.name, e
                );
                return String::new();
            }
        };

        let mut implementations = Vec::new();

        for trait_impl in trait_impls {
            let impl_tokens = self.token_generator.generate_trait_impl(&trait_impl);

            match syn::parse2(impl_tokens.clone()) {
                Ok(syntax_tree) => {
                    let formatted_impl = prettyplease::unparse(&syntax_tree);
                    implementations.push(formatted_impl);
                }
                Err(e) => {
                    eprintln!(
                        "Warning: Failed to parse trait implementation for {}: {}",
                        trait_impl.struct_name, e
                    );
                    eprintln!("Generated tokens:\n{impl_tokens}");
                }
            }
        }

        if implementations.is_empty() {
            String::new()
        } else {
            format!("// Trait implementations\n{}", implementations.join("\n\n"))
        }
    }

    pub(crate) fn generate_trait_reexports(
        &self,
        structure_def: &crate::fhir_types::StructureDefinition,
    ) -> String {
        let is_profile = crate::generators::type_registry::TypeRegistry::is_profile(structure_def);

        let (trait_module_name, trait_prefix) = if is_profile {
            let struct_name = crate::naming::Naming::struct_name(structure_def);
            let snake_module = crate::naming::Naming::to_rust_identifier(
                &crate::naming::Naming::to_snake_case(&struct_name),
            );
            (snake_module, struct_name)
        } else {
            let resource_name = crate::naming::Naming::to_rust_identifier(&structure_def.name);
            let snake_name = crate::naming::Naming::to_rust_identifier(
                &crate::naming::Naming::to_snake_case(&resource_name),
            );
            (snake_name, resource_name)
        };

        format!(
            r#"// Re-export traits for convenient importing
// This allows users to just import the resource module and get all associated traits
pub use crate::traits::{trait_module_name}::{{
    {trait_prefix}Mutators,
    {trait_prefix}Accessors,
    {trait_prefix}Existence,
}};"#
        )
    }

    pub(crate) fn generate_default_implementation(
        &self,
        structure_def: &crate::fhir_types::StructureDefinition,
        rust_struct: &crate::rust_types::RustStruct,
    ) -> String {
        let is_profile = crate::generators::type_registry::TypeRegistry::is_profile(structure_def);
        if is_profile {
            return String::new();
        }

        let struct_name = &rust_struct.name;

        if rust_struct.derives.iter().any(|d| d == "Default") {
            return String::new();
        }

        let elements = if let Some(differential) = &structure_def.differential {
            &differential.element
        } else if let Some(snapshot) = &structure_def.snapshot {
            &snapshot.element
        } else {
            &Vec::new()
        };

        let mut required_fields = Vec::new();
        for element in elements {
            let path_parts: Vec<&str> = element.path.split('.').collect();
            if path_parts.len() == 2 && path_parts[0] == structure_def.name {
                let field_name = path_parts[1];
                if let Some(min) = element.min {
                    if min >= 1 && !field_name.ends_with("[x]") {
                        required_fields.push((field_name, element.clone()));
                    }
                }
            }
        }

        let mut field_inits = Vec::new();

        if let Some(base_def) = &rust_struct.base_definition {
            let base_type = base_def.split('/').next_back().unwrap_or(base_def);
            let base_type = crate::naming::Naming::to_rust_identifier(base_type);
            let proper_base_type = if base_type
                .chars()
                .next()
                .map(|c| c.is_lowercase())
                .unwrap_or(false)
            {
                crate::naming::Naming::capitalize_first(&base_type)
            } else {
                base_type
            };
            field_inits.push(format!("base: {proper_base_type}::default()"));
        }

        for field in &rust_struct.fields {
            let field_name = &field.name;

            let is_required = required_fields.iter().any(|(name, _)| {
                let snake_name = crate::naming::Naming::to_snake_case(name);
                snake_name == *field_name
            });

            if is_required {
                let default_value = match field.field_type.to_string().as_str() {
                    s if s.contains("::") && !s.contains("Option") && !s.contains("Vec") => {
                        format!("{s}::default()")
                    }
                    "String" => "String::new()".to_string(),
                    "i32" | "i64" | "u32" | "u64" => "0".to_string(),
                    "f32" | "f64" => "0.0".to_string(),
                    "bool" => "false".to_string(),
                    s if s.starts_with("Vec<") => "Vec::new()".to_string(),
                    _ => format!("{}::default()", field.field_type.to_string()),
                };
                field_inits.push(format!("{field_name}: {default_value}"));
            } else {
                field_inits.push(format!("{field_name}: Default::default()"));
            }
        }

        let impl_block = format!(
            r#"impl Default for {} {{
    fn default() -> Self {{
        Self {{
            {}
        }}
    }}
}}"#,
            struct_name,
            field_inits.join(",\n            ")
        );

        impl_block
    }

    pub(crate) fn generate_nested_struct_default_implementation(
        &self,
        parent_structure_def: &crate::fhir_types::StructureDefinition,
        nested_struct: &crate::rust_types::RustStruct,
    ) -> String {
        let struct_name = &nested_struct.name;

        if nested_struct.derives.iter().any(|d| d == "Default") {
            return String::new();
        }

        let parent_name = &parent_structure_def.name;
        let nested_field_name = if struct_name.starts_with(parent_name) {
            let suffix = &struct_name[parent_name.len()..];
            crate::naming::Naming::to_snake_case(suffix)
        } else {
            return String::new();
        };

        let base_path = format!("{parent_name}.{nested_field_name}");

        let elements = if let Some(differential) = &parent_structure_def.differential {
            &differential.element
        } else if let Some(snapshot) = &parent_structure_def.snapshot {
            &snapshot.element
        } else {
            &Vec::new()
        };

        let mut required_fields = Vec::new();
        for element in elements {
            if element.path.starts_with(&format!("{base_path}.")) {
                let field_path = element
                    .path
                    .strip_prefix(&format!("{base_path}."))
                    .unwrap_or_else(|| {
                        panic!(
                            "codegen bug: element path '{}' does not start with '{base_path}.'",
                            element.path
                        )
                    });
                if !field_path.contains('.') && !field_path.ends_with("[x]") {
                    if let Some(min) = element.min {
                        if min >= 1 {
                            required_fields.push((field_path, element.clone()));
                        }
                    }
                }
            }
        }

        let mut field_inits = Vec::new();

        if let Some(base_def) = &nested_struct.base_definition {
            let base_type = base_def.split('/').next_back().unwrap_or(base_def);
            let base_type = crate::naming::Naming::to_rust_identifier(base_type);
            let proper_base_type = if base_type
                .chars()
                .next()
                .map(|c| c.is_lowercase())
                .unwrap_or(false)
            {
                crate::naming::Naming::capitalize_first(&base_type)
            } else {
                base_type
            };
            field_inits.push(format!("base: {proper_base_type}::default()"));
        }

        for field in &nested_struct.fields {
            let field_name = &field.name;

            let is_required = required_fields.iter().any(|(name, _)| {
                let snake_name = crate::naming::Naming::to_snake_case(name);
                snake_name == *field_name
            });

            if is_required {
                let default_value = match field.field_type.to_string().as_str() {
                    s if s.contains("::") && !s.contains("Option") && !s.contains("Vec") => {
                        format!("{s}::default()")
                    }
                    "String" => "String::new()".to_string(),
                    "i32" | "i64" | "u32" | "u64" => "0".to_string(),
                    "f32" | "f64" => "0.0".to_string(),
                    "bool" => "false".to_string(),
                    s if s.starts_with("Vec<") => "Vec::new()".to_string(),
                    _ => format!("{}::default()", field.field_type.to_string()),
                };
                field_inits.push(format!("{field_name}: {default_value}"));
            } else {
                field_inits.push(format!("{field_name}: Default::default()"));
            }
        }

        let impl_block = format!(
            r#"impl Default for {} {{
    fn default() -> Self {{
        Self {{
            {}
        }}
    }}
}}"#,
            struct_name,
            field_inits.join(",\n            ")
        );

        impl_block
    }
}