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