Skip to content

Commit

Permalink
feat(config): support rejects incomplete syntax like ${var:-}
Browse files Browse the repository at this point in the history
  • Loading branch information
indirection42 committed Apr 23, 2024
1 parent be8158f commit 1d1a561
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,11 @@ pub fn read_config(path: impl AsRef<path::Path>) -> Result<Config, anyhow::Error
}

fn render_template(templated_config_str: &str) -> Result<String, anyhow::Error> {
// match pattern: ${SOME_VAR}, ${SOME_VAR:-word}, or ${SOME_VAR:+word}
// TODO: Partial syntax like ${SOME_VAR:-} and ${SOME_VAR:+} should be invalid, but it's not supported yet
let re = Regex::new(r"\$\{([^\}:-]+)(?:(:-|:\+)([^\}]*))?\}").unwrap();
// match pattern with 1 group: {variable_name}
// match pattern with 3 groups: {variable:-word} or {variable:+word}
// note: incompete syntax like {variable:-} will be matched since group1 is ungreedy match
// but typically it will be rejected due to there is not corresponding env vars
let re = Regex::new(r"\$\{([^}]+?)(?:(:-|:\+)([^}]+))?\}").unwrap();

let mut config_str = String::with_capacity(templated_config_str.len());
let mut last_match = 0;
Expand Down Expand Up @@ -276,8 +278,7 @@ mod tests {
}

#[test]
#[ignore = "not supported yet"]
fn render_template_gets_error_when_syntax_is_partial() {
fn render_template_gets_error_when_syntax_is_incomplete() {
let templated_config_str = "${variable:-}";
let config_str = render_template(templated_config_str);
assert!(config_str.is_err());
Expand Down

0 comments on commit 1d1a561

Please sign in to comment.