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
use crate::prelude::*;
use anyhow::Result;

fn make_declarations() -> Declarations {
    let mut c = Declarations::new();

    c.add_config(DeclarationsConfig::HackNamespace("GraphiteIngester"));

    c.add(TypeDeclaration {
        name: "",
        docs: "Hello world
        ==========================================================================
        THIS IS AN INDEPENDENT DOCUMENTATION BLOCK
        ==========================================================================",
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::Docs,
    });

    let t_string_val_no_bounds_gen = TGeneric::TDefinition {
        name: "TStringVal",
        bounds: None,
    };

    let id = c.add(TypeDeclaration {
        name: "ID",
        docs: "",
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::TPrimitive(TPrimitive::TDifferentPerLanguage {
            rust: Box::new(TPrimitive::THardcoded("crate::types::ID")),
            hack: Box::new(TPrimitive::Ti64),
            flow: Box::new(TPrimitive::Ti64),
        }),
    });

    let indexable_str = c.add(TypeDeclaration {
        name: "IndexableStr",
        docs: "",
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::TPrimitive(TPrimitive::TDifferentPerLanguage {
            rust: Box::new(TPrimitive::THardcoded("crate::types::IndexableStr")),
            hack: Box::new(TPrimitive::String),
            flow: Box::new(TPrimitive::String),
        }),
    });

    let frame = c.add(TypeDeclaration {
        name: "Frame",
        docs: "Frame represents a tuple of an Timestamp (RFC3339) and an ID",
        config: vec![TypeDeclarationConfig::RustAttribute("#[derive(Copy)]")],
        generic_params: vec![],
        value: DeclarationValue::TTuple(TTuple {
            items: vec![
                TupleItem::TPrimitive(TPrimitive::String),
                TupleItem::TPrimitive(TPrimitive::Ti64),
            ],
        }),
    });

    c.add(TypeDeclaration {
        name: "StorageType",
        docs: "",
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::TSimpleEnum(TSimpleEnum {
            variants: vec!["Full", "Delta", "Empty", "Broken"],
        }),
    });

    c.add(TypeDeclaration {
        name: "Operation",
        docs: "Operation is a single unit of transormation logic",
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::TEnum(TEnum {
            variants: vec![
                EnumVariant {
                    name: "Fetch",
                    docs: "Fetch items by their IDs",
                    variant_type: EnumVariantType::TStruct(TStruct {
                        fields: vec![StructField {
                            name: "items",
                            docs: "item IDs",
                            config: vec![],
                            field_type: StructFieldType::TVec(TVec::TPrimitive(TPrimitive::Ti64)),
                        }],
                    }),
                },
                EnumVariant {
                    name: "Store",
                    docs: "Store graphs to a storage layer",
                    variant_type: EnumVariantType::TStruct(TStruct {
                        fields: vec![StructField {
                            name: "frames",
                            docs: "Destination frames for the storage",
                            config: vec![],
                            field_type: StructFieldType::TVec(TVec::TPrimitive(
                                TPrimitive::TReference(frame),
                            )),
                        }],
                    }),
                },
                EnumVariant {
                    name: "Drop",
                    docs: "Discard all graphs",
                    variant_type: EnumVariantType::TPrimitive(TPrimitive::Tbool),
                },
                EnumVariant {
                    name: "FakeOp",
                    docs: "Not a real operation",
                    variant_type: EnumVariantType::TPrimitive(TPrimitive::Tusize),
                },
            ],
        }),
    });

    let node_id = c.add(TypeDeclaration {
        name: "NodeID",
        docs: "",
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::TPrimitive(TPrimitive::Ti32),
    });

    let graph_node = c.add(TypeDeclaration {
        name: "GraphNode",
        docs: "",
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::TStruct(TStruct {
            fields: vec![StructField {
                name: "node_id",
                docs: "",
                config: vec![],
                field_type: StructFieldType::TPrimitive(TPrimitive::TReference(node_id)),
            }],
        }),
    });

    let generic_enum = c.add(TypeDeclaration {
        name: "GenericEnum",
        docs: "",
        config: vec![],
        generic_params: vec![t_string_val_no_bounds_gen.clone()],
        value: DeclarationValue::TEnum(TEnum {
            variants: vec![
                EnumVariant {
                    name: "A",
                    docs: "",
                    variant_type: EnumVariantType::TPrimitive(TPrimitive::TGeneric(
                        t_string_val_no_bounds_gen,
                    )),
                },
                EnumVariant {
                    name: "B",
                    docs: "",
                    variant_type: EnumVariantType::TPrimitive(TPrimitive::Tu32),
                },
            ],
        }),
    });

    let mut generic_enum_indexed = generic_enum.clone();
    generic_enum_indexed.generic_params = vec![TGeneric::TReference(id)];

    c.add(TypeDeclaration {
        name: "GenericEnumIndexed",
        docs: "",
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::TPrimitive(generic_enum_indexed.primitive()),
    });

    let mut generic_enum_unindexed = generic_enum;
    generic_enum_unindexed.generic_params = vec![TGeneric::TReference(indexable_str.clone())];

    c.add(TypeDeclaration {
        name: "GenericEnumUnindexed",
        docs: "",
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::TPrimitive(generic_enum_unindexed.primitive()),
    });

    let graph_data = c.add(TypeDeclaration {
        name: "GraphData",
        docs: r#"Wrapper value that represents a graph. It contains various top level
        data about the graph as well as a collection of nodes. This is a long
        multiline documentation block that is here for testing purposes only. I'll also
        add some Ascii diagram just to make sure nothing gets misaligned.

            $> SELECT name, age, hometown, credit_card_number FROM users

            +----------+-----+-------------+----------------+
            | Name     | Age | Location    | Credit Card    |
            +----------+-----+-------------+----------------+
            | Bobby    | 17  | El Paso, TX | 1234 4294 2492 |
            | Hannah   | 20  | Memphis, TN | 9494 2492 4024 |
            +----------+-----+-------------+----------------+

        Some more docs after the ASCII drawings.

        Maybe some extra line after a newline.
        "#,
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::TStruct(TStruct {
            fields: vec![
                StructField {
                    name: "entry_points",
                    docs: "Root nodes of the graph",
                    config: vec![],
                    field_type: StructFieldType::TVec(TVec::TPrimitive(TPrimitive::Ti64)),
                },
                StructField {
                    name: "nodes",
                    docs: "",
                    config: vec![],
                    field_type: StructFieldType::TMap(TMap {
                        key: TPrimitive::Ti64,
                        value: TMapValue::TPrimitive(TPrimitive::TReference(graph_node.clone())),
                        t: TMapType::BTree,
                    }),
                },
                StructField {
                    name: "string_fields",
                    docs: "A bunch of random string fields
                    that are represented as a map between string and string
                    and other important lines of documentation.",
                    config: vec![StructFieldConfig::RustAttribute(
                        r#"#[serde(skip_serializing_if = "Option::is_none")]"#,
                    )],
                    field_type: StructFieldType::TOption(TOption::TMap(TMap {
                        key: TPrimitive::String,
                        value: TMapValue::TPrimitive(TPrimitive::String),
                        t: TMapType::BTree,
                    })),
                },
            ],
        }),
    });

    let mut graph_data_unindexed = graph_data;
    graph_data_unindexed.generic_params = vec![
        TGeneric::TReference(indexable_str.clone()),
        TGeneric::TReference(indexable_str),
    ];

    c.add(TypeDeclaration {
        name: "GraphDataUnindexed",
        docs: "",
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::TPrimitive(TPrimitive::TReference(graph_data_unindexed)),
    });

    c.add(TypeDeclaration {
        name: "BasicVec",
        docs: "",
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::TVec(Box::new(TVec::TPrimitive(TPrimitive::Tf64))),
    });

    c.add(TypeDeclaration {
        name: "BasicMap",
        docs: "",
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::TMap(Box::new(TMap {
            key: TPrimitive::Ti64,
            value: TMapValue::TPrimitive(graph_node.primitive()),
            t: TMapType::BTree,
        })),
    });

    c.add(TypeDeclaration {
        name: "BasicOption",
        docs: "",
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::TOption(Box::new(TOption::TPrimitive(graph_node.primitive()))),
    });

    c.add(TypeDeclaration {
        name: "BasicStruct",
        docs: "",
        config: vec![],
        generic_params: vec![],
        value: DeclarationValue::TStruct(TStruct {
            fields: vec![
                StructField {
                    name: "map",
                    docs: "",
                    config: vec![],
                    field_type: StructFieldType::TPrimitive(TPrimitive::TMap(Box::new(TMap {
                        key: TPrimitive::Ti64,
                        value: TMapValue::TPrimitive(graph_node.primitive()),
                        t: TMapType::BTree,
                    }))),
                },
                StructField {
                    name: "vec",
                    docs: "",
                    config: vec![],
                    field_type: StructFieldType::TPrimitive(TPrimitive::TVec(Box::new(
                        TVec::TPrimitive(graph_node.primitive()),
                    ))),
                },
                StructField {
                    name: "vec",
                    docs: "",
                    config: vec![],
                    field_type: StructFieldType::TPrimitive(TPrimitive::TOption(Box::new(
                        TOption::TPrimitive(graph_node.primitive()),
                    ))),
                },
            ],
        }),
    });

    c
}

