romance-core 0.2.5

Core library for Romance CLI code generation
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
use crate::entity::{EntityDefinition, FieldType, FieldVisibility, RelationType, ValidationRule};
use crate::generator::context::{self, markers, ProjectFeatures};
use crate::generator::junction;
use crate::generator::plan::{self, GenerationTracker};
use crate::relation;
use crate::template::TemplateEngine;
use crate::utils;
use anyhow::Result;
use heck::{ToPascalCase, ToSnakeCase};
use std::path::Path;
use tera::Context;

/// Pre-validate that all required markers exist before generation.
pub fn validate(_entity: &EntityDefinition) -> Result<()> {
    let base = Path::new("backend/src");
    let mut checks = vec![
        plan::check(base.join("routes/mod.rs"), markers::ROUTES),
        plan::check(base.join("routes/mod.rs"), markers::MODS),
        plan::check(base.join("entities/mod.rs"), markers::MODS),
        plan::check(base.join("handlers/mod.rs"), markers::MODS),
    ];

    let seed_path = Path::new("backend/src/seed.rs");
    if seed_path.exists() {
        checks.push(plan::check(seed_path, markers::SEEDS));
    }

    let main_rs = base.join("main.rs");
    if main_rs.exists() {
        let content = std::fs::read_to_string(&main_rs).unwrap_or_default();
        if content.contains(markers::OPENAPI_PATHS) {
            checks.push(plan::check(&main_rs, markers::OPENAPI_PATHS));
            checks.push(plan::check(&main_rs, markers::OPENAPI_SCHEMAS));
            checks.push(plan::check(&main_rs, markers::OPENAPI_TAGS));
        }
    }

    plan::validate_markers(&checks)
}

pub fn generate(entity: &EntityDefinition, tracker: &mut GenerationTracker) -> Result<()> {
    let engine = TemplateEngine::new()?;
    let ctx = build_context(entity);
    let snake_name = entity.name.to_snake_case();

    let base = Path::new("backend/src");

    // Generate model
    let model_content = engine.render("entity/backend/model.rs.tera", &ctx)?;
    let model_path = base.join(format!("entities/{}.rs", snake_name));
    utils::write_generated(&model_path, &model_content)?;
    tracker.track(model_path.to_path_buf());

    // Generate handlers
    let handlers_content = engine.render("entity/backend/handlers.rs.tera", &ctx)?;
    let handlers_path = base.join(format!("handlers/{}.rs", snake_name));
    utils::write_generated(&handlers_path, &handlers_content)?;
    tracker.track(handlers_path.to_path_buf());

    // Generate routes
    let routes_content = engine.render("entity/backend/routes.rs.tera", &ctx)?;
    let routes_path = base.join(format!("routes/{}.rs", snake_name));
    utils::write_generated(&routes_path, &routes_content)?;
    tracker.track(routes_path.to_path_buf());

    // Register module in entities/handlers/routes mod.rs files
    context::register_backend_module(base, &snake_name)?;

    // Register entity in OpenAPI spec (only if OpenAPI markers are present)
    let main_rs = base.join("main.rs");
    let main_has_openapi = main_rs.exists()
        && std::fs::read_to_string(&main_rs)
            .map(|c| c.contains(markers::OPENAPI_PATHS))
            .unwrap_or(false);
    if main_has_openapi {
        // Add handler paths
        let paths = vec![
            format!("crate::handlers::{}::list", snake_name),
            format!("crate::handlers::{}::get", snake_name),
            format!("crate::handlers::{}::create", snake_name),
            format!("crate::handlers::{}::update", snake_name),
            format!("crate::handlers::{}::delete", snake_name),
            format!("crate::handlers::{}::bulk_create", snake_name),
            format!("crate::handlers::{}::bulk_delete", snake_name),
        ];
        for path in &paths {
            utils::insert_at_marker(
                &main_rs,
                markers::OPENAPI_PATHS,
                &format!("        {},", path),
            )?;
        }

        // Add schema types
        let schemas = vec![
            format!("crate::entities::{}::Model", snake_name),
            format!("crate::entities::{}::Create{}", snake_name, entity.name),
            format!("crate::entities::{}::Update{}", snake_name, entity.name),
            format!(
                "crate::entities::{}::{}Response",
                snake_name, entity.name
            ),
        ];
        for schema in &schemas {
            utils::insert_at_marker(
                &main_rs,
                markers::OPENAPI_SCHEMAS,
                &format!("            {},", schema),
            )?;
        }

        // Add tag
        utils::insert_at_marker(
            &main_rs,
            markers::OPENAPI_TAGS,
            &format!(
                "        (name = \"{}\", description = \"{} management\"),",
                entity.name, entity.name
            ),
        )?;
    }

    // Handle reverse relations: inject has-many into target entities
    // Note: junction (M2M) generation is deferred to generate_relations()
    // to ensure the entity migration runs first.
    let project_root = Path::new(".");
    let features = ProjectFeatures::load(project_root);
    for rel in &entity.relations {
        if rel.relation_type == RelationType::BelongsTo {
            // Skip self-referential FKs — the Related impl is already generated
            // by the model template, and injecting a reverse has-many would create
            // a duplicate Related impl (Bug #3)
            if rel.target_entity.to_snake_case() == entity.name.to_snake_case() {
                continue;
            }
            if relation::entity_exists(project_root, &rel.target_entity) {
                inject_has_many(
                    base,
                    &rel.target_entity,
                    &entity.name,
                    &rel.fk_column.clone().unwrap_or_else(|| format!("{}_id", rel.target_entity.to_snake_case())),
                    &features.api_prefix,
                )?;
            }
        }
    }

    utils::ui::success(&format!("Generated backend files for '{}'", entity.name));

    // Insert seed function if seed.rs exists
    let seed_path = Path::new("backend/src/seed.rs");
    if seed_path.exists() {
        let seed_fn = build_seed_function(entity);
        utils::insert_at_marker(seed_path, markers::SEEDS, &seed_fn)?;
    }

    Ok(())
}

