11/// Configuration file format for floki
2- use crate :: errors;
2+ use crate :: errors:: FlokiError ;
33use crate :: image;
4- use anyhow:: Error ;
54use serde:: { Deserialize , Serialize } ;
65use tera:: from_value;
76use tera:: Context ;
@@ -126,7 +125,7 @@ fn tomlloader(args: &HashMap<String, tera::Value>) -> tera::Result<tera::Value>
126125}
127126
128127// Renders a template from a given string.
129- pub fn render_template ( template : & str , source_filename : & Path ) -> Result < String , Error > {
128+ pub fn render_template ( template : & str , source_filename : & Path ) -> Result < String , FlokiError > {
130129 let template_path = source_filename. display ( ) . to_string ( ) ;
131130
132131 debug ! ( "Rendering template: {template_path}" ) ;
@@ -140,7 +139,7 @@ pub fn render_template(template: &str, source_filename: &Path) -> Result<String,
140139 tera. register_function ( "toml" , tomlloader) ;
141140
142141 tera. add_raw_template ( & template_path, template)
143- . map_err ( |e| errors :: FlokiError :: ProblemRenderingTemplate {
142+ . map_err ( |e| FlokiError :: ProblemRenderingTemplate {
144143 name : template_path. clone ( ) ,
145144 error : e,
146145 } ) ?;
@@ -152,35 +151,37 @@ pub fn render_template(template: &str, source_filename: &Path) -> Result<String,
152151 context. insert ( "env" , & vars) ;
153152
154153 // Render the floki file to string using the context.
155- Ok ( tera. render ( & template_path, & context) ?)
154+ tera. render ( & template_path, & context)
155+ . map_err ( |e| FlokiError :: ProblemRenderingTemplate {
156+ name : template_path. clone ( ) ,
157+ error : e,
158+ } )
156159}
157160
158161impl FlokiConfig {
159- pub fn render ( file : & Path ) -> Result < String , Error > {
160- let content = std :: fs :: read_to_string ( file ) . map_err ( |e| {
161- errors :: FlokiError :: ProblemOpeningConfigYaml {
162+ pub fn render ( file : & Path ) -> Result < String , FlokiError > {
163+ let content =
164+ std :: fs :: read_to_string ( file ) . map_err ( |e| FlokiError :: ProblemOpeningConfigYaml {
162165 name : file. display ( ) . to_string ( ) ,
163166 error : e,
164- }
165- } ) ?;
167+ } ) ?;
166168
167169 // Render the template first before parsing it.
168170 render_template ( & content, file)
169171 }
170172
171- pub fn from_file ( file : & Path ) -> Result < Self , Error > {
173+ pub fn from_file ( file : & Path ) -> Result < Self , FlokiError > {
172174 debug ! ( "Reading configuration file: {:?}" , file) ;
173175
174176 // Render the output from the configuration file before parsing.
175177 let output = Self :: render ( file) ?;
176178
177179 // Parse the rendered floki file from the string.
178- let mut config: FlokiConfig = serde_yaml :: from_str ( & output ) . map_err ( |e| {
179- errors :: FlokiError :: ProblemParsingConfigYaml {
180+ let mut config: FlokiConfig =
181+ serde_yaml :: from_str ( & output ) . map_err ( |e| FlokiError :: ProblemParsingConfigYaml {
180182 name : file. display ( ) . to_string ( ) ,
181183 error : e,
182- }
183- } ) ?;
184+ } ) ?;
184185
185186 // Ensure the path to an external yaml file is correct.
186187 // If the image.yaml.path file is relative, then it should
@@ -191,7 +192,7 @@ impl FlokiConfig {
191192 if yaml. file . is_relative ( ) {
192193 yaml. file = file
193194 . parent ( )
194- . ok_or_else ( || errors :: FlokiInternalError :: InternalAssertionFailed {
195+ . ok_or_else ( || FlokiError :: InternalAssertionFailed {
195196 description : format ! (
196197 "could not construct path to external yaml file '{:?}'" ,
197198 & yaml. file
0 commit comments