Skip to main content

nargo_template/
engine.rs

1#![warn(missing_docs)]
2
3use serde::Serialize;
4
5/// 模板引擎类型
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
7pub enum TemplateEngine {
8    /// 自定义模板引擎
9    Custom,
10
11    #[cfg(feature = "handlebars")]
12    /// Handlebars 模板引擎
13    Handlebars,
14
15    #[cfg(feature = "dejavu")]
16    /// DejaVu 模板引擎
17    DejaVu,
18
19    #[cfg(feature = "jinja")]
20    /// Jinja2 模板引擎
21    Jinja2,
22
23    #[cfg(feature = "liquid")]
24    /// Liquid 模板引擎
25    Liquid,
26}
27
28impl TemplateEngine {
29    /// 获取所有可用的模板引擎
30    pub fn all() -> &'static [Self] {
31        #[cfg(all(not(feature = "handlebars"), not(feature = "dejavu"), not(feature = "jinja"), not(feature = "liquid")))]
32        {
33            &[Self::Custom]
34        }
35
36        #[cfg(any(feature = "handlebars", feature = "dejavu", feature = "jinja", feature = "liquid"))]
37        {
38            &[
39                Self::Custom,
40                #[cfg(feature = "handlebars")] Self::Handlebars,
41                #[cfg(feature = "dejavu")] Self::DejaVu,
42                #[cfg(feature = "jinja")] Self::Jinja2,
43                #[cfg(feature = "liquid")] Self::Liquid,
44            ]
45        }
46    }
47
48    /// 获取模板引擎的显示名称
49    pub fn display_name(&self) -> &'static str {
50        match self {
51            Self::Custom => "自定义",
52            #[cfg(feature = "handlebars")]
53            Self::Handlebars => "Handlebars",
54            #[cfg(feature = "dejavu")]
55            Self::DejaVu => "DejaVu",
56            #[cfg(feature = "jinja")]
57            Self::Jinja2 => "Jinja2",
58            #[cfg(feature = "liquid")]
59            Self::Liquid => "Liquid",
60        }
61    }
62
63    /// 获取模板引擎的描述
64    pub fn description(&self) -> &'static str {
65        match self {
66            Self::Custom => "自定义模板引擎",
67            #[cfg(feature = "handlebars")]
68            Self::Handlebars => "Handlebars 模板引擎,流行的 JavaScript 模板语言",
69            #[cfg(feature = "dejavu")]
70            Self::DejaVu => "DejaVu 模板引擎,编译期零成本抽象",
71            #[cfg(feature = "jinja")]
72            Self::Jinja2 => "Jinja2 模板引擎,Python 生态系统中流行的模板语言",
73            #[cfg(feature = "liquid")]
74            Self::Liquid => "Liquid 模板引擎,Shopify 开发的模板语言",
75        }
76    }
77
78    /// 获取默认的文件扩展名
79    pub fn default_extension(&self) -> &'static str {
80        match self {
81            Self::Custom => "tpl",
82            #[cfg(feature = "handlebars")]
83            Self::Handlebars => "hbs",
84            #[cfg(feature = "dejavu")]
85            Self::DejaVu => "dejavu",
86            #[cfg(feature = "jinja")]
87            Self::Jinja2 => "jinja2",
88            #[cfg(feature = "liquid")]
89            Self::Liquid => "liquid",
90        }
91    }
92}