runique 2.2.0

A Django-inspired web framework for Rust with ORM, templates, and comprehensive security middleware
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
//! AST Parser for the builder DSL (`ModelSchema`) — extracts the `ParsedSchema` from Rust source code.
//! Supports two syntaxes:
//!   v1: `fields: { name: String [required], ... }`
//!   v2: `{ name: text [required], ... }` (anonymous block, semantic types)
use crate::utils::trad::tf;
use syn::{
    Ident, LitStr, Token, braced, bracketed, parenthesized,
    parse::{Parse, ParseStream},
    visit::Visit,
};

use crate::migration::utils::types::{ParsedColumn, ParsedFk, ParsedIndex, ParsedSchema};

// ── Lightweight parsing structures ────────────────────────────────────────────

struct DslModel {
    name: String,
    table: String,
    pk: DslPk,
    enum_types: Vec<(String, String, Vec<String>)>, // (enum_name, backing_type, string_values)
    fields: Vec<DslField>,
    relations: Vec<DslRelation>,
    unique_together: Vec<Vec<String>>,
    indexes: Vec<Vec<String>>,
}

struct DslPk {
    name: String,
    ty: String, // "i32", "i64", "uuid", "Pk"
}

struct DslField {
    name: String,
    ty: String,
    enum_name: Option<String>, // from type (v1: `enum(X)`) or attrs (v2: `choice [enum(X)]`)
    options: Vec<String>,
    default_value: Option<String>, // literal `[default: X]` value (rendered for `.default(...)`)
    renamed_from: Option<String>,  // `[renamed_from: "old"]` → RENAME COLUMN instead of DROP+ADD
}

/// Concatenates every remaining token in `buf` (used for `default(...)` paren values).
fn capture_tokens_string(buf: &syn::parse::ParseBuffer) -> String {
    let mut s = String::new();
    while !buf.is_empty() {
        match buf.parse::<proc_macro2::TokenTree>() {
            Ok(tt) => s.push_str(&tt.to_string()),
            Err(_) => break,
        }
    }
    s
}

/// Concatenates tokens in `buf` up to the next comma (used for `key: value` attr values).
fn capture_value_until_comma(buf: &syn::parse::ParseBuffer) -> String {
    let mut s = String::new();
    while !buf.is_empty() && !buf.peek(Token![,]) {
        match buf.parse::<proc_macro2::TokenTree>() {
            Ok(tt) => s.push_str(&tt.to_string()),
            Err(_) => break,
        }
    }
    s
}

enum DslRelationKind {
    BelongsTo {
        from_column: String,
        on_delete: String,
        on_update: String,
    },
    HasMany,
    HasOne,
    ManyToMany {
        #[allow(dead_code)]
        via: String,
    },
}

struct DslRelation {
    kind: DslRelationKind,
    target: String,
}

// ── Parse impls ───────────────────────────────────────────────────────────────

impl Parse for DslModel {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        // ModelName,
        let name: Ident = input.parse()?;
        input.parse::<Token![,]>()?;

        // table: "...",
        let kw: Ident = input.parse()?;
        if kw != "table" {
            return Err(syn::Error::new(kw.span(), "expected 'table'"));
        }
        input.parse::<Token![:]>()?;
        let table: LitStr = input.parse()?;
        input.parse::<Token![,]>()?;

        // pk: name => type,
        let kw: Ident = input.parse()?;
        if kw != "pk" {
            return Err(syn::Error::new(kw.span(), "expected 'pk'"));
        }
        input.parse::<Token![:]>()?;
        let pk_name: Ident = input.parse()?;
        input.parse::<Token![=>]>()?;
        let pk_ty: Ident = input.parse()?;
        input.parse::<Token![,]>()?;

