feat(config): implement depends_on field for variables

This commit is contained in:
Federico Terzi 2021-11-10 23:22:53 +01:00
parent 8acca4a366
commit aa26f27ed9
3 changed files with 52 additions and 0 deletions

View File

@ -316,6 +316,7 @@ pub fn try_convert_into_variable(
params: convert_params(yaml_var.params)?,
id: next_id(),
inject_vars: !use_compatibility_mode && yaml_var.inject_vars.unwrap_or(true),
depends_on: yaml_var.depends_on,
},
Vec::new(),
))
@ -745,6 +746,52 @@ mod tests {
)
}
#[test]
fn vars_inject_vars_and_depends_on() {
let vars = vec![
Variable {
name: "var1".to_string(),
var_type: "test".to_string(),
depends_on: vec!["test".to_owned()],
..Default::default()
},
Variable {
name: "var2".to_string(),
var_type: "test".to_string(),
inject_vars: false,
..Default::default()
},
];
assert_eq!(
create_match(
r#"
trigger: "Hello"
replace: "world"
vars:
- name: var1
type: test
depends_on: ["test"]
- name: var2
type: "test"
inject_vars: false
"#
)
.unwrap(),
Match {
cause: MatchCause::Trigger(TriggerCause {
triggers: vec!["Hello".to_string()],
..Default::default()
}),
effect: MatchEffect::Text(TextEffect {
replace: "world".to_string(),
vars,
..Default::default()
}),
..Default::default()
}
)
}
#[test]
fn vars_no_params_maps_correctly() {
let vars = vec![Variable {

View File

@ -128,6 +128,9 @@ pub struct YAMLVariable {
#[serde(default)]
pub inject_vars: Option<bool>,
#[serde(default)]
pub depends_on: Vec<String>,
}
fn default_params() -> Mapping {

View File

@ -206,6 +206,7 @@ pub struct Variable {
pub var_type: String,
pub params: Params,
pub inject_vars: bool,
pub depends_on: Vec<String>,
}
impl Default for Variable {
@ -216,6 +217,7 @@ impl Default for Variable {
var_type: String::new(),
params: Params::new(),
inject_vars: true,
depends_on: Vec::new(),
}
}
}