cp-ast-json 0.1.3

Lossless JSON serialization boundary for cp-ast-core AST
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
//! Data Transfer Objects for lossless AST JSON serialization.
//!
//! These types form the JSON schema contract. Internal types are never
//! directly serialized — conversion goes through these DTOs.

use serde::{Deserialize, Serialize};

/// Schema version. Bump when breaking the JSON format.
pub const CURRENT_SCHEMA_VERSION: u32 = 1;

// ── Top-level envelope ──────────────────────────────────────────────

/// Versioned envelope wrapping the AST document.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AstDocumentEnvelope {
    pub schema_version: u32,
    pub document: AstDocumentDto,
}

/// The complete AST document (structure + constraints).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AstDocumentDto {
    pub structure: StructureAstDto,
    pub constraints: ConstraintSetDto,
}

// ── Structure ───────────────────────────────────────────────────────

/// Arena-based structure AST.
///
/// `arena` preserves insertion order and tombstone (`null`) slots.
/// IDs are decimal strings to avoid JS 53-bit precision loss.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StructureAstDto {
    pub root: String,
    pub next_id: String,
    pub arena: Vec<Option<StructureNodeDto>>,
}

/// A single structure node.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StructureNodeDto {
    pub id: String,
    pub kind: NodeKindDto,
}

/// Discriminated union for node kinds.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum NodeKindDto {
    Scalar {
        name: String,
    },
    Array {
        name: String,
        length: ExpressionDto,
    },
    Matrix {
        name: String,
        rows: ReferenceDto,
        cols: ReferenceDto,
    },
    Tuple {
        elements: Vec<String>,
    },
    Repeat {
        count: ExpressionDto,
        #[serde(skip_serializing_if = "Option::is_none")]
        index_var: Option<String>,
        body: Vec<String>,
    },
    Section {
        #[serde(skip_serializing_if = "Option::is_none")]
        header: Option<String>,
        body: Vec<String>,
    },
    Sequence {
        children: Vec<String>,
    },
    Choice {
        tag: ReferenceDto,
        variants: Vec<ChoiceVariantDto>,
    },
    Hole {
        #[serde(skip_serializing_if = "Option::is_none")]
        expected_kind: Option<String>,
    },
}

/// A variant arm in a `Choice` node.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChoiceVariantDto {
    pub tag_value: LiteralDto,
    pub body: Vec<String>,
}

// ── Constraints ─────────────────────────────────────────────────────

/// Arena-based constraint set.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConstraintSetDto {
    pub next_id: String,
    pub arena: Vec<Option<ConstraintEntryDto>>,
    pub by_node: Vec<ByNodeEntryDto>,
    pub global: Vec<String>,
}

/// A single constraint entry in the arena.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConstraintEntryDto {
    pub id: String,
    pub constraint: ConstraintDto,
}

/// Per-node constraint index entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ByNodeEntryDto {
    pub node_id: String,
    pub constraints: Vec<String>,
}

/// Discriminated union for constraints.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum ConstraintDto {
    Range {
        target: ReferenceDto,
        lower: ExpressionDto,
        upper: ExpressionDto,
    },
    TypeDecl {
        target: ReferenceDto,
        expected: String,
    },
    LengthRelation {
        target: ReferenceDto,
        length: ExpressionDto,
    },
    Relation {
        lhs: ExpressionDto,
        op: String,
        rhs: ExpressionDto,
    },
    Distinct {
        elements: ReferenceDto,
        unit: String,
    },
    Property {
        target: ReferenceDto,
        tag: PropertyTagDto,
    },
    SumBound {
        variable: ReferenceDto,
        upper: ExpressionDto,
    },
    Sorted {
        elements: ReferenceDto,
        order: String,
    },
    Guarantee {
        description: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        predicate: Option<ExpressionDto>,
    },
    CharSet {
        target: ReferenceDto,
        charset: CharSetSpecDto,
    },
    StringLength {
        target: ReferenceDto,
        min: ExpressionDto,
        max: ExpressionDto,
    },
    RenderHint {
        target: ReferenceDto,
        hint: RenderHintKindDto,
    },
}