/// Generate M2M junction tables and apply pending relations.
/// Must be called AFTER migration::generate() to ensure correct migration order.
pub fn generate_relations(entity: &EntityDefinition) -> Result<()> {
    let project_root = Path::new(".");

    for rel in &entity.relations {
        if rel.relation_type == RelationType::ManyToMany {
            junction::generate(&entity.name, &rel.target_entity)?;
        }
    }

    // Apply any pending relations that target this newly generated entity
    let pending = relation::take_pending_for(project_root, &entity.name)?;
    for p in &pending {
        if p.relation_type == "ManyToMany" {
            junction::generate(&p.source_entity, &p.target_entity)?;
            println!("  Applied pending M2M: {} <-> {}", p.source_entity, p.target_entity);
        }
    }

    Ok(())
}

/// Inject has-many relation into an existing target entity.
/// When we generate Post with author_id:uuid->User, we inject into User:
/// 1. Related<post::Entity> impl in user model
/// 2. list_posts handler in user handlers
/// 3. /users/:id/posts route in user routes
fn inject_has_many(
    base: &Path,
    parent_entity: &str,
    child_entity: &str,
    fk_column: &str,
    api_prefix: &str,
) -> Result<()> {
    let parent_snake = parent_entity.to_snake_case();
    let child_snake = child_entity.to_snake_case();
    let fk_pascal = fk_column.to_pascal_case();

    // Derive the Relation variant name from the FK column (matches model template)
    let fk_base = fk_column.strip_suffix("_id").unwrap_or(fk_column);
    let relation_variant = fk_base.to_pascal_case();

    // 1. Inject Related impl into parent model
    // Only inject if Related<child::Entity> doesn't already exist — when multiple FKs
    // from child to parent exist (e.g., creator_id + assignee_id → User), only the first
    // Related impl should be generated (Rust allows only one impl per trait+type pair).
    let model_path = base.join(format!("entities/{}.rs", parent_snake));
    let related_check = format!("impl Related<super::{}::Entity> for Entity", child_snake);
    let model_content = std::fs::read_to_string(&model_path).unwrap_or_default();
    if !model_content.contains(&related_check) {
        let related_impl = format!(
            r#"impl Related<super::{}::Entity> for Entity {{
    fn to() -> RelationDef {{
        super::{}::Relation::{}.def().rev()
    }}
}}"#,
            child_snake, child_snake, relation_variant
        );
        utils::insert_at_marker(&model_path, markers::RELATIONS, &related_impl)?;
    }

    // 2. Inject list handler into parent handlers
    // Disambiguate handler name when FK doesn't match simple parent_id pattern.
    // e.g., sender_id on Account (parent=User) -> list_accounts_by_sender
    // e.g., user_id on Post (parent=User) -> list_posts (no suffix needed)
    let child_plural = utils::pluralize(&child_snake);
    let handler_name = if fk_base == parent_snake {
        format!("list_{}", child_plural)
    } else {
        format!("list_{}_by_{}", child_plural, fk_base)
    };

    // Skip handler/route injection if parent doesn't have its own handlers/routes files.
    // This happens for auth-generated entities (User) where handlers live in auth.rs.
    let handlers_path = base.join(format!("handlers/{}.rs", parent_snake));
    let routes_path = base.join(format!("routes/{}.rs", parent_snake));

    if !handlers_path.exists() || !routes_path.exists() {
        println!(
            "  Injected has-many: {} -> {} (via {}) [model only — no handlers/routes file]",
            parent_entity, utils::pluralize(child_entity), fk_column
        );
        return Ok(());
    }

    let handler_code = format!(
        r#"pub async fn {handler_name}(
    State(state): State<AppState>,
    Path(id): Path<Uuid>,
    Query(params): Query<crate::pagination::PageRequest>,
) -> crate::errors::AppResult<crate::api::ApiResponse<Vec<crate::entities::{child_snake}::Model>>> {{
    use crate::api::ok_page;
    use crate::pagination::PageMeta;

    let page = params.page();
    let per_page = params.per_page();
    let paginator = crate::entities::{child_snake}::Entity::find()
        .filter(crate::entities::{child_snake}::Column::{fk_pascal}.eq(id))
        .paginate(&state.db, per_page);
    let total = paginator.num_items().await?;
    let data = paginator.fetch_page(page - 1).await?;
    let meta = PageMeta::from_request(&params, total);
    Ok(ok_page(data, meta))
}}"#,
        handler_name = handler_name,
        child_snake = child_snake,
        fk_pascal = fk_pascal,
    );
    utils::insert_at_marker(
        &handlers_path,
        markers::RELATION_HANDLERS,
        &handler_code,
    )?;

    // 3. Inject route into parent routes
    let parent_plural = utils::pluralize(&parent_snake);

    // URL path uses the same disambiguation as handler name
    let url_suffix = if fk_base == parent_snake {
        child_plural.clone()
    } else {
        format!("{}-by-{}", child_plural, fk_base.replace('_', "-"))
    };

    let route_line = format!(
        "        .route(\"{}/{}/{{id}}/{}\", get({}::{}))",
        api_prefix, parent_plural, url_suffix, parent_snake, handler_name
    );
    utils::insert_at_marker(
        &routes_path,
        markers::RELATION_ROUTES,
        &route_line,
    )?;

    println!(
        "  Injected has-many: {} -> {} (via {})",
        parent_entity, utils::pluralize(child_entity), fk_column
    );
    Ok(())
}

