forgedb-parser 0.2.1

Parser for ForgeDB schema language
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
/// Abstract Syntax Tree representation

use forgedb_validation::Position;

/// Constraint parameter value
#[derive(Debug, Clone, PartialEq)]
pub enum ConstraintParam {
    Number(i64),
    String(String),
}

/// Constraint directive (e.g., @min(10), @email)
#[derive(Debug, Clone, PartialEq)]
pub struct Constraint {
    pub name: String,
    pub params: Vec<ConstraintParam>,
}

impl Constraint {
    pub fn new(name: String) -> Self {
        Constraint {
            name,
            params: Vec::new(),
        }
    }

    pub fn with_param(mut self, param: ConstraintParam) -> Self {
        self.params.push(param);
        self
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum IndexType {
    Hash,  // Default for exact matches, unordered types
    BTree, // For range queries on ordered types
}

#[derive(Debug, Clone, PartialEq)]
pub struct CompositeIndex {
    pub fields: Vec<String>,
}

/// A model-level `@projection(<name>: <field>, ...)` directive (#113): a named,
/// compile-time-known subset of a model's stored columns.  Generates a tailored
/// projection struct + narrow read that materializes only PK + these fields.
/// The identity column is always materialized regardless of whether it appears
/// in `fields` (validation/codegen enforce this).
#[derive(Debug, Clone, PartialEq)]
pub struct Projection {
    pub name: String,
    pub fields: Vec<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum FieldType {
    U32,
    U64,
    I32,
    I64,
    F64,
    Bool,
    String,
    Uuid,
    Timestamp,
    /// JSON value stored as its serialized bytes in a variable-length column,
    /// typed `serde_json::Value` in generated Rust (#json). Rides the same
    /// variable-column storage path as `String`.
    Json,
    /// Exact fixed-point decimal (money/quantity). Typed `rust_decimal::Decimal`
    /// in generated Rust; stored in a FIXED 16-byte column (via `Decimal::serialize`),
    /// exactly like `Uuid`. Serialized as a JSON string to preserve precision.
    /// `Ord`+`Hash`, so it is filterable/sortable/indexable (index key normalized
    /// to be scale-invariant). Bare `decimal` only — `decimal(p, s)` is deferred.
    Decimal,
    /// A reference to a user-declared top-level `enum Name { ... }` by its bare
    /// PascalCase name (#enum). Typed as the generated Rust enum in `database.rs`,
    /// serialized as the variant NAME string (so REST/TS/JSON all agree). Stored in
    /// a FIXED 1-byte `u8` discriminant column (variants map to `0..N` in declaration
    /// order). `Ord`+`Hash`, so it is filterable/sortable/indexable (index key = the
    /// variant name string). A bare PascalCase identifier that is NOT a declared enum
    /// stays a `StructType` and is caught by struct-reference validation.
    Enum(String),
    // Fixed-size types (Sprint 8)
    Char(usize),                       // Fixed-size character array: char(N)
    FixedArray(Box<FieldType>, usize), // Fixed array: [type; count]
    StructType(String),                // Reference to a struct by name
    OptionalStructType(String),        // Optional struct reference
    /// Nullable wrapper for primitive/scalar types (e.g. `age: ?i32` or `age: i32?`)
    Nullable(Box<FieldType>),
    // Relations
    Relation(RelationType),
    // Component references (Sprint 17)
    Component(ComponentReference),
}

/// Component protocol (Sprint 17)
#[derive(Debug, Clone, PartialEq)]
pub enum ComponentProtocol {
    Tsx,  // tsx://
    Jsx,  // jsx://
    Api,  // api://
}

/// Relation inclusion for components (Sprint 17)
#[derive(Debug, Clone, PartialEq)]
pub enum RelationInclusion {
    None,
    All,
    Specific(Vec<String>),
}

/// Component reference (Sprint 17)
#[derive(Debug, Clone, PartialEq)]
pub struct ComponentReference {
    pub protocol: ComponentProtocol,
    pub path: String,
    pub relations: RelationInclusion,
}

#[derive(Debug, Clone, PartialEq)]
pub enum RelationType {
    /// One-to-many: [Post] means this model has many Posts
    OneToMany(String),
    /// Required reference: *User means this model must reference a User
    RequiredReference(String),
    /// Optional reference: ?User means this model optionally references a User
    OptionalReference(String),
    /// Many-to-many: detected from bidirectional OneToMany fields
    ManyToMany(String),
}

#[derive(Debug, Clone, PartialEq)]
pub struct Field {
    pub name: String,
    pub field_type: FieldType,
    pub auto_generate: bool,          // + symbol
    pub unique: bool,                 // & symbol
    pub indexed: bool,                // ^ symbol
    pub constraints: Vec<Constraint>, // @ directives
    pub index_type: IndexType,        // Hash or BTree
    pub is_computed: bool,            // @computed directive
    pub fulltext_indexed: bool,       // @fulltext directive (Sprint 18)
    pub is_materialized: bool,        // @materialized directive (Sprint 19)
    /// Source position of the field name (1-based line/column). `None` when the
    /// node is synthesized rather than parsed (e.g. test fixtures, migrations).
    pub position: Option<Position>,
}

/// Represents a struct definition (Sprint 8)
#[derive(Debug, Clone, PartialEq)]
pub struct Struct {
    pub name: String,
    pub fields: Vec<Field>,
    /// Source position of the struct name (`None` when synthesized).
    pub position: Option<Position>,
}

/// A user-declared top-level `enum Name { V1, V2, ... }` (#enum).  A sibling of
/// `Model`/`Struct`.  Referenced from a field by its bare PascalCase name.
/// Variants are PascalCase, unique, non-empty, and map to `0..N` (the stored
/// `u8` discriminant) in declaration order.
#[derive(Debug, Clone, PartialEq)]
pub struct EnumDef {
    pub name: String,
    pub variants: Vec<String>,
    /// Source position of the enum name (`None` when synthesized).
    pub position: Option<Position>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Model {
    pub name: String,
    pub fields: Vec<Field>,
    pub composite_indexes: Vec<CompositeIndex>,
    pub projections: Vec<Projection>, // @projection(name: a, b) directives (#113)
    pub soft_delete: bool,            // @soft_delete directive at model level (Sprint 19)
    /// Source position of the model name (`None` when synthesized).
    pub position: Option<Position>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Schema {
    pub structs: Vec<Struct>, // Struct definitions (Sprint 8)
    pub enums: Vec<EnumDef>,   // Enum definitions (#enum)
    pub models: Vec<Model>,
}

impl Schema {
    /// Find a model by name
    pub fn find_model(&self, name: &str) -> Option<&Model> {
        self.models.iter().find(|m| m.name == name)
    }

    /// Find a struct by name (Sprint 8)
    pub fn find_struct(&self, name: &str) -> Option<&Struct> {
        self.structs.iter().find(|s| s.name == name)
    }

    /// Find an enum by name (#enum)
    pub fn find_enum(&self, name: &str) -> Option<&EnumDef> {
        self.enums.iter().find(|e| e.name == name)
    }

    // Schema-level semantic validation (relation targets, struct/enum references,
    // fixed-size struct fields, duplicate names, naming conventions) lives in
    // `crate::validate` — the single positioned authority consumed by the parser,
    // the CLI, and the LSP. See that module for the rationale on why it lives in
    // this crate rather than `forgedb-validation`.

    /// Detect one-to-many relationships by finding matching reference and collection fields
    pub fn detect_relations(&self) -> Vec<RelationPair> {
        let mut relations = Vec::new();

        for model in &self.models {
            for field in &model.fields {
                if let FieldType::Relation(RelationType::OneToMany(target_model)) =
                    &field.field_type
                {
                    // Found a one-to-many field, look for corresponding FK in target model
                    if let Some(target) = self.find_model(target_model) {
                        // Find a reference back to this model
                        for target_field in &target.fields {
                            if let FieldType::Relation(rel) = &target_field.field_type {
                                if rel.is_reference() && rel.target_model() == model.name {
                                    relations.push(RelationPair {
                                        parent_model: model.name.clone(),
                                        parent_field: field.name.clone(),
                                        child_model: target.name.clone(),
                                        child_field: target_field.name.clone(),
                                        is_required: matches!(
                                            rel,
                                            RelationType::RequiredReference(_)
                                        ),
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }

        relations
    }

    /// Detect many-to-many relationships by finding bidirectional OneToMany fields
    /// Returns pairs where Model A has [ModelB] and Model B has [ModelA]
    /// Excludes relationships that are already handled by FK references (1:N)
    pub fn detect_many_to_many_relations(&self) -> Vec<ManyToManyRelation> {
        let mut m2m_relations = Vec::new();
        let mut processed_pairs = std::collections::HashSet::new();

        // First, identify all 1:N relationships (OneToMany with matching FK)
        // We need to track specific FIELD pairs, not just model pairs
        let one_to_many_field_pairs: std::collections::HashSet<(String, String, String, String)> =
            self.detect_relations()
                .iter()
                .map(|rel| {
                    // For a 1:N relation, the parent's OneToMany field and child's FK field form a pair
                    // We store both orderings to match against
                    vec![
                        (
                            rel.parent_model.clone(),
                            rel.parent_field.clone(),
                            rel.child_model.clone(),
                            rel.child_field.clone(),
                        ),
                        (
                            rel.child_model.clone(),
                            rel.child_field.clone(),
                            rel.parent_model.clone(),
                            rel.parent_field.clone(),
                        ),
                    ]
                })
                .flatten()
                .collect();

        for model in &self.models {
            for field in &model.fields {
                if let FieldType::Relation(RelationType::OneToMany(target_model)) =
                    &field.field_type
                {
                    // Check if target model has a OneToMany field pointing back to this model
                    if let Some(target) = self.find_model(target_model) {
                        for target_field in &target.fields {
                            if let FieldType::Relation(RelationType::OneToMany(back_ref)) =
                                &target_field.field_type
                            {
                                if back_ref == &model.name {
                                    // Check if this specific FIELD pair is NOT a 1:N relationship (no FK)
                                    // by checking if this field pair is in the one_to_many_field_pairs set
                                    let is_one_to_many = one_to_many_field_pairs.contains(&(
                                        model.name.clone(),
                                        field.name.clone(),
                                        target_model.clone(),
                                        target_field.name.clone(),
                                    ));

                                    if !is_one_to_many {
                                        // Found a true M:N relationship
                                        // Create a consistent ordering to avoid duplicates
                                        let mut models_fields = vec![
                                            (model.name.as_str(), field.name.as_str()),
                                            (target.name.as_str(), target_field.name.as_str()),
                                        ];
                                        models_fields.sort();

                                        let pair_key = format!(
                                            "{}:{}:{}:{}",
                                            models_fields[0].0,
                                            models_fields[0].1,
                                            models_fields[1].0,
                                            models_fields[1].1
                                        );

                                        if !processed_pairs.contains(&pair_key) {
                                            processed_pairs.insert(pair_key);

                                            // Always store with consistent ordering
                                            let (model1, field1, model2, field2) =
                                                if model.name < target.name {
                                                    (
                                                        model.name.clone(),
                                                        field.name.clone(),
                                                        target.name.clone(),
                                                        target_field.name.clone(),
                                                    )
                                                } else {
                                                    (
                                                        target.name.clone(),
                                                        target_field.name.clone(),
                                                        model.name.clone(),
                                                        field.name.clone(),
                                                    )
                                                };

                                            m2m_relations.push(ManyToManyRelation {
                                                model1,
                                                field1,
                                                model2,
                                                field2,
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        m2m_relations
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct RelationPair {
    pub parent_model: String,
    pub parent_field: String,
    pub child_model: String,
    pub child_field: String,
    pub is_required: bool,
}

/// Represents a many-to-many relationship between two models
#[derive(Debug, Clone, PartialEq)]
pub struct ManyToManyRelation {
    pub model1: String,
    pub field1: String,
    pub model2: String,
    pub field2: String,
}

impl FieldType {
    pub fn to_rust_type(&self) -> String {
        match self {
            FieldType::U32 => "u32".to_string(),
            FieldType::U64 => "u64".to_string(),
            FieldType::I32 => "i32".to_string(),
            FieldType::I64 => "i64".to_string(),
            FieldType::F64 => "f64".to_string(),
            FieldType::Bool => "bool".to_string(),
            FieldType::String => "String".to_string(),
            FieldType::Json => "serde_json::Value".to_string(),
            FieldType::Decimal => "rust_decimal::Decimal".to_string(),
            FieldType::Enum(name) => name.clone(),
            FieldType::Uuid => "uuid::Uuid".to_string(),
            FieldType::Timestamp => "i64".to_string(),
            FieldType::Char(size) => format!("[u8; {}]", size),
            FieldType::FixedArray(inner_type, count) => {
                format!("[{}; {}]", inner_type.to_rust_type(), count)
            }
            FieldType::StructType(name) => name.clone(),
            FieldType::OptionalStructType(name) => format!("Option<{}>", name),
            FieldType::Nullable(inner) => format!("Option<{}>", inner.to_rust_type()),
            FieldType::Relation(rel) => match rel {
                RelationType::RequiredReference(model) => {
                    format!("uuid::Uuid /* FK to {} */", model)
                }
                RelationType::OptionalReference(model) => {
                    format!("Option<uuid::Uuid> /* FK to {} */", model)
                }
                RelationType::OneToMany(_) => "/* virtual field - no storage */".to_string(),
                RelationType::ManyToMany(_) => {
                    "/* virtual field - stored in junction table */".to_string()
                }
            },
            FieldType::Component(_) => "/* component reference - no storage */".to_string(),
        }
    }

    pub fn is_auto_incrementable(&self) -> bool {
        matches!(self, FieldType::U32 | FieldType::U64)
    }

    pub fn is_auto_generatable(&self) -> bool {
        matches!(
            self,
            FieldType::U32 | FieldType::U64 | FieldType::Uuid | FieldType::Timestamp
        )
    }

    pub fn is_relation(&self) -> bool {
        matches!(self, FieldType::Relation(_))
    }

    /// Check if this type is fixed-size (Sprint 8)
    pub fn is_fixed_size(&self) -> bool {
        match self {
            FieldType::U32
            | FieldType::U64
            | FieldType::I32
            | FieldType::I64
            | FieldType::F64
            | FieldType::Bool
            | FieldType::Uuid
            | FieldType::Timestamp
            | FieldType::Decimal // exact decimal is a fixed 16-byte column, like Uuid
            | FieldType::Enum(_) // enum is a fixed 1-byte discriminant column
            | FieldType::Char(_) => true,
            FieldType::FixedArray(inner, _) => inner.is_fixed_size(),
            FieldType::StructType(_) => true, // Structs must be fixed-size
            FieldType::OptionalStructType(_) => true, // Optional struct still fixed-size (uses discriminant)
            FieldType::Nullable(inner) => inner.is_fixed_size(),
            FieldType::String => false,
            FieldType::Json => false, // JSON is a variable-length column, like String
            FieldType::Relation(_) => false, // Relations are virtual or variable
            FieldType::Component(_) => false, // Components are virtual
        }
    }

    /// Get struct name if this is a struct type (Sprint 8)
    pub fn struct_name(&self) -> Option<&str> {
        match self {
            FieldType::StructType(name) => Some(name),
            FieldType::OptionalStructType(name) => Some(name),
            _ => None,
        }
    }

    /// Get the enum name if this is (or wraps) an enum reference (#enum).
    pub fn enum_name(&self) -> Option<&str> {
        match self {
            FieldType::Enum(name) => Some(name),
            FieldType::Nullable(inner) => inner.enum_name(),
            _ => None,
        }
    }

    /// Get the size in bytes for fixed-size types (Sprint 8)
    pub fn size_in_bytes(&self, schema: &Schema) -> usize {
        match self {
            FieldType::U32 | FieldType::I32 => 4,
            FieldType::U64 | FieldType::I64 | FieldType::F64 | FieldType::Timestamp => 8,
            FieldType::Bool => 1,
            FieldType::Enum(_) => 1, // 1-byte u8 discriminant
            FieldType::Uuid => 16,
            FieldType::Decimal => 16, // exact decimal is a fixed 16-byte column, like Uuid (#189)
            FieldType::Char(size) => *size,
            FieldType::FixedArray(inner, count) => inner.size_in_bytes(schema) * count,
            FieldType::StructType(name) => {
                if let Some(struct_def) = schema.find_struct(name) {
                    Struct::calculate_size(struct_def, schema)
                } else {
                    0
                }
            }
            FieldType::OptionalStructType(name) => {
                // Option adds 1 byte discriminant + size of struct
                1 + if let Some(struct_def) = schema.find_struct(name) {
                    Struct::calculate_size(struct_def, schema)
                } else {
                    0
                }
            }
            // Nullable wraps the inner type in Option; use the inner size (discriminant handled by Rust)
            FieldType::Nullable(inner) => inner.size_in_bytes(schema),
            _ => 0, // Variable-size or virtual
        }
    }

    /// Get alignment requirement for this type (Sprint 8)
    pub fn alignment(&self, schema: &Schema) -> usize {
        match self {
            FieldType::U32 | FieldType::I32 => 4,
            FieldType::U64 | FieldType::I64 | FieldType::F64 | FieldType::Timestamp => 8,
            FieldType::Bool => 1,
            FieldType::Enum(_) => 1, // 1-byte u8 discriminant
            FieldType::Uuid => 16, // UUID is typically 16-byte aligned
            FieldType::Char(_) => 1,
            FieldType::FixedArray(inner, _) => inner.alignment(schema),
            FieldType::StructType(name) => {
                if let Some(struct_def) = schema.find_struct(name) {
                    Struct::calculate_alignment(struct_def, schema)
                } else {
                    1
                }
            }
            FieldType::OptionalStructType(name) => {
                if let Some(struct_def) = schema.find_struct(name) {
                    Struct::calculate_alignment(struct_def, schema)
                } else {
                    1
                }
            }
            FieldType::Nullable(inner) => inner.alignment(schema),
            _ => 1,
        }
    }

    /// Determine if this type supports range queries (ordered)
    pub fn supports_range_queries(&self) -> bool {
        matches!(
            self,
            FieldType::U32
                | FieldType::U64
                | FieldType::I32
                | FieldType::I64
                | FieldType::F64
                | FieldType::Timestamp
        )
    }

    /// Get the default index type for this field type
    pub fn default_index_type(&self) -> IndexType {
        if self.supports_range_queries() {
            IndexType::BTree
        } else {
            IndexType::Hash
        }
    }
}

impl Struct {
    /// Calculate the total size of a struct with proper padding (Sprint 8)
    pub fn calculate_size(struct_def: &Struct, schema: &Schema) -> usize {
        let mut size = 0;
        let mut max_alignment = 1;

        for field in &struct_def.fields {
            let field_size = field.field_type.size_in_bytes(schema);
            let field_align = field.field_type.alignment(schema);

            max_alignment = max_alignment.max(field_align);

            // Add padding before field if needed
            if size % field_align != 0 {
                size += field_align - (size % field_align);
            }

            size += field_size;
        }

        // Add final padding to make struct size a multiple of its alignment
        if size % max_alignment != 0 {
            size += max_alignment - (size % max_alignment);
        }

        size
    }

    /// Calculate the alignment requirement for a struct (Sprint 8)
    pub fn calculate_alignment(struct_def: &Struct, schema: &Schema) -> usize {
        struct_def
            .fields
            .iter()
            .map(|f| f.field_type.alignment(schema))
            .max()
            .unwrap_or(1)
    }
}

impl Field {
    /// Check if field has a specific constraint
    pub fn has_constraint(&self, name: &str) -> bool {
        self.constraints.iter().any(|c| c.name == name)
    }

    /// Get a constraint by name
    pub fn get_constraint(&self, name: &str) -> Option<&Constraint> {
        self.constraints.iter().find(|c| c.name == name)
    }

    /// Check if field is nullable (optional references, optional structs, and nullable primitives)
    pub fn is_nullable(&self) -> bool {
        matches!(
            &self.field_type,
            FieldType::Relation(RelationType::OptionalReference(_))
                | FieldType::OptionalStructType(_)
                | FieldType::Nullable(_)
        )
    }
}

impl RelationType {
    pub fn target_model(&self) -> &str {
        match self {
            RelationType::OneToMany(model) => model,
            RelationType::RequiredReference(model) => model,
            RelationType::OptionalReference(model) => model,
            RelationType::ManyToMany(model) => model,
        }
    }

    pub fn is_one_to_many(&self) -> bool {
        matches!(self, RelationType::OneToMany(_))
    }

    pub fn is_reference(&self) -> bool {
        matches!(
            self,
            RelationType::RequiredReference(_) | RelationType::OptionalReference(_)
        )
    }

    pub fn is_many_to_many(&self) -> bool {
        matches!(self, RelationType::ManyToMany(_))
    }
}