@@ -99,28 +99,32 @@ pub(crate) struct FlokiConfig {
9999 pub ( crate ) entrypoint : Entrypoint ,
100100}
101101
102- fn yamlloader ( args : & HashMap < String , tera:: Value > ) -> tera:: Result < tera :: Value > {
102+ fn path_from_args ( args : & HashMap < String , tera:: Value > ) -> tera:: Result < String > {
103103 let file = match args. get ( "file" ) {
104104 Some ( file) => file,
105105 None => return Err ( "file parameter is required" . into ( ) ) ,
106106 } ;
107+ Ok ( from_value :: < String > ( file. clone ( ) ) ?)
108+ }
107109
108- let path = from_value :: < String > ( file. clone ( ) ) ?;
110+ fn yamlloader ( args : & HashMap < String , tera:: Value > ) -> tera:: Result < tera:: Value > {
111+ let path = path_from_args ( args) ?;
109112 let f = std:: fs:: File :: open ( path) ?;
110113 serde_yaml:: from_reader ( f) . map_err ( |_| "Failed to read file" . into ( ) )
111114}
112115
113116fn jsonloader ( args : & HashMap < String , tera:: Value > ) -> tera:: Result < tera:: Value > {
114- let file = match args. get ( "file" ) {
115- Some ( file) => file,
116- None => return Err ( "file parameter is required" . into ( ) ) ,
117- } ;
118-
119- let path = from_value :: < String > ( file. clone ( ) ) ?;
117+ let path = path_from_args ( args) ?;
120118 let f = std:: fs:: File :: open ( path) ?;
121119 serde_json:: from_reader ( f) . map_err ( |_| "Failed to read file" . into ( ) )
122120}
123121
122+ fn tomlloader ( args : & HashMap < String , tera:: Value > ) -> tera:: Result < tera:: Value > {
123+ let path = path_from_args ( args) ?;
124+ let contents = std:: fs:: read_to_string ( path) ?;
125+ toml:: from_str ( & contents) . map_err ( |_| "Failed to read file" . into ( ) )
126+ }
127+
124128// Renders a template from a given string.
125129pub fn render_template ( template : & str , source_filename : & Path ) -> Result < String , Error > {
126130 let template_path = source_filename. display ( ) . to_string ( ) ;
@@ -130,9 +134,10 @@ pub fn render_template(template: &str, source_filename: &Path) -> Result<String,
130134 // Read the template using tera
131135 let mut tera = Tera :: default ( ) ;
132136
133- // Allow templates to load yaml and json files as Values.
137+ // Allow templates to load variables files as Values.
134138 tera. register_function ( "yamlload" , yamlloader) ;
135139 tera. register_function ( "jsonload" , jsonloader) ;
140+ tera. register_function ( "tomlload" , tomlloader) ;
136141
137142 tera. add_raw_template ( & template_path, template)
138143 . map_err ( |e| errors:: FlokiError :: ProblemRenderingTemplate {
@@ -328,4 +333,13 @@ mod test {
328333 assert_eq ! ( config, "shell: bar" ) ;
329334 Ok ( ( ) )
330335 }
336+
337+ #[ test]
338+ fn test_tera_tomlload ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
339+ let template =
340+ r#"{% set values = tomlload(file="Cargo.toml") %}floki: {{ values.package.name }}"# ;
341+ let config = render_template ( template, Path :: new ( "floki" ) ) ?;
342+ assert_eq ! ( config, "floki: floki" ) ;
343+ Ok ( ( ) )
344+ }
331345}
0 commit comments