/// Build a seed function string for the given entity.
///
/// Generates a `seed_{entity}s()` async function that uses the `fake` crate
/// to insert randomised rows, plus a call line to invoke it from `run()`.
/// Both are inserted before the `ROMANCE:SEEDS` marker in `seed.rs`.
fn build_seed_function(entity: &EntityDefinition) -> String {
    let snake = entity.name.to_snake_case();

    // Build field assignment lines. We skip:
    //  - fields with a relation (FK fields) — handled by ..Default::default()
    //  - optional fields — default to None via ..Default::default()
    let mut field_lines: Vec<String> = Vec::new();
    for f in &entity.fields {
        if f.relation.is_some() || f.optional {
            continue;
        }
        let faker_expr = field_type_to_faker(&f.field_type);
        field_lines.push(format!("            {}: Set({}),", utils::rust_ident(&f.name), faker_expr));
    }

    let fields_block = field_lines.join("\n");

    // The function itself + the call that goes into run().
    // We put the call first so that when inserted before the marker the
    // function definition sits above the call (insert_at_marker prepends).
    format!(
        r#"pub async fn seed_{snake}s(db: &DatabaseConnection, count: usize) -> Result<()> {{
    use fake::Fake;
    use fake::faker::lorem::en::*;
    use fake::faker::name::en::*;
    use fake::faker::internet::en::*;
    use crate::entities::{snake}::ActiveModel;
    use sea_orm::Set;

    for _ in 0..count {{
        let model = ActiveModel {{
            id: Set(uuid::Uuid::new_v4()),
{fields_block}
            created_at: Set(chrono::Utc::now().fixed_offset()),
            updated_at: Set(chrono::Utc::now().fixed_offset()),
            ..Default::default()
        }};
        model.insert(db).await?;
    }}
    tracing::info!("Seeded {{}} {snake}s", count);
    Ok(())
}}