        // enums: { ... } optional
        let mut enum_types: Vec<(String, String, Vec<String>)> = Vec::new();
        if input.peek(Ident) {
            let peek: Ident = input.fork().parse()?;
            if peek == "enums" {
                input.parse::<Ident>()?;
                input.parse::<Token![:]>()?;
                let enum_block;
                braced!(enum_block in input);
                while !enum_block.is_empty() {
                    if let Ok(enum_name) = enum_block.parse::<Ident>() {
                        let _ = enum_block.parse::<Token![:]>();
                        // optional type: i32 | i64 (String and pg are obsolete but tolerated here)
                        let backing = if enum_block.peek(Ident) {
                            let ty: Ident = enum_block.fork().parse().unwrap();
                            match ty.to_string().as_str() {
                                "String" | "i32" | "i64" | "pg" => {
                                    enum_block.parse::<Ident>().ok();
                                    ty.to_string()
                                }
                                _ => "Auto".to_string(),
                            }
                        } else {
                            "Auto".to_string()
                        };
                        // [Name] or [Name="value", ...] variants
                        let mut string_values: Vec<String> = Vec::new();
                        if enum_block.peek(syn::token::Bracket) {
                            let variants;
                            bracketed!(variants in enum_block);
                            while !variants.is_empty() {
                                if let Ok(vname) = variants.parse::<Ident>() {
                                    if variants.peek(Token![=]) {
                                        let _ = variants.parse::<Token![=]>();
                                        if let Ok(lit) = variants.parse::<syn::Lit>() {
                                            match lit {
                                                syn::Lit::Str(s) => string_values.push(s.value()),
                                                syn::Lit::Int(n) => {
                                                    string_values.push(n.to_string())
                                                }
                                                _ => string_values.push(vname.to_string()),
                                            }
                                        } else if variants.peek(syn::token::Paren) {
                                            // tuple syntax: ("db_value", "Display") — only keep first value
                                            let tuple;
                                            parenthesized!(tuple in variants);
                                            if let Ok(syn::Lit::Str(s)) = tuple.parse::<syn::Lit>()
                                            {
                                                string_values.push(s.value());
                                            } else {
                                                string_values.push(vname.to_string());
                                            }
                                            // consume the rest of the tuple (display label, etc.)
                                            while !tuple.is_empty() {
                                                tuple.parse::<proc_macro2::TokenTree>().ok();
                                            }
                                        } else {
                                            string_values.push(vname.to_string());
                                        }
                                    } else {
                                        string_values.push(vname.to_string());
                                    }
                                } else {
                                    variants.parse::<proc_macro2::TokenTree>().ok();
                                }
                                let _ = variants.parse::<Token![,]>();
                            }
                        }
                        enum_types.push((enum_name.to_string(), backing, string_values));
                        let _ = enum_block.parse::<Token![,]>();
                    } else {
                        enum_block.parse::<proc_macro2::TokenTree>().ok();
                    }
                }
                let _ = input.parse::<Token![,]>();
            }
        }

        // Anonymous `{ ... }` block — semantic types, SQL derived from them.
        let mut fields = Vec::new();
        let fields_content;
        braced!(fields_content in input);
        while !fields_content.is_empty() {
            fields.push(DslField::parse(&fields_content)?);
        }

        // optional blocks: relations, meta, form_fields (ignored), etc.
        let mut relations = Vec::new();
        let mut unique_together: Vec<Vec<String>> = Vec::new();
        let mut indexes: Vec<Vec<String>> = Vec::new();
        while !input.is_empty() {
            let _ = input.parse::<Token![,]>();
            if input.is_empty() {
                break;
            }
            if !input.peek(Ident) {
                input.parse::<proc_macro2::TokenTree>().ok();
                continue;
            }
            let kw: Ident = input.parse()?;
            input.parse::<Token![:]>()?;

            let block_content;
            braced!(block_content in input);

            match kw.to_string().as_str() {
                "relations" => {
                    while !block_content.is_empty() {
                        if let Ok(rel) = DslRelation::parse(&block_content) {
                            relations.push(rel);
                        } else {
                            while !block_content.is_empty() && !block_content.peek(Token![,]) {
                                block_content.parse::<proc_macro2::TokenTree>().ok();
                            }
                            let _ = block_content.parse::<Token![,]>();
                        }
                    }
                }
                "meta" => {
                    while !block_content.is_empty() {
                        let Ok(meta_key): syn::Result<Ident> = block_content.parse() else {
                            block_content.parse::<proc_macro2::TokenTree>().ok();
                            continue;
                        };
                        let _ = block_content.parse::<Token![:]>();
                        match meta_key.to_string().as_str() {
                            "unique_together" | "indexes" => {
                                let list_content;
                                bracketed!(list_content in block_content);
                                let mut groups: Vec<Vec<String>> = Vec::new();
                                while !list_content.is_empty() {
                                    let tuple_content;
                                    syn::parenthesized!(tuple_content in list_content);
                                    let mut group = Vec::new();
                                    while !tuple_content.is_empty() {
                                        if let Ok(f) = tuple_content.parse::<Ident>() {
                                            group.push(f.to_string());
                                        }
                                        let _ = tuple_content.parse::<Token![,]>();
                                    }
                                    if !group.is_empty() {
                                        groups.push(group);
                                    }
                                    let _ = list_content.parse::<Token![,]>();
                                }
                                if meta_key == "unique_together" {
                                    unique_together.extend(groups);
                                } else {
                                    indexes.extend(groups);
                                }
                            }
                            _ => {
                                // ordering, verbose_name, abstract, etc. — value is one TokenTree
                                block_content.parse::<proc_macro2::TokenTree>().ok();
                            }
                        }
                        let _ = block_content.parse::<Token![,]>();
                    }
                }
                _ => {
                    // form_fields, etc. — ignored
                    while !block_content.is_empty() {
                        block_content.parse::<proc_macro2::TokenTree>().ok();
                    }
                }
            }
        }