#[test]
fn rust_test() -> Result<()> {
    let declarations = make_declarations();
    k9::snapshot!(
        declarations.codegen_rust()?,
        r#"
use std::collections::BTreeMap;


// Hello world
// ==========================================================================
// THIS IS AN INDEPENDENT DOCUMENTATION BLOCK
// ==========================================================================


pub type ID = crate::types::ID;

pub type IndexableStr = crate::types::IndexableStr;

#[derive(Copy)]
/// Frame represents a tuple of an Timestamp (RFC3339) and an ID
pub type Frame = (String, i64);

pub enum StorageType {
    Full,
    Delta,
    Empty,
    Broken,
}

/// Operation is a single unit of transormation logic
pub enum Operation {
    /// Fetch items by their IDs
    Fetch {
        /// item IDs
        items: Vec<i64>,
    },
    /// Store graphs to a storage layer
    Store {
        /// Destination frames for the storage
        frames: Vec<Frame>,
    },
    /// Discard all graphs
    Drop(bool),
    /// Not a real operation
    FakeOp(usize),
}

pub type NodeID = i32;

pub struct GraphNode {
    pub node_id: NodeID,
}

pub enum GenericEnum<TStringVal> {
    A(TStringVal),
    B(u32),
}

pub type GenericEnumIndexed = GenericEnum<ID>;

pub type GenericEnumUnindexed = GenericEnum<IndexableStr>;

/// Wrapper value that represents a graph. It contains various top level
/// data about the graph as well as a collection of nodes. This is a long
/// multiline documentation block that is here for testing purposes only. I'll also
/// add some Ascii diagram just to make sure nothing gets misaligned.
/// 
///     $> SELECT name, age, hometown, credit_card_number FROM users
/// 
///     +----------+-----+-------------+----------------+
///     | Name     | Age | Location    | Credit Card    |
///     +----------+-----+-------------+----------------+
///     | Bobby    | 17  | El Paso, TX | 1234 4294 2492 |
///     | Hannah   | 20  | Memphis, TN | 9494 2492 4024 |
///     +----------+-----+-------------+----------------+
/// 
/// Some more docs after the ASCII drawings.
/// 
/// Maybe some extra line after a newline.
pub struct GraphData {
    /// Root nodes of the graph
    pub entry_points: Vec<i64>,
    pub nodes: BTreeMap<i64, GraphNode>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// A bunch of random string fields
    /// that are represented as a map between string and string
    /// and other important lines of documentation.
    pub string_fields: Option<BTreeMap<String, String>>,
}

pub type GraphDataUnindexed = GraphData<IndexableStr, IndexableStr>;

pub type BasicVec = Vec<f64>;

pub type BasicMap = BTreeMap<i64, GraphNode>;

pub type BasicOption = Option<GraphNode>;

pub struct BasicStruct {
    pub map: BTreeMap<i64, GraphNode>,
    pub vec: Vec<GraphNode>,
    pub vec: Option<GraphNode>,
}

"#
    );

    Ok(())
}