seed_{snake}s(db, 10).await?;"#,
        snake = snake,
        fields_block = fields_block,
    )
}

/// Map a `FieldType` to a faker/random expression used inside seed functions.
fn field_type_to_faker(ft: &FieldType) -> String {
    match ft {
        FieldType::String | FieldType::Enum(_) | FieldType::File | FieldType::Image => {
            "Sentence(3..8).fake::<String>()".to_string()
        }
        FieldType::Text => "Paragraph(1..3).fake::<String>()".to_string(),
        FieldType::Bool => "rand::random::<bool>()".to_string(),
        FieldType::Int32 => "(1..1000i32).fake::<i32>()".to_string(),
        FieldType::Int64 => "(1..10000i64).fake::<i64>()".to_string(),
        FieldType::Float64 => "rand::random::<f64>() * 100.0".to_string(),
        FieldType::Decimal => "rust_decimal::Decimal::new((1..10000).fake::<i64>(), 2)".to_string(),
        FieldType::Uuid => "uuid::Uuid::new_v4()".to_string(),
        FieldType::DateTime => "chrono::Utc::now().fixed_offset()".to_string(),
        FieldType::Date => "chrono::Utc::now().date_naive()".to_string(),
        FieldType::Json => "serde_json::json!({{}})".to_string(),
    }
}