        Ok(DslModel {
            name: name.to_string(),
            table: table.value(),
            pk: DslPk {
                name: pk_name.to_string(),
                ty: pk_ty.to_string(),
            },
            enum_types,
            fields,
            relations,
            unique_together,
            indexes,
        })
    }
}

impl Parse for DslField {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        // name: type [opt1, opt2, ...],
        let name: Ident = input.parse()?;
        input.parse::<Token![:]>()?;

        // `enum` is a Rust keyword — separate handling (v1: `enum(X)` in type position)
        let (ty, mut enum_name) = if input.peek(Token![enum]) {
            input.parse::<Token![enum]>()?;
            let inner;
            syn::parenthesized!(inner in input);
            let ename: Ident = inner.parse()?;
            ("enum".to_string(), Some(ename.to_string()))
        } else {
            let ty: Ident = input.parse()?;
            // Consume optional (…) qualifier — e.g. `decimal(10, 2)` or `varchar(255)`
            if input.peek(syn::token::Paren) {
                let inner;
                syn::parenthesized!(inner in input);
                while !inner.is_empty() {
                    inner.parse::<proc_macro2::TokenTree>().ok();
                }
            }
            (ty.to_string(), None)
        };

        let mut options = Vec::new();
        let mut default_value: Option<String> = None;
        let mut renamed_from: Option<String> = None;
        if input.peek(syn::token::Bracket) {
            let opts;
            bracketed!(opts in input);
            while !opts.is_empty() {
                // v2: `enum(X)` in attr position (choice/radio)
                if opts.peek(Token![enum]) {
                    opts.parse::<Token![enum]>()?;
                    let inner;
                    syn::parenthesized!(inner in opts);
                    let ename: Ident = inner.parse()?;
                    enum_name = Some(ename.to_string());
                    let _ = opts.parse::<Token![,]>();
                    continue;
                }

                let opt: Ident = opts.parse()?;
                let opt_str = opt.to_string();
                options.push(opt_str.clone());

                // paren syntax (v1): max_len(150), default(0)
                if opts.peek(syn::token::Paren) {
                    let inner;
                    syn::parenthesized!(inner in opts);
                    let captured = capture_tokens_string(&inner);
                    if opt_str == "default" {
                        default_value = Some(captured);
                    } else if opt_str == "renamed_from" {
                        renamed_from = Some(captured.trim_matches('"').to_string());
                    }
                }
                // colon syntax (v2): max_length: 150, rows: 6, upload_to: "path", default: 0
                else if opts.peek(Token![:]) {
                    opts.parse::<Token![:]>()?;
                    let captured = capture_value_until_comma(&opts);
                    if opt_str == "default" {
                        default_value = Some(captured);
                    } else if opt_str == "renamed_from" {
                        renamed_from = Some(captured.trim_matches('"').to_string());
                    }
                }

                let _ = opts.parse::<Token![,]>();
            }
        }

        let _ = input.parse::<Token![,]>();
        Ok(DslField {
            name: name.to_string(),
            ty,
            enum_name,
            options,
            default_value,
            renamed_from,
        })
    }
}

impl Parse for DslRelation {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let kind_kw: Ident = input.parse()?;
        input.parse::<Token![:]>()?;
        let target: Ident = input.parse()?;