// ── Expressions ─────────────────────────────────────────────────────

/// Discriminated union for numeric expressions.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum ExpressionDto {
    Lit {
        value: i64,
    },
    Var {
        reference: ReferenceDto,
    },
    BinOp {
        op: String,
        lhs: Box<ExpressionDto>,
        rhs: Box<ExpressionDto>,
    },
    Pow {
        base: Box<ExpressionDto>,
        exp: Box<ExpressionDto>,
    },
    FnCall {
        name: String,
        args: Vec<ExpressionDto>,
    },
}

/// Discriminated union for references.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum ReferenceDto {
    VariableRef {
        node_id: String,
    },
    IndexedRef {
        target: String,
        indices: Vec<String>,
    },
    Unresolved {
        name: String,
    },
}

/// Discriminated union for literal values.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "kind")]
pub enum LiteralDto {
    IntLit { value: i64 },
    StrLit { value: String },
}

// ── Small enums ─────────────────────────────────────────────────────

/// Property tag (discriminated union with Custom having a value).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum PropertyTagDto {
    Simple,
    Connected,
    Tree,
    Permutation,
    Binary,
    Odd,
    Even,
    Custom { value: String },
}

/// Character set specification.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "kind")]
pub enum CharSetSpecDto {
    LowerAlpha,
    UpperAlpha,
    Alpha,
    Digit,
    AlphaNumeric,
    Custom { chars: Vec<char> },
    Range { from: char, to: char },
}

/// Render hint kind.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum RenderHintKindDto {
    Separator { value: String },
}

// ── Actions ─────────────────────────────────────────────────────────

/// Discriminated union for builder-layer actions.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "action")]
pub enum ActionDto {
    FillHole {
        target: String,
        fill: FillContentDto,
    },
    ReplaceNode {
        target: String,
        replacement: FillContentDto,
    },
    AddConstraint {
        target: String,
        constraint: ConstraintDefDto,
    },
    RemoveConstraint {
        constraint_id: String,
    },
    IntroduceMultiTestCase {
        count_var_name: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        sum_bound: Option<SumBoundDefDto>,
    },
    AddSlotElement {
        parent: String,
        slot_name: String,
        element: FillContentDto,
    },
    RemoveSlotElement {
        parent: String,
        slot_name: String,
        child: String,
    },
    AddSibling {
        target: String,
        element: FillContentDto,
    },
    AddChoiceVariant {
        choice: String,
        tag_value: LiteralDto,
        first_element: FillContentDto,
    },
}

/// High-level fill intent for hole filling.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "kind")]
pub enum FillContentDto {
    Scalar {
        name: String,
        typ: String,
    },
    Array {
        name: String,
        element_type: String,
        length: LengthSpecDto,
    },
    Repeat {
        count: LengthSpecDto,
    },
    Grid {
        name: String,
        rows: LengthSpecDto,
        cols: LengthSpecDto,
        cell_type: String,
    },
    Section {
        label: String,
    },
    OutputSingleValue {
        typ: String,
    },
    OutputYesNo,
    EdgeList {
        edge_count: LengthSpecDto,
    },
    WeightedEdgeList {
        edge_count: LengthSpecDto,
        weight_name: String,
        weight_type: String,
    },
    QueryList {
        query_count: LengthSpecDto,
    },
    MultiTestCaseTemplate {
        count: LengthSpecDto,
    },
    GridTemplate {
        name: String,
        rows: LengthSpecDto,
        cols: LengthSpecDto,
        cell_type: String,
    },
}

/// Length specification for arrays/grids.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "kind")]
pub enum LengthSpecDto {
    Fixed { value: usize },
    RefVar { node_id: String },
    Expr { expr: String },
}