fn build_context(entity: &EntityDefinition) -> Context {
    let mut ctx = Context::new();
    ctx.insert("entity_name", &entity.name);
    ctx.insert("entity_name_snake", &entity.name.to_snake_case());

    let features = ProjectFeatures::load(Path::new("."));
    ctx.insert("api_prefix", &features.api_prefix);
    ctx.insert("soft_delete", &features.soft_delete);
    ctx.insert("has_validation", &features.has_validation);
    ctx.insert("has_search", &features.has_search);
    ctx.insert("has_audit", &features.has_audit);
    ctx.insert("has_auth", &features.has_auth);
    ctx.insert("has_multitenancy", &features.has_multitenancy);

    let has_searchable_fields = entity.fields.iter().any(|f| f.searchable);
    ctx.insert("has_searchable_fields", &has_searchable_fields);

    // Track which target entities we've seen for deduplicating Related impls
    let mut seen_targets: std::collections::HashSet<String> = std::collections::HashSet::new();

    let fields: Vec<serde_json::Value> = entity
        .fields
        .iter()
        .map(|f| {
            let validations = context::validation_rules_to_json(&f.validations);
            let has_validations = !f.validations.is_empty();

            let visibility_str = match &f.visibility {
                FieldVisibility::Public => "public",
                FieldVisibility::Authenticated => "authenticated",
                FieldVisibility::AdminOnly => "admin_only",
                FieldVisibility::Roles(_) => "roles",
            };
            let visibility_roles: Vec<String> = match &f.visibility {
                FieldVisibility::Roles(r) => r.clone(),
                _ => vec![],
            };

            // Compute unique Relation variant name from FK field name
            // e.g., sender_id -> Sender, author_id -> Author, user_id -> User
            let relation_variant = if f.relation.is_some() {
                let base = f.name.strip_suffix("_id").unwrap_or(&f.name);
                base.to_pascal_case()
            } else {
                String::new()
            };

            // Only generate Related<target::Entity> impl for first FK to each target
            let is_first = if let Some(ref target) = f.relation {
                seen_targets.insert(target.clone())
            } else {
                false
            };

            serde_json::json!({
                "name": f.name,
                "rust_name": utils::rust_ident(&f.name),
                "rust_type": f.field_type.to_rust(),
                "postgres_type": f.field_type.to_postgres(),
                "sea_orm_column": f.field_type.to_sea_orm_column(),
                "optional": f.optional,
                "relation": f.relation,
                "relation_variant": relation_variant,
                "is_first_relation_to_target": is_first,
                "validations": validations,
                "has_validations": has_validations,
                "is_numeric": context::is_numeric(&f.field_type),
                "searchable": f.searchable,
                "is_file": matches!(f.field_type, FieldType::File),
                "is_image": matches!(f.field_type, FieldType::Image),
                "filter_method": context::filter_method(&f.field_type),
                "visibility": visibility_str,
                "visibility_roles": visibility_roles,
            })
        })
        .collect();

    ctx.insert("fields", &fields);

    // Track if any field has restricted visibility (for conditional filter_for_role method)
    let has_restricted_fields = entity.fields.iter().any(|f| f.visibility != FieldVisibility::Public);
    ctx.insert("has_restricted_fields", &has_restricted_fields);

    // Track if any field has validation rules (for conditional imports/derives)
    let has_any_validations = entity.fields.iter().any(|f| !f.validations.is_empty());
    ctx.insert("has_any_validations", &has_any_validations);

    // Track if any field has a regex validation (for lazy_static / once_cell statics)
    let has_regex_validations = entity.fields.iter().any(|f| {
        f.validations
            .iter()
            .any(|v| matches!(v, ValidationRule::Regex(_)))
    });
    ctx.insert("has_regex_validations", &has_regex_validations);

    // Build belongs_to_relations array for nested/related serialization (?include= support).
    // Each entry provides the target entity name, snake_case name, and the FK field name
    // so that templates can generate DetailResponse structs and eager-fetch handlers.
    let belongs_to_relations: Vec<serde_json::Value> = entity
        .fields
        .iter()
        .filter(|f| f.relation.is_some())
        .map(|f| {
            let target = f.relation.as_ref().unwrap();
            // Use FK base name for the detail field (e.g., creator_id -> creator)
            // This avoids duplicate fields when multiple FKs point to the same entity
            let fk_base = f.name.strip_suffix("_id").unwrap_or(&f.name);
            serde_json::json!({
                "target": target,
                "target_snake": target.to_snake_case(),
                "detail_field": fk_base.to_snake_case(),
                "fk_field": f.name,
                "fk_rust_name": utils::rust_ident(&f.name),
                "optional": f.optional,
            })
        })
        .collect();
    let has_belongs_to = !belongs_to_relations.is_empty();
    ctx.insert("belongs_to_relations", &belongs_to_relations);
    ctx.insert("has_belongs_to", &has_belongs_to);

    ctx
}