        let rel = match kind_kw.to_string().as_str() {
            "belongs_to" => {
                let via_kw: Ident = input.parse()?;
                if via_kw != "via" {
                    return Err(syn::Error::new(
                        via_kw.span(),
                        "expected 'via' after target model",
                    ));
                }
                let from_col: Ident = input.parse()?;

                let mut on_delete = "NoAction".to_string();
                let mut on_update = "NoAction".to_string();
                if input.peek(syn::token::Bracket) {
                    let opts;
                    bracketed!(opts in input);
                    let mut i: i32 = 0;
                    while !opts.is_empty() {
                        let opt: Ident = opts.parse()?;
                        let val = normalize_fk_action(&opt.to_string());
                        if i == 0 {
                            on_delete = val;
                        } else {
                            on_update = val;
                        }
                        i = i.saturating_add(1);
                        let _ = opts.parse::<Token![,]>();
                    }
                }
                let _ = input.parse::<Token![,]>();
                DslRelation {
                    kind: DslRelationKind::BelongsTo {
                        from_column: from_col.to_string(),
                        on_delete,
                        on_update,
                    },
                    target: target.to_string(),
                }
            }
            "has_many" => {
                let _ = input.parse::<Token![,]>();
                DslRelation {
                    kind: DslRelationKind::HasMany,
                    target: target.to_string(),
                }
            }
            "has_one" => {
                let _ = input.parse::<Token![,]>();
                DslRelation {
                    kind: DslRelationKind::HasOne,
                    target: target.to_string(),
                }
            }
            "many_to_many" => {
                let via_kw: Ident = input.parse()?;
                if via_kw != "via" {
                    return Err(syn::Error::new(
                        via_kw.span(),
                        "expected 'via' after target model",
                    ));
                }
                let via: Ident = input.parse()?;
                let _ = input.parse::<Token![,]>();
                DslRelation {
                    kind: DslRelationKind::ManyToMany {
                        via: via.to_string(),
                    },
                    target: target.to_string(),
                }
            }
            _ => {
                while !input.is_empty() && !input.peek(Token![,]) {
                    input.parse::<proc_macro2::TokenTree>().ok();
                }
                let _ = input.parse::<Token![,]>();
                DslRelation {
                    kind: DslRelationKind::HasMany,
                    target: target.to_string(),
                }
            }
        };
        Ok(rel)
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn normalize_fk_action(s: &str) -> String {
    match s.to_lowercase().as_str() {
        "cascade" => "Cascade".to_string(),
        "restrict" => "Restrict".to_string(),
        "set_null" | "setnull" => "SetNull".to_string(),
        "set_default" | "setdefault" => "SetDefault".to_string(),
        _ => "NoAction".to_string(),
    }
}

/// PascalCase → snake_case to derive target table name from model
fn pascal_to_snake(s: &str) -> String {
    let mut result = String::new();
    for (i, ch) in s.chars().enumerate() {
        if ch.is_uppercase() && i > 0 {
            result.push('_');
        }
        result.push(ch.to_lowercase().next().unwrap());
    }
    result
}

// ── Type Mapping ──────────────────────────────────────────────────────────

/// Converts a DSL type (v1 SQL or v2 semantic) to a SeaORM type name — the single
/// mapping table shared by both the `pk:` position and ordinary fields.
/// `default` covers a type name this table doesn't recognize; the two call
/// sites below need different ones (a `pk:` position with no clear type still
/// wants an auto-increment `Integer`, an ordinary unknown field is safest as
/// `String`), but every OTHER type name must resolve identically everywhere —
/// two separate match arms for the same table is exactly how a `Pk`-typed FK
/// field ended up correctly resolved in the pk-position table but silently
/// misclassified as `String` in the field one (fixed 2026-09-01, then
/// collapsed here so the two can no longer drift apart again).
fn dsl_type_to_col_type(ty: &str, default: &str) -> String {
    match ty {
        // v1 SQL
        "String" | "char" | "varchar" => "String".to_string(),
        // "text" in v2 = short text field (VARCHAR) — same behavior as String in v1
        "text" => "String".to_string(),
        "i8" => "TinyInteger".to_string(),
        "i16" => "SmallInteger".to_string(),
        "i32" | "integer" => "Integer".to_string(),
        "i64" | "big_integer" | "bigint" => "BigInteger".to_string(),
        "u32" => "Unsigned".to_string(),
        "u64" => "BigUnsigned".to_string(),
        "f32" => "Float".to_string(),
        "f64" | "float" | "percent" => "Double".to_string(),
        "decimal" => "Decimal".to_string(),
        "bool" => "Boolean".to_string(),
        "date" => "Date".to_string(),
        "time" => "Time".to_string(),
        "datetime" | "timestamp" => "DateTime".to_string(),
        "timestamp_tz" => "TimestampWithTimeZone".to_string(),
        "uuid" => "Uuid".to_string(),
        "json" | "json_binary" => "Json".to_string(),
        "binary" | "blob" => "Binary".to_string(),
        "var_binary" => "VarBinary".to_string(),
        "inet" | "cidr" | "mac_address" | "interval" | "ip" => "String".to_string(),
        // v2 semantic text → String (VARCHAR) or Text
        "email" | "url" | "password" | "slug" | "color" | "phone" => "String".to_string(),
        "richtext" | "textarea" => "Text".to_string(),
        // v2 files → String (JSON path)
        "image" | "document" | "file" => "String".to_string(),
        // v2 choice/radio → resolved via enum_ref in dsl_to_parsed_schema, fallback String
        "choice" | "radio" => "String".to_string(),
        // explicit int
        "int" => "Integer".to_string(),
        // `Pk` usable on any field, not just `pk:` (e.g. an FK typed `Pk` to stay in
        // sync with the referenced table's PK type under big-pk/pk-uuid).
        "Pk" => pk_alias_col_type(),
        _ => {
            eprintln!("{}", tf("makemigrations.unknown_dsl_type", &[ty, default]));
            default.to_string()
        }
    }
}

fn dsl_field_type_to_col_type(ty: &str) -> String {
    dsl_type_to_col_type(ty, "String")
}

fn dsl_pk_to_col_type(ty: &str) -> String {
    dsl_type_to_col_type(ty, "Integer")
}

/// Column type for the generic `Pk` DSL keyword — follows the same global alias as
/// `runique::utils::config::Pk`, so the migration SQL matches the ORM's actual column type.
#[cfg(feature = "pk-uuid")]
fn pk_alias_col_type() -> String {
    "Uuid".to_string()
}
#[cfg(all(feature = "big-pk", not(feature = "pk-uuid")))]
fn pk_alias_col_type() -> String {
    "BigInteger".to_string()
}
#[cfg(not(any(feature = "big-pk", feature = "pk-uuid")))]
fn pk_alias_col_type() -> String {
    "Integer".to_string()
}

// ── Conversion to ParsedSchema ──────────────────────────────────────────────

fn dsl_to_parsed_schema(model: DslModel) -> ParsedSchema {
    let primary_key = Some(ParsedColumn {
        name: model.pk.name,
        col_type: dsl_pk_to_col_type(&model.pk.ty),
        nullable: false,
        unique: false,
        ignored: false,
        created_at: false,
        updated_at: false,
        has_default_now: false,
        default_value: None,
        enum_name: None,
        enum_string_values: Vec::new(),
        enum_is_pg: false,
        renamed_from: None,
    });

    let enum_types = model.enum_types;

    let columns = model
        .fields
        .into_iter()
        .map(|f| {
            let has_auto_now = f.options.contains(&"auto_now".to_string());
            let has_auto_now_update = f.options.contains(&"auto_now_update".to_string());
            let has_required = f.options.contains(&"required".to_string());
            let has_nullable = f.options.contains(&"nullable".to_string());
            let is_created_at = f.name == "created_at";
            let is_updated_at = f.name == "updated_at";

            // Semantic v2 types (lowercase): lack of `required` → nullable by default.
            // v1 types (SQL / Rust: String, i32...): only explicit `[nullable]` makes it nullable.
            // auto_now / auto_now_update → never nullable in both cases.
            const V2_TYPES: &[&str] = &[
                "text",
                "email",
                "password",
                "richtext",
                "textarea",
                "url",
                "int",
                "bool",
                "boolean",
                "float",
                "decimal",
                "percent",
                "date",
                "time",
                "datetime",
                "timestamp",
                "timestamp_tz",
                "image",
                "document",
                "file",
                "color",
                "slug",
                "uuid",
                "json",
                "json_binary",
                "ip",
                "choice",
                "radio",
                "bigint",
                "binary",
                "blob",
                "inet",
                "cidr",
                "mac_address",
                "interval",
                "phone",
            ];
            let is_v2 = V2_TYPES.contains(&f.ty.as_str());
            let nullable = if has_auto_now || has_auto_now_update || has_required {
                false
            } else if has_nullable {
                true
            } else {
                is_v2 // v2 without required → nullable ; v1 without explicit nullable → not nullable
            };

            let unique = f.options.contains(&"unique".to_string());

            // Resolution of the associated enum (v1: ty=="enum", v2: ty=="choice"/"radio")
            let is_enum_field = f.ty == "enum" || f.ty == "choice" || f.ty == "radio";
            let enum_entry = if is_enum_field {
                f.enum_name
                    .as_deref()
                    .and_then(|n| enum_types.iter().find(|(name, _, _)| name == n))
            } else {
                None
            };

            let col_type = if has_auto_now || has_auto_now_update {
                "DateTime".to_string()
            } else if is_enum_field {
                match enum_entry.map(|(_, bt, _)| bt.as_str()).unwrap_or("Auto") {
                    "i32" => "Integer".to_string(),
                    "i64" => "BigInteger".to_string(),
                    _ => "String".to_string(), // Auto / String → VARCHAR
                }
            } else {
                dsl_field_type_to_col_type(&f.ty)
            };

            // Enum string values for diff (only string-backed enums)
            let (enum_name, enum_string_values, enum_is_pg) = if is_enum_field {
                match enum_entry {
                    Some((name, backing, values)) if backing != "i32" && backing != "i64" => {
                        // Auto → `enum_is_pg = false`, the generator decides via DbKind
                        (Some(name.clone()), values.clone(), false)
                    }
                    _ => (None, Vec::new(), false),
                }
            } else {
                (None, Vec::new(), false)
            };

            let ignored = f.options.contains(&"readonly".to_string()) || f.name == "cache_key";

            ParsedColumn {
                name: f.name,
                col_type,
                nullable,
                unique,
                ignored,
                created_at: has_auto_now || is_created_at,
                updated_at: has_auto_now_update || is_updated_at,
                has_default_now: has_auto_now
                    || has_auto_now_update
                    || is_created_at
                    || is_updated_at,
                default_value: f.default_value,
                enum_name,
                enum_string_values,
                enum_is_pg,
                renamed_from: f.renamed_from,
            }
        })
        .collect();

    let foreign_keys = model
        .relations
        .iter()
        .filter_map(|rel| {
            if let DslRelationKind::BelongsTo {
                from_column,
                on_delete,
                on_update,
            } = &rel.kind
            {
                Some(ParsedFk {
                    from_column: from_column.clone(),
                    to_table: pascal_to_snake(&rel.target),
                    to_column: "id".to_string(),
                    on_delete: on_delete.clone(),
                    on_update: on_update.clone(),
                })
            } else {
                None
            }
        })
        .collect();

    let table = model.table.clone();

    // unique_together → unique indexes
    let mut parsed_indexes: Vec<ParsedIndex> = model
        .unique_together
        .iter()
        .map(|cols| ParsedIndex {
            name: format!("{}_{}_uniq", table, cols.join("_")),
            columns: cols.clone(),
            unique: true,
        })
        .collect();

    // indexes → non-unique indexes
    for cols in &model.indexes {
        parsed_indexes.push(ParsedIndex {
            name: format!("idx_{}_{}", table, cols.join("_")),
            columns: cols.clone(),
            unique: false,
        });
    }

    ParsedSchema {
        table_name: table,
        primary_key,
        columns,
        foreign_keys,
        indexes: parsed_indexes,
    }
}

// ── Visitor ──────────────────────────────────────────────────────────────────

struct DslVisitor {
    pub schema: Option<ParsedSchema>,
    pub model_name: Option<String>,
}

impl DslVisitor {
    fn new() -> Self {
        Self {
            schema: None,
            model_name: None,
        }
    }
}

impl<'ast> Visit<'ast> for DslVisitor {
    fn visit_macro(&mut self, mac: &'ast syn::Macro) {
        if self.schema.is_some() {
            return;
        }
        let is_model = mac
            .path
            .segments
            .last()
            .map(|s| s.ident == "model")
            .unwrap_or(false);

        if is_model && let Ok(model) = syn::parse2::<DslModel>(mac.tokens.clone()) {
            self.model_name = Some(pascal_to_snake(&model.name));
            self.schema = Some(dsl_to_parsed_schema(model));
        }
        syn::visit::visit_macro(self, mac);
    }
}

// ── Public entry point ─────────────────────────────────────────────────────

pub fn parse_schema_from_source(source: &str) -> Option<(String, ParsedSchema)> {
    let file = syn::parse_str::<syn::File>(source).ok()?;
    let mut visitor = DslVisitor::new();
    visitor.visit_file(&file);
    let schema = visitor.schema?;
    let model_name = visitor.model_name.unwrap_or_default();
    Some((model_name, schema))
}