From 9fb1d2a22a75ab3f264a2bc9eae314de390c4752 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 7 Nov 2021 15:43:24 +0100 Subject: [PATCH] feat(config): add inject_vars option to matches. #856 --- espanso-config/src/legacy/mod.rs | 2 +- .../src/matches/group/loader/yaml/mod.rs | 23 +++++++++++++------ .../src/matches/group/loader/yaml/parse.rs | 3 +++ espanso-config/src/matches/mod.rs | 2 ++ 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/espanso-config/src/legacy/mod.rs b/espanso-config/src/legacy/mod.rs index 7c3126e..8347c8e 100644 --- a/espanso-config/src/legacy/mod.rs +++ b/espanso-config/src/legacy/mod.rs @@ -89,7 +89,7 @@ fn split_config(config: LegacyConfig) -> (LegacyInteropConfig, LegacyMatchGroup) .iter() .filter_map(|var| { let var: YAMLVariable = serde_yaml::from_value(var.clone()).ok()?; - let (var, warnings) = try_convert_into_variable(var).ok()?; + let (var, warnings) = try_convert_into_variable(var, true).ok()?; warnings.into_iter().for_each(|warning| { warn!("{}", warning); }); diff --git a/espanso-config/src/matches/group/loader/yaml/mod.rs b/espanso-config/src/matches/group/loader/yaml/mod.rs index 6e1e3d1..785cea3 100644 --- a/espanso-config/src/matches/group/loader/yaml/mod.rs +++ b/espanso-config/src/matches/group/loader/yaml/mod.rs @@ -74,7 +74,7 @@ impl Importer for YAMLImporter { let mut global_vars = Vec::new(); for yaml_global_var in yaml_group.global_vars.as_ref().cloned().unwrap_or_default() { - match try_convert_into_variable(yaml_global_var) { + match try_convert_into_variable(yaml_global_var, false) { Ok((var, warnings)) => { global_vars.push(var); non_fatal_errors.extend(warnings.into_iter().map(ErrorRecord::warn)); @@ -208,8 +208,9 @@ pub fn try_convert_into_match( let mut vars: Vec = Vec::new(); for yaml_var in yaml_match.vars.unwrap_or_default() { - let (var, var_warnings) = try_convert_into_variable(yaml_var.clone()) - .with_context(|| format!("failed to load variable: {:?}", yaml_var))?; + let (var, var_warnings) = + try_convert_into_variable(yaml_var.clone(), use_compatibility_mode) + .with_context(|| format!("failed to load variable: {:?}", yaml_var))?; warnings.extend(var_warnings); vars.push(var); } @@ -270,6 +271,7 @@ pub fn try_convert_into_match( name: "form1".to_owned(), var_type: "form".to_owned(), params, + ..Default::default() }]; MatchEffect::Text(TextEffect { @@ -303,13 +305,17 @@ pub fn try_convert_into_match( )) } -pub fn try_convert_into_variable(yaml_var: YAMLVariable) -> Result<(Variable, Vec)> { +pub fn try_convert_into_variable( + yaml_var: YAMLVariable, + use_compatibility_mode: bool, +) -> Result<(Variable, Vec)> { Ok(( Variable { name: yaml_var.name, var_type: yaml_var.var_type, params: convert_params(yaml_var.params)?, id: next_id(), + inject_vars: !use_compatibility_mode && yaml_var.inject_vars.unwrap_or(true), }, Vec::new(), )) @@ -615,7 +621,8 @@ mod tests { id: 0, name: "form1".to_string(), var_type: "form".to_string(), - params + params, + ..Default::default() }], ..Default::default() }), @@ -651,7 +658,8 @@ mod tests { id: 0, name: "form1".to_string(), var_type: "form".to_string(), - params + params, + ..Default::default() }], ..Default::default() }), @@ -689,7 +697,8 @@ mod tests { id: 0, name: "form1".to_string(), var_type: "form".to_string(), - params + params, + ..Default::default() }], ..Default::default() }), diff --git a/espanso-config/src/matches/group/loader/yaml/parse.rs b/espanso-config/src/matches/group/loader/yaml/parse.rs index 39a18fa..52ca447 100644 --- a/espanso-config/src/matches/group/loader/yaml/parse.rs +++ b/espanso-config/src/matches/group/loader/yaml/parse.rs @@ -125,6 +125,9 @@ pub struct YAMLVariable { #[serde(default = "default_params")] pub params: Mapping, + + #[serde(default)] + pub inject_vars: Option, } fn default_params() -> Mapping { diff --git a/espanso-config/src/matches/mod.rs b/espanso-config/src/matches/mod.rs index 7e3fb13..5d74bdd 100644 --- a/espanso-config/src/matches/mod.rs +++ b/espanso-config/src/matches/mod.rs @@ -205,6 +205,7 @@ pub struct Variable { pub name: String, pub var_type: String, pub params: Params, + pub inject_vars: bool, } impl Default for Variable { @@ -214,6 +215,7 @@ impl Default for Variable { name: String::new(), var_type: String::new(), params: Params::new(), + inject_vars: true, } } }