/// Constraint definition from the builder layer.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "kind")]
pub enum ConstraintDefDto {
    Range { lower: String, upper: String },
    TypeDecl { typ: String },
    Relation { op: String, rhs: String },
    Distinct,
    Sorted { order: String },
    Property { tag: String },
    SumBound { over_var: String, upper: String },
    CharSet { charset: CharSetSpecDto },
    StringLength { min: String, max: String },
    Guarantee { description: String },
}

/// Sum bound definition for multi-test-case introduction.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SumBoundDefDto {
    pub bound_var: String,
    pub upper: String,
}

// ── FullProjection DTOs ─────────────────────────────────────────────

/// Rich projection of the entire AST for the editor UI.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FullProjectionDto {
    pub nodes: Vec<ProjectedNodeDto>,
    pub structure_lines: Vec<StructureLineDto>,
    pub hotspots: Vec<HotspotDto>,
    pub constraints: ProjectedConstraintsDto,
    pub available_vars: Vec<ExprCandidateDto>,
    pub completeness: CompletenessSummaryDto,
}

/// A display row in the Structure pane.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StructureLineDto {
    pub depth: usize,
    pub nodes: Vec<ProjectedNodeDto>,
}

/// A projected node for UI display.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectedNodeDto {
    pub id: String,
    pub label: String,
    pub depth: usize,
    pub is_hole: bool,
    pub edit: Option<NodeEditProjectionDto>,
}

/// Semantic edit metadata for a projected structure node.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeEditProjectionDto {
    pub kind: String,
    pub name: String,
    pub value_type: String,
    pub length_expr: Option<String>,
    pub allowed_kinds: Vec<String>,
    pub allowed_types: Vec<String>,
}

/// An insertion point in the UI.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HotspotDto {
    pub parent_id: String,
    pub direction: String,
    pub candidates: Vec<String>,
    pub candidate_details: Vec<HoleCandidateDetailDto>,
    pub action: HotspotActionDto,
}

/// Declarative action routing for a projected hotspot.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HotspotActionDto {
    pub kind: String,
    pub target_id: String,
    pub slot_name: Option<String>,
}

/// Detailed field schema for a candidate shown in the node popup.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HoleCandidateDetailDto {
    pub kind: String,
    pub label: String,
    pub fields: Vec<CandidateFieldDto>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CandidateFieldDto {
    pub name: String,
    pub field_type: String,
    pub label: String,
    pub required: bool,
    pub options: Option<Vec<String>>,
    pub default_value: Option<String>,
}

/// Projected constraints split into draft and completed.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectedConstraintsDto {
    pub items: Vec<ConstraintItemDto>,
    pub drafts: Vec<DraftConstraintDto>,
    pub completed: Vec<CompletedConstraintDto>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConstraintItemDto {
    pub index: usize,
    pub status: String,
    pub target_id: String,
    pub target_name: String,
    pub display: String,
    pub template: Option<String>,
    pub constraint_id: Option<String>,
    pub draft_index: Option<usize>,
    pub completed_index: Option<usize>,
    pub edit: Option<ConstraintEditProjectionDto>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum ConstraintEditProjectionDto {
    Range {
        lower: String,
        upper: String,
        constraint_id: Option<String>,
    },
    CharSet {
        charset: CharSetSpecDto,
        constraint_id: Option<String>,
    },
    StringLength {
        min: String,
        max: String,
        constraint_id: Option<String>,
    },
}

/// An unfilled constraint generated on-the-fly by projection.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DraftConstraintDto {
    pub index: usize,
    pub target_id: String,
    pub target_name: String,
    pub display: String,
    pub template: String,
}

/// A fully specified constraint from the AST.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompletedConstraintDto {
    pub index: usize,
    pub constraint_id: String,
    pub display: String,
}

/// A variable available for use in expressions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExprCandidateDto {
    pub name: String,
    pub node_id: String,
    pub value_type: String,
    pub node_kind: String,
}

/// Summary of AST completeness and satisfaction status.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompletenessSummaryDto {
    pub total_holes: usize,
    pub filled_slots: usize,
    pub unsatisfied_constraints: usize,
    pub is_complete: bool,
}