cookie_cutter_core 0.2.0

A feature-rich template engine with context aware escaping and both runtime and compiletime compilation
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
use chumsky::Parser;

use crate::{
    __internal__parse_include_str, __internal__parse_with_path, __internal__span_macro_include_str,
    ast::{
        Command, CurlyKind, Expression, IndentationMarker, Literal, MemberAccess, SourceFile,
        StringLiteralComponent, Template, TemplateBody, TemplateContent,
        TemplateContentEscapeSequence, TemplateLine, TemplateLineNewline, Type, TypeDefinition,
    },
    parse::source_file::{source_file, source_file_whitespace_or_comments},
};

use pretty_assertions::assert_eq;

#[test]
fn test_parse_source_file_whitespace_or_comments() {
    assert!(!__internal__parse_with_path!(
        source_file_whitespace_or_comments(),
        "<test>",
        " // comments galore!\n \t /* hello // nested comment\n/* more nested */ still comment */"
    )
    .has_errors());
}

#[test]
#[allow(clippy::too_many_lines)]
fn test_parse_source_file() {
    __internal__span_macro_include_str!(
        crate::Span,
        "../../../../testdata/source_files/stress_test.cctmpl"
    );

    assert_eq!(
        __internal__parse_include_str!(source_file(), "../../../../testdata/source_files/stress_test.cctmpl")
            .unwrap(),
        SourceFile {
            type_defs: vec![
                (
                    TypeDefinition {
                        name: (
                            "Blub",
                            s!(344..348),
                        ),
                        ty: (
                            Type::Options(
                                vec![
                                    (
                                        Type::Tuple(
                                            vec![],
                                        ),
                                        s!(351..353),
                                    ),
                                    (
                                        Type::Struct(
                                            vec![],
                                        ),
                                        s!(356..358),
                                    ),
                                    (
                                        Type::Ident(
                                            "Blub",
                                        ),
                                        s!(361..365),
                                    ),
                                ],
                            ),
                            s!(351..365),
                        ),
                    },
                    s!(339..366),
                ),
            ],

            templates: vec![
                (
                    Template {
                        output_type: (None, s!(79..79)),
                        name: ("tour", s!(88..92)),
                        parameters: vec![],
                        body: TemplateBody {
                            indentation_marker: IndentationMarker::Present {
                                indentation_and_span: ("", s!(100..100)),
                            },
                            lines: vec![
                                (
                                    TemplateLine {
                                        had_comments: true,
                                        indentation: ("\t", s!(102..103)),
                                        content: vec![],
                                        newline: (
                                            TemplateLineNewline::Normal("\n"),
                                            s!(122..123)
                                        )
                                    },
                                    s!(102..123)
                                ),
                                (
                                    TemplateLine {
                                        had_comments: true,
                                        indentation: ("\t", s!(123..124)),
                                        content: vec![],
                                        newline: (
                                            TemplateLineNewline::Normal("\n"),
                                            s!(157..158)
                                        )
                                    },
                                    s!(123..158)
                                ),
                                (
                                    TemplateLine {
                                        had_comments: false,
                                        indentation: ("\t", s!(158..159)),
                                        content: vec![
                                            (
                                                TemplateContent::EscapeSequence(
                                                    TemplateContentEscapeSequence::Curly(
                                                        CurlyKind::Opening,
                                                        2,
                                                        "{{",
                                                        s!(95..97),
                                                    )
                                                ),
                                                s!(159..162)
                                            ),
                                            (TemplateContent::Static(" "), s!(162..163)),
                                            (
                                                TemplateContent::EscapeSequence(
                                                    TemplateContentEscapeSequence::Curly(
                                                        CurlyKind::Closing,
                                                        2,
                                                        "}}",
                                                        s!(95..97)
                                                    )
                                                ),
                                                s!(163..166)
                                            ),
                                            (TemplateContent::Static(" "), s!(166..167)),
                                            (
                                                TemplateContent::EscapeSequence(
                                                    TemplateContentEscapeSequence::Curly(
                                                        CurlyKind::Opening,
                                                        2,
                                                        "{{",
                                                        s!(95..97)
                                                    )
                                                ),
                                                s!(167..170)
                                            ),
                                            (TemplateContent::Static("{ "), s!(170..172)),
                                            (
                                                TemplateContent::EscapeSequence(
                                                    TemplateContentEscapeSequence::Curly(
                                                        CurlyKind::Closing,
                                                        2,
                                                        "}}",
                                                        s!(95..97)
                                                    )
                                                ),
                                                s!(172..175)
                                            ),
                                            (TemplateContent::Static("} {} "), s!(175..180))
                                        ],
                                        newline: (TemplateLineNewline::Escaped, s!(180..182))
                                    },
                                    s!(158..182)
                                ),
                                (
                                    TemplateLine {
                                        had_comments: false,
                                        indentation: ("\t  ", s!(182..185)),
                                        content: vec![(
                                            TemplateContent::Static("Some more"),
                                            s!(185..194)
                                        )],
                                        newline: (
                                            TemplateLineNewline::Normal("\n"),
                                            s!(194..195)
                                        )
                                    },
                                    s!(182..195)
                                ),
                                (
                                    TemplateLine {
                                        had_comments: false,
                                        indentation: ("\t", s!(195..196)),
                                        content: vec![(
                                            TemplateContent::Static("Second line"),
                                            s!(196..207)
                                        )],
                                        newline: (
                                            TemplateLineNewline::Normal("\n"),
                                            s!(207..208)
                                        )
                                    },
                                    s!(195..208)
                                ),
                                (
                                    TemplateLine {
                                        had_comments: false,
                                        indentation: ("\t", s!(208..209)),
                                        content: vec![(
                                            TemplateContent::Command(Box::new(Command::Assignment {
                                                variable: ("var", s!(233..236)),
                                                expression: (
                                                    Expression::Literal(Literal::String(vec![(
                                                        StringLiteralComponent::Literal("test"),
                                                        s!(240..244)
                                                    )])),
                                                    s!(239..245)
                                                )
                                            })),
                                            s!(209..247)
                                        )],
                                        newline: (
                                            TemplateLineNewline::Normal("\n"),
                                            s!(247..248)
                                        )
                                    },
                                    s!(208..248)
                                ),
                                (
                                    TemplateLine {
                                        had_comments: false,
                                        indentation: ("\t", s!(248..249)),
                                        content: vec![(
                                            TemplateContent::Command(Box::new(Command::Expression(
                                                Expression::Call {
                                                    func: (Box::new(Expression::VariableOrCall("tour2")), s!(254..259)),
                                                    arguments: vec![(
                                                        Expression::Call {
                                                            func: (Box::new(Expression::VariableOrCall("some_func")), s!(262..271)),
                                                            arguments: vec![
                                                                (
                                                                    Expression::VariableOrCall("some"),
                                                                    s!(272..276)
                                                                ),
                                                                (
                                                                    Expression::VariableOrCall("args"),
                                                                    s!(310..314)
                                                                )
                                                            ]
                                                        },
                                                        s!(262..314)
                                                    )]
                                                },
                                                s!(254..314)
                                            ))),
                                            s!(249..334)
                                        )],
                                        newline: (
                                            TemplateLineNewline::Normal("\n"),
                                            s!(334..335)
                                        )
                                    },
                                    s!(248..335)
                                )
                            ]
                        }
                    },
                    s!(79..337)
                ),
                (
                    Template {
                        output_type: (Some("html"), s!(368..372)),
                        name: ("tour2", s!(382..387)),
                        parameters: vec![
                            (("bla", s!(388..391)), (None, s!(391..391))),
                            (
                                ("bla2", s!(393..397)),
                                (
                                    Some(Type::Struct(vec![
                                        (("key", s!(461..464)), (None, s!(464..464))),
                                        (
                                            ("other_key", s!(466..475)),
                                            (Some(Type::Ident("text")), s!(477..481))
                                        ),
                                        (
                                            ("key3", s!(483..487)),
                                            (Some(Type::Array((None, s!(516..516)))), s!(489..517))
                                        )
                                    ])),
                                    s!(460..518)
                                )
                            )
                        ],
                        body: TemplateBody {
                            indentation_marker: IndentationMarker::Escaped {
                                indentation_and_span: ("\t", s!(522..523)),
                                marker_span: s!(523..525),
                                newline: ("\n", s!(525..526))
                            },
                            lines: vec![
                                (
                                    TemplateLine {
                                        had_comments: false,
                                        indentation: ("\t", s!(526..527)),
                                        content: vec![(
                                            TemplateContent::Command(Box::new(Command::Expression(
                                                Expression::VariableOrCall("bla"),
                                                s!(528..531)
                                            ))),
                                            s!(527..532)
                                        )],
                                        newline: (
                                            TemplateLineNewline::Normal("\n"),
                                            s!(532..533)
                                        )
                                    },
                                    s!(526..533)
                                ),
                                (
                                    TemplateLine {
                                        had_comments: false,
                                        indentation: ("", s!(533..533)),
                                        content: vec![],
                                        newline: (
                                            TemplateLineNewline::Normal("\n",),
                                            s!(533..534)
                                        )
                                    },
                                    s!(533..534)
                                ),
                                (
                                    TemplateLine {
                                        had_comments: false,
                                        indentation: ("\t", s!(534..535)),
                                        content: vec![
                                            (TemplateContent::Command(Box::new(Command::Expression(
                                                Expression::Literal(Literal::Template(
                                                    (Some("html"), s!(537..541)),
                                                    Box::new(TemplateBody {
                                                        indentation_marker: IndentationMarker::None,
                                                        lines: vec![(
                                                            TemplateLine {
                                                                had_comments: false,
                                                                indentation: (
                                                                    "\t\t",
                                                                    s!(543..545)
                                                                ),
                                                                content: vec![
                                                                    (
                                                                        TemplateContent::Static(
                                                                            "Hello "
                                                                        ),
                                                                        s!(545..551)
                                                                    ),
                                                                    (TemplateContent::Command(
                                                                        Box::new(Command::Expression(
                                                                            Expression::MemberAccess {
                                                                                expression: (Box::new(Expression::VariableOrCall("bla2")), s!(552..556)),
                                                                                access: (MemberAccess::Named("other_key", s!(557..566)), s!(556..566))
                                                                            },
                                                                            s!(552..566)
                                                                        ))
                                                                    ), s!(551..567)),
                                                                    (
                                                                        TemplateContent::Static("!"),
                                                                        s!(567..568)
                                                                    )
                                                                ],
                                                                newline: (TemplateLineNewline::Normal("\n"), s!(568..569))
                                                            },
                                                            s!(543..569)
                                                        )]
                                                    })
                                                )),
                                                s!(537..571)
                                            ))), s!(535..573))
                                        ],
                                        newline: (TemplateLineNewline::Normal("\n"), s!(573..574))
                                    },
                                    s!(534..574)
                                ),
                                (
                                    TemplateLine {
                                        had_comments: false,
                                        indentation: (
                                            "",
                                            s!(574..574),
                                        ),
                                        content: vec![],
                                        newline: (
                                            TemplateLineNewline::Normal(
                                                "\n",
                                            ),
                                            s!(574..575),
                                        ),
                                    },
                                    s!(574..575),
                                ),
                                (
                                    TemplateLine {
                                        had_comments: false,
                                        indentation: (
                                            "    ",
                                            s!(575..579),
                                        ),
                                        content: vec![
                                            (
                                                TemplateContent::Command(
                                                    Box::new(Command::Expression(
                                                        Expression::Select {
                                                            expr: (
                                                                Box::new(Expression::VariableOrCall(
                                                                    "bla",
                                                                )),
                                                                s!(618..621),
                                                            ),
                                                            arms: vec![
                                                                (
                                                                    (
                                                                        "whatever",
                                                                        s!(657..665),
                                                                    ),
                                                                    (
                                                                        Type::Options(
                                                                            vec![
                                                                                (
                                                                                    Type::Tuple(
                                                                                        vec![],
                                                                                    ),
                                                                                    s!(667..669),
                                                                                ),
                                                                                (
                                                                                    Type::Ident(
                                                                                        "whichever",
                                                                                    ),
                                                                                    s!(672..681),
                                                                                ),
                                                                            ],
                                                                        ),
                                                                        s!(667..681),
                                                                    ),
                                                                    (
                                                                        Expression::Literal(
                                                                            Literal::String(
                                                                                vec![],
                                                                            ),
                                                                        ),
                                                                        s!(685..687),
                                                                    ),
                                                                ),
                                                            ],
                                                        },
                                                        s!(581..694),
                                                    )),
                                                ),
                                                s!(579..708),
                                            ),
                                        ],
                                        newline: (
                                             TemplateLineNewline::Normal(
                                                "\n",
                                            ),
                                            s!(708..709),
                                        ),
                                    },
                                    s!(575..709),
                                ),
                            ]
                        }
                    },
                    s!(368..710)
                )
            ]
        }
    );
}