telchar_codegen/
template.rs

1//! This module provides functionality for initializing Handlebars templates and defining template types.
2
3use std::fmt;
4use std::str;
5use std::str::FromStr;
6use handlebars::{handlebars_helper, Handlebars};
7use super::schema;
8
9static TEMPLATE_BLAZE: &'static str = include_str!(".././templates/blaze.hbs");
10static TEMPLATE_LUCID_EVOLUTION: &'static str = include_str!(".././templates/lucid-evolution.hbs");
11static TEMPLATE_MESH: &'static str = include_str!(".././templates/mesh.hbs");
12
13/// Enum representing the different templates available.
14pub enum Template {
15    Blaze,
16    LucidEvolution,
17    Mesh,
18}
19
20impl str::FromStr for Template {
21    type Err = ();
22    fn from_str(input: &str) -> Result<Template, Self::Err> {
23        match input {
24            "blaze" => Ok(Template::Blaze),
25            "lucid-evolution" => Ok(Template::LucidEvolution),
26            "mesh" => Ok(Template::Mesh),
27            _ => Err(()),
28        }
29    }
30}
31
32impl fmt::Display for Template {
33        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34        match self {
35            Template::Blaze => write!(f, "blaze"),
36            Template::LucidEvolution => write!(f, "lucid-evolution"),
37            Template::Mesh => write!(f, "mesh"),
38        }
39    }
40}
41
42/// Helper function to check if a type name matches a specified `TypeName`.
43///
44/// # Arguments
45///
46/// * `type_name_str` - The string representation of the type name.
47/// * `type_name` - The `TypeName` to compare against.
48///
49/// # Returns
50///
51/// A boolean indicating whether the type names match.
52fn helper_is_type(type_name_str: String, type_name: schema::TypeName) -> bool {
53    schema::TypeName::from_str(&type_name_str).unwrap().eq(&type_name)
54}
55
56/// Initializes Handlebars with registered templates and helpers.
57///
58/// # Returns
59///
60/// A `Handlebars` instance with registered templates and helpers.
61pub fn init_handlebars() -> Handlebars<'static> {
62    let mut handlebars = Handlebars::new();
63
64    handlebars.register_template_string("blaze", TEMPLATE_BLAZE).unwrap();
65    handlebars.register_template_string("lucid-evolution", TEMPLATE_LUCID_EVOLUTION).unwrap();
66    handlebars.register_template_string("mesh", TEMPLATE_MESH).unwrap();
67
68    handlebars_helper!(is_anydata: |type_name: str| helper_is_type(type_name.into(), schema::TypeName::AnyData));
69    handlebars.register_helper("is_anydata", Box::new(is_anydata));
70
71    handlebars_helper!(is_integer: |type_name: str| helper_is_type(type_name.into(), schema::TypeName::Integer));
72    handlebars.register_helper("is_integer", Box::new(is_integer));
73
74    handlebars_helper!(is_bytes: |type_name: str| helper_is_type(type_name.into(), schema::TypeName::Bytes));
75    handlebars.register_helper("is_bytes", Box::new(is_bytes));
76
77    handlebars_helper!(is_literal: |type_name: str| helper_is_type(type_name.into(), schema::TypeName::Literal));
78    handlebars.register_helper("is_literal", Box::new(is_literal));
79
80    handlebars_helper!(is_nullable: |type_name: str| helper_is_type(type_name.into(), schema::TypeName::Nullable));
81    handlebars.register_helper("is_nullable", Box::new(is_nullable));
82
83    handlebars_helper!(is_object: |type_name: str| helper_is_type(type_name.into(), schema::TypeName::Object));
84    handlebars.register_helper("is_object", Box::new(is_object));
85
86    handlebars_helper!(is_enum: |type_name: str| helper_is_type(type_name.into(), schema::TypeName::Enum));
87    handlebars.register_helper("is_enum", Box::new(is_enum));
88
89    handlebars_helper!(is_tuple: |type_name: str| helper_is_type(type_name.into(), schema::TypeName::Tuple));
90    handlebars.register_helper("is_tuple", Box::new(is_tuple));
91
92    handlebars_helper!(is_list: |type_name: str| helper_is_type(type_name.into(), schema::TypeName::List));
93    handlebars.register_helper("is_list", Box::new(is_list));
94
95    handlebars
96}