#[test]
fn hack_test() -> Result<()> {
    let declarations = make_declarations();
    k9::snapshot!(
        declarations.codegen_hack()?,
        r#"
<?hh

// Hello world
// ==========================================================================
// THIS IS AN INDEPENDENT DOCUMENTATION BLOCK
// ==========================================================================


type GraphiteIngesterID = int;

type GraphiteIngesterIndexableStr = string;

// Frame represents a tuple of an Timestamp (RFC3339) and an ID
type GraphiteIngesterFrame = (string, int);

enum GraphiteIngesterStorageType: string as string {
    FULL = "Full";
    DELTA = "Delta";
    EMPTY = "Empty";
    BROKEN = "Broken";
}

// Operation is a single unit of transormation logic

enum GraphiteIngesterOperationType: string as string {
    FETCH = "Fetch";
    STORE = "Store";
    DROP = "Drop";
    FAKE_OP = "FakeOp";
}

type GraphiteIngesterOperation = shape(
    // Fetch items by their IDs
    ?'Fetch' =>  shape(
        // item IDs
        'items' => vec<int>,
    ),
    // Store graphs to a storage layer
    ?'Store' =>  shape(
        // Destination frames for the storage
        'frames' => vec<GraphiteIngesterFrame>,
    ),
    // Discard all graphs
    ?'Drop' => bool,
    // Not a real operation
    ?'FakeOp' => int,
);

type GraphiteIngesterNodeID = int;

type GraphiteIngesterGraphNode = shape(
    'node_id' => GraphiteIngesterNodeID,
);


enum GraphiteIngesterGenericEnumType: string as string {
    A = "A";
    B = "B";
}

type GraphiteIngesterGenericEnum<TStringVal> = shape(
    ?'A' => TStringVal,
    ?'B' => int,
);

type GraphiteIngesterGenericEnumIndexed = GraphiteIngesterGenericEnum<GraphiteIngesterID>;

type GraphiteIngesterGenericEnumUnindexed = GraphiteIngesterGenericEnum<GraphiteIngesterIndexableStr>;

// Wrapper value that represents a graph. It contains various top level
// data about the graph as well as a collection of nodes. This is a long
// multiline documentation block that is here for testing purposes only. I'll also
// add some Ascii diagram just to make sure nothing gets misaligned.
// 
//     $> SELECT name, age, hometown, credit_card_number FROM users
// 
//     +----------+-----+-------------+----------------+
//     | Name     | Age | Location    | Credit Card    |
//     +----------+-----+-------------+----------------+
//     | Bobby    | 17  | El Paso, TX | 1234 4294 2492 |
//     | Hannah   | 20  | Memphis, TN | 9494 2492 4024 |
//     +----------+-----+-------------+----------------+
// 
// Some more docs after the ASCII drawings.
// 
// Maybe some extra line after a newline.
type GraphiteIngesterGraphData = shape(
    // Root nodes of the graph
    'entry_points' => vec<int>,
    'nodes' => dict<int, GraphiteIngesterGraphNode>,
    // A bunch of random string fields
    // that are represented as a map between string and string
    // and other important lines of documentation.
    ?'string_fields' => dict<string, string>,
);

type GraphiteIngesterGraphDataUnindexed = GraphiteIngesterGraphData<GraphiteIngesterIndexableStr, GraphiteIngesterIndexableStr>;

type GraphiteIngesterBasicVec = vec<float>;

type GraphiteIngesterBasicMap = dict<int, GraphiteIngesterGraphNode>;

type GraphiteIngesterBasicOption = ?GraphiteIngesterGraphNode;

type GraphiteIngesterBasicStruct = shape(
    'map' => dict<int, GraphiteIngesterGraphNode>,
    'vec' => vec<GraphiteIngesterGraphNode>,
    'vec' => ?GraphiteIngesterGraphNode,
);

"#
    );

    Ok(())
}

