@@ -49,12 +49,7 @@ impl RuntimeConfig {
49
49
/// later-loaded file take precedence over any earlier-loaded files.
50
50
pub fn merge_config_file ( & mut self , path : impl Into < PathBuf > ) -> Result < ( ) > {
51
51
let path = path. into ( ) ;
52
- let bytes = fs:: read ( & path) . with_context ( || {
53
- format ! ( "Failed to load runtime config file {}" , quoted_path( & path) )
54
- } ) ?;
55
- let mut opts: RuntimeConfigOpts = toml:: from_slice ( & bytes) . with_context ( || {
56
- format ! ( "Failed to parse runtime config file {}" , quoted_path( & path) )
57
- } ) ?;
52
+ let mut opts = RuntimeConfigOpts :: parse_file ( & path) ?;
58
53
opts. file_path = Some ( path) ;
59
54
self . files . push ( opts) ;
60
55
Ok ( ( ) )
@@ -221,6 +216,30 @@ pub struct RuntimeConfigOpts {
221
216
pub file_path : Option < PathBuf > ,
222
217
}
223
218
219
+ impl RuntimeConfigOpts {
220
+ fn parse_file ( path : & Path ) -> Result < Self > {
221
+ let contents = fs:: read_to_string ( path)
222
+ . with_context ( || format ! ( "Failed to read runtime config file {}" , quoted_path( path) ) ) ?;
223
+ let ext = path. extension ( ) . unwrap_or_default ( ) ;
224
+ let is_json = ext != "toml" && ( ext == "json" || contents. trim_start ( ) . starts_with ( '{' ) ) ;
225
+ if is_json {
226
+ serde_json:: from_str ( & contents) . with_context ( || {
227
+ format ! (
228
+ "Failed to parse runtime config JSON file {}" ,
229
+ quoted_path( path)
230
+ )
231
+ } )
232
+ } else {
233
+ toml:: from_str ( & contents) . with_context ( || {
234
+ format ! (
235
+ "Failed to parse runtime config TOML file {}" ,
236
+ quoted_path( path)
237
+ )
238
+ } )
239
+ }
240
+ }
241
+ }
242
+
224
243
fn resolve_config_path ( path : & Path , config_opts : & RuntimeConfigOpts ) -> Result < PathBuf > {
225
244
if path. is_absolute ( ) {
226
245
return Ok ( path. to_owned ( ) ) ;
0 commit comments