1pub(crate) const FN_IDX: &str = "idx";
10
11pub(crate) const FN_LEN: &str = "len";
13
14pub(crate) const FN_KIND: &str = "kind";
16pub(crate) const FN_KINDS: &str = "kinds";
18
19pub(crate) const FN_HAS: &str = "has";
21
22pub(crate) const BUILTIN_FUNCTIONS: &[&str] = &[FN_IDX, FN_LEN, FN_KIND, FN_KINDS, FN_HAS];
27
28pub(crate) const FILTER_UPPER: &str = "upper";
32pub(crate) const FILTER_LOWER: &str = "lower";
34pub(crate) const FILTER_TRIM: &str = "trim";
36pub(crate) const FILTER_FIXED: &str = "fixed";
38pub(crate) const FILTER_JOIN: &str = "join";
40pub(crate) const FILTER_LIMIT: &str = "limit";
42pub(crate) const FILTER_ADD: &str = "add";
44pub(crate) const FILTER_SUB: &str = "sub";
46
47pub const ENUM_TAG_KEY: &str = "__kind__";
54pub const ENUM_VARIANTS_KEY: &str = "__variants__";
56
57pub const PAREN_OPEN: char = '(';
61pub const PAREN_CLOSE: char = ')';
63pub const PATH_SEP: char = '.';
65pub const PIPE: char = '|';
67pub const ANGLE_OPEN: char = '<';
69pub const ANGLE_CLOSE: char = '>';
71pub const BRACKET_OPEN: char = '[';
73pub const BRACKET_CLOSE: char = ']';
75pub const BRACE_OPEN: char = '{';
77pub const BRACE_CLOSE: char = '}';
79pub const COMMA: char = ',';
81pub const COLON: char = ':';
83pub const EQUALS: char = '=';
85pub const SLASH: char = '/';
87pub const PATH_PREFIX_SLASH: &str = "/";
89pub const PATH_PREFIX_CUR: &str = "./";
91pub const PATH_PREFIX_PARENT: &str = "../";
93pub const PATH_PREFIX_CUR_WIN: &str = ".\\";
95pub const PATH_PREFIX_PARENT_WIN: &str = "..\\";
97pub const BACKSLASH: char = '\\';
99pub const CHAR_NEWLINE: char = '\n';
101pub const CHAR_CR: char = '\r';
103pub const CHAR_SPACE: char = ' ';
105pub const CHAR_TAB: char = '\t';
107
108pub const STR_NEWLINE: &str = "\n";
110pub const STR_DOUBLE_NEWLINE: &str = "\n\n";
112pub const STR_CRLF: &str = "\r\n";
114
115#[must_use]
117pub fn is_valid_resolved_path(path: &str) -> bool {
118 path.starts_with(PATH_PREFIX_SLASH)
119 || path.starts_with(PATH_PREFIX_CUR)
120 || path.starts_with(PATH_PREFIX_PARENT)
121 || path.starts_with(PATH_PREFIX_CUR_WIN)
122 || path.starts_with(PATH_PREFIX_PARENT_WIN)
123}
124
125#[must_use]
127pub fn is_valid_include_path(path: &str) -> bool {
128 is_valid_resolved_path(path) || path.starts_with(EXPR_START)
129}
130
131pub const EXT_TMPL_MD: &str = ".tmpl.md";
133pub const EXT_TMPL: &str = ".tmpl";
135pub const EXT_MD: &str = ".md";
137pub const LIST_BLOCK_SEP: &str = " - ";
139pub const LIST_ITEM_PREFIX: &str = "- ";
141pub const QUOTE_DOUBLE: char = '"';
143pub const QUOTE_SINGLE: char = '\'';
145
146pub const PAREN_OPEN_BYTE: u8 = b'(';
147pub const PAREN_CLOSE_BYTE: u8 = b')';
148pub const ANGLE_OPEN_BYTE: u8 = b'<';
149pub const ANGLE_CLOSE_BYTE: u8 = b'>';
150pub const BRACKET_OPEN_BYTE: u8 = b'[';
151pub const BRACKET_CLOSE_BYTE: u8 = b']';
152pub const BRACE_OPEN_BYTE: u8 = b'{';
153pub const BRACE_CLOSE_BYTE: u8 = b'}';
154pub const COLON_BYTE: u8 = b':';
155pub const EQUALS_BYTE: u8 = b'=';
156
157pub(crate) const EXPR_START: &str = "{{";
161pub(crate) const EXPR_END: &str = "}}";
163
164pub(crate) const STMT_START: &str = "{%";
166pub(crate) const STMT_END: &str = "%}";
168
169pub(crate) const COMMENT_START: &str = "{#";
171pub(crate) const COMMENT_END: &str = "#}";
173
174pub(crate) const TRIM_MARKER: char = '-';
176
177pub(crate) const TAG_FOR_PREFIX: &str = "for ";
181pub(crate) const KW_IN_SPACED: &str = " in ";
183
184pub(crate) const OP_EQ: &str = " == ";
185pub(crate) const OP_NE: &str = " != ";
186pub(crate) const OP_LE: &str = " <= ";
187pub(crate) const OP_GE: &str = " >= ";
188pub(crate) const OP_LT: &str = " < ";
189pub(crate) const OP_GT: &str = " > ";
190
191pub(crate) const OP_AND: &str = "&&";
193pub(crate) const OP_OR: &str = "||";
195pub(crate) const OP_NOT: char = '!';
197
198pub(crate) const TAG_IF_PREFIX: &str = "if ";
200pub(crate) const TAG_ELIF_PREFIX: &str = "elif ";
202pub(crate) const KW_ELSE: &str = "else";
204
205pub(crate) const KW_RAW: &str = "raw";
207pub(crate) const KW_RAW_ASSIGN: &str = "raw=";
209
210pub(crate) const KW_INCLUDE: &str = "include";
212pub(crate) const TAG_INCLUDE_PREFIX: &str = "include ";
214pub(crate) const TAG_WITH_PREFIX: &str = "with ";
216pub(crate) const TAG_WITH_SPACED: &str = " with ";
218
219pub(crate) const TAG_TMPL_PREFIX: &str = "tmpl ";
221
222pub(crate) const TAG_MATCH_PREFIX: &str = "match ";
224pub(crate) const TAG_CASE_PREFIX: &str = "case ";
226pub(crate) const TAG_CASE_SPACED: &str = " case";
228pub(crate) const KW_CASE_SPACED: &str = " case ";
230pub(crate) const VARIANT_SEP: char = '|';
232pub(crate) const KW_PANIC: &str = "panic";
233pub(crate) const TAG_PANIC_PREFIX: &str = "panic ";
234pub(crate) const TAG_PANIC_PAREN: &str = "panic(";
235
236pub(crate) const CLOSE_IF: &str = "/if";
240pub(crate) const CLOSE_FOR: &str = "/for";
242pub(crate) const CLOSE_RAW: &str = "/raw";
244pub(crate) const CLOSE_TMPL: &str = "/tmpl";
246pub(crate) const CLOSE_MATCH: &str = "/match";
248
249pub(crate) const BLOCKQUOTE_PREFIX: char = '>';
253pub(crate) const BLOCKQUOTE_PREFIX_SPACED: &str = "> ";
255pub(crate) const BLOCKQUOTE_COMPACT_OPEN: &str = ">{";
257pub(crate) const BLOCKQUOTE_SPACED_OPEN: &str = "> {%";
259
260pub(crate) const FM_DELIMITER: &str = "---";
264pub(crate) const FM_DELIMITER_NEWLINE: &str = "\n---";
266
267pub(crate) const FM_NAME_PREFIX: &str = "name:";
269pub(crate) const FM_DESC_PREFIX: &str = "description:";
271pub(crate) const FM_PARAMS_PREFIX: &str = "params:";
273pub(crate) const FM_ALLOW_UNUSED_PREFIX: &str = "allow_unused:";
275pub(crate) const FM_TYPES_PREFIX: &str = "types:";
277pub(crate) const FM_IMPORTS_PREFIX: &str = "imports:";
279pub(crate) const FM_CONSTS_PREFIX: &str = "consts:";
281pub(crate) const FM_ENV_PREFIX: &str = "env:";
283
284pub(crate) const TYPE_STR: &str = "str";
288pub(crate) const TYPE_BOOL: &str = "bool";
290pub(crate) const TYPE_INT: &str = "int";
292pub(crate) const TYPE_FLOAT: &str = "float";
294pub(crate) const TYPE_LIST: &str = "list";
296pub(crate) const TYPE_STRUCT: &str = "struct";
298pub(crate) const TYPE_ENUM: &str = "enum";
300pub(crate) const TYPE_TMPL: &str = "tmpl";
302pub(crate) const TYPE_NONE: &str = "none";
304
305pub(crate) const TYPE_LIST_PREFIX: &str = "list(";
307pub(crate) const TYPE_STRUCT_PREFIX: &str = "struct(";
309pub(crate) const TYPE_STRUCT_ANGLE_PREFIX: &str = "struct<";
311pub(crate) const TYPE_STRUCT_BRACKET_PREFIX: &str = "struct[";
313pub(crate) const TYPE_STRUCT_SPACE_PREFIX: &str = "struct ";
315pub(crate) const TYPE_ENUM_PREFIX: &str = "enum(";
317pub(crate) const TYPE_TMPL_PREFIX: &str = "tmpl(";
319pub(crate) const TYPE_OPTION: &str = "option";
321pub(crate) const TYPE_OPTION_PREFIX: &str = "option(";
323
324pub const OPTION_SOME: &str = "Some";
326pub const OPTION_NONE: &str = "None";
328pub const OPTION_VAL_FIELD: &str = "val";
330pub const MATCH_DEFAULT: &str = "_";
332
333pub(crate) const PREFIX_CONSTS_DOT: &str = "consts.";
337pub(crate) const PREFIX_OPTS_DOT: &str = "opts.";
339pub(crate) const PREFIX_OPTIONS_DOT: &str = "options.";
341pub(crate) const PREFIX_PARAMS_DOT: &str = "params.";
343
344pub(crate) const LIT_TRUE: &str = "true";
348pub(crate) const LIT_FALSE: &str = "false";
350
351#[must_use]
356pub fn strip_string_literal(token: &str) -> Option<&str> {
357 if token.len() >= 2
358 && ((token.starts_with(QUOTE_DOUBLE) && token.ends_with(QUOTE_DOUBLE))
359 || (token.starts_with(QUOTE_SINGLE) && token.ends_with(QUOTE_SINGLE)))
360 {
361 return Some(&token[1..token.len() - 1]);
362 }
363 None
364}
365
366pub(crate) const ERR_MISSING_FM: &str =
370 "missing mandatory YAML frontmatter block (starts with ---)";
371pub(crate) const ERR_UNCLOSED_FM: &str = "unclosed YAML frontmatter block";
373pub(crate) const ERR_UNDECLARED_PREFIX: &str = "undeclared variable(s) referenced in body: ";
375
376pub(crate) const ERR_RESERVED_KEYWORD: &str = "reserved keyword used as name";
378pub(crate) const ERR_DUPLICATE_PARAM: &str = "duplicate parameter name";
380pub(crate) const ERR_DUPLICATE_TYPE_ALIAS: &str = "duplicate type alias";
382pub(crate) const ERR_BUILTIN_SHADOW: &str = "type alias shadows built-in type name";
384pub(crate) const ERR_TYPE_PARAM_CONFLICT: &str =
386 "type alias name conflicts with parameter name (PascalCase collision)";
387#[cfg(feature = "std")]
389pub(crate) const ERR_CIRCULAR_IMPORT: &str = "circular import detected";
390pub(crate) const ERR_TYPE_SHADOWS_IMPORT: &str = "type alias shadows import alias";
392pub(crate) const ERR_PARAM_SHADOWS_IMPORT: &str =
394 "parameter name (PascalCase) shadows import alias";
395pub(crate) const ERR_UNUSED_TYPE_ALIAS: &str = "unused type alias";
396pub(crate) const ERR_DUPLICATE_CONST: &str = "duplicate constant name";
398pub(crate) const ERR_PARAM_CONST_CONFLICT: &str = "parameter name conflicts with constant name";
400pub(crate) const ERR_FOR_BINDING_SHADOWS: &str = "for loop binding shadows";
402pub(crate) const ERR_BARE_STMT_TAG: &str =
404 "statement tag at line start must be blockquote-prefixed with '> '";
405pub(crate) const ERR_COMPOUND_BRACKETS_PROHIBITED: &str =
407 "must use parentheses (...); angle brackets <...> and square brackets [...] are prohibited";
408
409pub(crate) const RESERVED_NAMES: &[&str] = &[
411 TYPE_LIST,
412 TYPE_STRUCT,
413 TYPE_ENUM,
414 TYPE_TMPL,
415 TYPE_OPTION,
416 "params",
417 TYPE_STR,
418 TYPE_INT,
419 TYPE_FLOAT,
420 TYPE_BOOL,
421];
422
423#[cfg(test)]
424mod tests {
425 use super::*;
426
427 #[test]
430 fn strip_double_quoted_string() {
431 assert_eq!(strip_string_literal("\"hello\""), Some("hello"));
432 }
433
434 #[test]
435 fn strip_single_quoted_string() {
436 assert_eq!(strip_string_literal("'world'"), Some("world"));
437 }
438
439 #[test]
440 fn strip_empty_double_quoted_string() {
441 assert_eq!(strip_string_literal("\"\""), Some(""));
442 }
443
444 #[test]
445 fn strip_empty_single_quoted_string() {
446 assert_eq!(strip_string_literal("''"), Some(""));
447 }
448
449 #[test]
450 fn strip_unquoted_string_returns_none() {
451 assert_eq!(strip_string_literal("hello"), None);
452 }
453
454 #[test]
455 fn strip_mismatched_quotes_returns_none() {
456 assert_eq!(strip_string_literal("\"hello'"), None);
457 assert_eq!(strip_string_literal("'hello\""), None);
458 }
459
460 #[test]
461 fn strip_single_quote_char_returns_none() {
462 assert_eq!(strip_string_literal("\""), None);
463 assert_eq!(strip_string_literal("'"), None);
464 }
465
466 #[test]
467 fn strip_empty_input_returns_none() {
468 assert_eq!(strip_string_literal(""), None);
469 }
470
471 #[test]
472 fn strip_quoted_string_with_spaces() {
473 assert_eq!(strip_string_literal("\"hello world\""), Some("hello world"));
474 assert_eq!(strip_string_literal("'hello world'"), Some("hello world"));
475 }
476
477 #[test]
478 fn strip_quoted_string_with_inner_quotes() {
479 assert_eq!(strip_string_literal("\"it's\""), Some("it's"));
481 assert_eq!(strip_string_literal("'say \"hi\"'"), Some("say \"hi\""));
482 }
483
484 #[test]
487 fn reserved_names_contains_type_names() {
488 for name in &[
489 "list", "struct", "enum", "tmpl", "option", "str", "int", "float", "bool",
490 ] {
491 assert!(
492 RESERVED_NAMES.contains(name),
493 "{name} should be in RESERVED_NAMES"
494 );
495 }
496 }
497
498 #[test]
499 fn reserved_names_contains_params() {
500 assert!(RESERVED_NAMES.contains(&"params"));
501 }
502
503 #[test]
506 fn builtin_functions_contains_expected_entries() {
507 assert!(BUILTIN_FUNCTIONS.contains(&"idx"));
508 assert!(BUILTIN_FUNCTIONS.contains(&"len"));
509 assert!(BUILTIN_FUNCTIONS.contains(&"kind"));
510 assert!(BUILTIN_FUNCTIONS.contains(&"kinds"));
511 assert!(BUILTIN_FUNCTIONS.contains(&"has"));
512 }
513
514 #[test]
515 fn builtin_functions_length() {
516 assert_eq!(BUILTIN_FUNCTIONS.len(), 5);
517 }
518
519 #[test]
522 fn expr_delimiters() {
523 assert_eq!(EXPR_START, "{{");
524 assert_eq!(EXPR_END, "}}");
525 }
526
527 #[test]
528 fn stmt_delimiters() {
529 assert_eq!(STMT_START, "{%");
530 assert_eq!(STMT_END, "%}");
531 }
532
533 #[test]
534 fn comment_delimiters() {
535 assert_eq!(COMMENT_START, "{#");
536 assert_eq!(COMMENT_END, "#}");
537 }
538
539 #[test]
540 fn closing_block_tags() {
541 assert_eq!(CLOSE_IF, "/if");
542 assert_eq!(CLOSE_FOR, "/for");
543 assert_eq!(CLOSE_RAW, "/raw");
544 assert_eq!(CLOSE_TMPL, "/tmpl");
545 assert_eq!(CLOSE_MATCH, "/match");
546 }
547
548 #[test]
549 fn frontmatter_delimiter() {
550 assert_eq!(FM_DELIMITER, "---");
551 }
552
553 #[test]
554 fn enum_tag_key_value() {
555 assert_eq!(ENUM_TAG_KEY, "__kind__");
556 }
557
558 #[test]
559 fn syntax_chars() {
560 assert_eq!(PAREN_OPEN, '(');
561 assert_eq!(PAREN_CLOSE, ')');
562 assert_eq!(PATH_SEP, '.');
563 assert_eq!(PIPE, '|');
564 assert_eq!(QUOTE_DOUBLE, '"');
565 assert_eq!(QUOTE_SINGLE, '\'');
566 }
567
568 #[test]
569 fn test_is_valid_include_path() {
570 assert!(is_valid_include_path("./file.tmpl.md"));
571 assert!(is_valid_include_path("../file.tmpl.md"));
572 assert!(is_valid_include_path(".\\file.tmpl.md"));
573 assert!(is_valid_include_path("..\\file.tmpl.md"));
574 assert!(is_valid_include_path("/file.tmpl.md"));
575 assert!(is_valid_include_path("{{ consts.DIR }}/file.tmpl.md"));
576 assert!(!is_valid_include_path("file.tmpl.md"));
577 assert!(!is_valid_include_path("sub/file.tmpl.md"));
578
579 assert!(is_valid_resolved_path("./file.tmpl.md"));
580 assert!(!is_valid_resolved_path("{{ consts.DIR }}/file.tmpl.md"));
581 }
582}