From 3dfde8e830f56611dc0ee914740cd8da78aec189 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sat, 24 Apr 2021 18:00:46 +0200 Subject: [PATCH] feat(config): wire up form match syntax --- .../src/matches/group/loader/yaml/mod.rs | 36 ++++++++++++++++++- .../src/matches/group/loader/yaml/parse.rs | 4 +-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/espanso-config/src/matches/group/loader/yaml/mod.rs b/espanso-config/src/matches/group/loader/yaml/mod.rs index e99580c..ccef02c 100644 --- a/espanso-config/src/matches/group/loader/yaml/mod.rs +++ b/espanso-config/src/matches/group/loader/yaml/mod.rs @@ -21,12 +21,13 @@ use crate::{ counter::next_id, matches::{ group::{path::resolve_imports, MatchGroup}, - Match, UpperCasingStyle, Variable, + Match, UpperCasingStyle, Variable, Params, Value, }, }; use anyhow::Result; use log::{error, warn}; use parse::YAMLMatchGroup; +use regex::{Regex, Captures}; use std::convert::{TryFrom, TryInto}; use self::{ @@ -40,6 +41,10 @@ use super::Importer; pub(crate) mod parse; mod util; +lazy_static! { + static ref VAR_REGEX: Regex = Regex::new("\\{\\{\\s*(\\w+)(\\.\\w+)?\\s*\\}\\}").unwrap(); +} + pub(crate) struct YAMLImporter {} impl YAMLImporter { @@ -153,6 +158,35 @@ impl TryFrom for Match { replace, vars: vars?, }) + } else if let Some(form_layout) = yaml_match.form { // TODO: test form case + // Replace all the form fields with actual variables + let resolved_layout = VAR_REGEX.replace_all(&form_layout, |caps: &Captures| { + let var_name = caps.get(1).unwrap().as_str(); + format!("{{{{form1.{}}}}}", var_name) + }).to_string(); + + // Convert escaped brakets in forms + let resolved_layout = resolved_layout.replace("\\{", "{ ").replace("\\}", " }"); + + // Convert the form data to valid variables + let mut params = Params::new(); + params.insert("layout".to_string(), Value::String(form_layout)); + + if let Some(fields) = yaml_match.form_fields { + params.insert("fields".to_string(), Value::Object(convert_params(fields)?)); + } + + let vars = vec![Variable { + id: next_id(), + name: "form1".to_owned(), + var_type: "form".to_owned(), + params, + }]; + + MatchEffect::Text(TextEffect { + replace: resolved_layout, + vars, + }) } else { MatchEffect::None }; diff --git a/espanso-config/src/matches/group/loader/yaml/parse.rs b/espanso-config/src/matches/group/loader/yaml/parse.rs index c2039bd..0bb73bd 100644 --- a/espanso-config/src/matches/group/loader/yaml/parse.rs +++ b/espanso-config/src/matches/group/loader/yaml/parse.rs @@ -71,10 +71,10 @@ pub struct YAMLMatch { pub image_path: Option, // TODO: map #[serde(default)] - pub form: Option, // TODO: map + pub form: Option, #[serde(default)] - pub form_fields: Option>, // TODO: map + pub form_fields: Option, #[serde(default)] pub vars: Option>,