@@ -3,7 +3,6 @@ use std::path::Path;
33use std:: sync:: Arc ;
44use std:: sync:: RwLock ;
55
6-
76/// Template engine for AIScript
87pub struct TemplateEngine {
98 env : RwLock < Environment < ' static > > ,
@@ -19,7 +18,7 @@ impl TemplateEngine {
1918 let path = std:: path:: Path :: new ( "templates" ) . join ( name) ;
2019 match std:: fs:: read_to_string ( path) {
2120 Ok ( content) => Ok ( Some ( content) ) ,
22- Err ( _) => Ok ( None )
21+ Err ( _) => Ok ( None ) ,
2322 }
2423 } ) ;
2524
@@ -29,41 +28,47 @@ impl TemplateEngine {
2928 }
3029
3130 /// Render a template with the given context
32- pub fn render ( & self , template_name : & str , context : & serde_json:: Value ) -> Result < String , String > {
31+ pub fn render (
32+ & self ,
33+ template_name : & str ,
34+ context : & serde_json:: Value ,
35+ ) -> Result < String , String > {
3336 let env = self . env . read ( ) . unwrap ( ) ;
34-
35- // 获取模板
36- let template = env. get_template ( template_name)
37+
38+ // get the template
39+ let template = env
40+ . get_template ( template_name)
3741 . map_err ( |e| format ! ( "Failed to load template '{}': {}" , template_name, e) ) ?;
38-
39- // 渲染模板并返回结果
40- template. render ( context)
42+
43+ // render the template and return the result
44+ template
45+ . render ( context)
4146 . map_err ( |e| format ! ( "Failed to render template '{}': {}" , template_name, e) )
4247 }
4348
4449 /// Reload the templates
4550 pub fn reload ( & self ) -> Result < ( ) , String > {
46- let mut env = self . env . write ( ) . unwrap ( ) ;
51+ let mut env = self . env . write ( ) . unwrap ( ) ;
4752
48- //reload templates
53+ //reload templates
4954 env. set_loader ( |name| -> Result < Option < String > , minijinja:: Error > {
5055 let path = std:: path:: Path :: new ( "templates" ) . join ( name) ;
5156 match std:: fs:: read_to_string ( path) {
5257 Ok ( content) => Ok ( Some ( content) ) ,
53- Err ( _) => Ok ( None )
58+ Err ( _) => Ok ( None ) ,
5459 }
5560 } ) ;
5661
5762 Ok ( ( ) )
5863 }
5964}
6065
61- //Create a global instance of the template engine
66+ //Create a global instance of the template engine
6267lazy_static:: lazy_static! {
6368 static ref TEMPLATE_ENGINE : TemplateEngine = TemplateEngine :: new( ) ;
6469}
6570
66- //Get the template engine instance
71+ //Get the template engine instance
6772pub fn get_template_engine ( ) -> & ' static TemplateEngine {
6873 & TEMPLATE_ENGINE
69- }
74+ }
0 commit comments