Skip to content

Commit ae1d3e6

Browse files
authored
Merge pull request #2427 from lann/json-runtime-config
2 parents 96342e2 + ef038cc commit ae1d3e6

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

crates/trigger/src/runtime_config.rs

+25-6
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ impl RuntimeConfig {
4949
/// later-loaded file take precedence over any earlier-loaded files.
5050
pub fn merge_config_file(&mut self, path: impl Into<PathBuf>) -> Result<()> {
5151
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)?;
5853
opts.file_path = Some(path);
5954
self.files.push(opts);
6055
Ok(())
@@ -221,6 +216,30 @@ pub struct RuntimeConfigOpts {
221216
pub file_path: Option<PathBuf>,
222217
}
223218

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+
224243
fn resolve_config_path(path: &Path, config_opts: &RuntimeConfigOpts) -> Result<PathBuf> {
225244
if path.is_absolute() {
226245
return Ok(path.to_owned());

0 commit comments

Comments
 (0)