#[test]
fn flow_test() -> Result<()> {
    let declarations = make_declarations();
    k9::snapshot!(
        declarations.codegen_flow()?,
        r#"

// Hello world
// ==========================================================================
// THIS IS AN INDEPENDENT DOCUMENTATION BLOCK
// ==========================================================================


export type ID = number;

export type IndexableStr = string;

// Frame represents a tuple of an Timestamp (RFC3339) and an ID
export type Frame = [string, number];

export type StorageType = "Full" | "Delta" | "Empty" | "Broken";

export enum StorageTypeEnum {Full, Delta, Empty, Broken};

// Operation is a single unit of transormation logic
export type OperationType = "Fetch" | "Store" | "Drop" | "FakeOp";

export type Operation = {
    // Fetch items by their IDs
    'Fetch'?:  {
        // item IDs
        'items': Array<number>,
    },
    // Store graphs to a storage layer
    'Store'?:  {
        // Destination frames for the storage
        'frames': Array<Frame>,
    },
    // Discard all graphs
    'Drop'?: boolean,
    // Not a real operation
    'FakeOp'?: number,
};

export type NodeID = number;

export type GraphNode = {
    'node_id': NodeID,
};

export type GenericEnumType = "A" | "B";

export type GenericEnum<TStringVal> = {
    'A'?: TStringVal,
    'B'?: number,
};

export type GenericEnumIndexed = GenericEnum<ID>;

export type GenericEnumUnindexed = GenericEnum<IndexableStr>;

// Wrapper value that represents a graph. It contains various top level
// data about the graph as well as a collection of nodes. This is a long
// multiline documentation block that is here for testing purposes only. I'll also
// add some Ascii diagram just to make sure nothing gets misaligned.
// 
//     $> SELECT name, age, hometown, credit_card_number FROM users
// 
//     +----------+-----+-------------+----------------+
//     | Name     | Age | Location    | Credit Card    |
//     +----------+-----+-------------+----------------+
//     | Bobby    | 17  | El Paso, TX | 1234 4294 2492 |
//     | Hannah   | 20  | Memphis, TN | 9494 2492 4024 |
//     +----------+-----+-------------+----------------+
// 
// Some more docs after the ASCII drawings.
// 
// Maybe some extra line after a newline.
export type GraphData = {
    // Root nodes of the graph
    'entry_points': Array<number>,
    'nodes': {[key: number]: GraphNode},
    // A bunch of random string fields
    // that are represented as a map between string and string
    // and other important lines of documentation.
    'string_fields'?: {[key: string]: string},
};

export type GraphDataUnindexed = GraphData<IndexableStr, IndexableStr>;

export type BasicVec = Array<number>;

export type BasicMap = {[key: number]: GraphNode};

export type BasicOption = ?GraphNode;

export type BasicStruct = {
    'map': {[key: number]: GraphNode},
    'vec': Array<GraphNode>,
    'vec': ?GraphNode,
};

"#
    );
    Ok(())
}