feat(config): wire up form match syntax

This commit is contained in:
Federico Terzi 2021-04-24 18:00:46 +02:00
parent a2522af57a
commit 3dfde8e830
2 changed files with 37 additions and 3 deletions

View File

@ -21,12 +21,13 @@ use crate::{
counter::next_id, counter::next_id,
matches::{ matches::{
group::{path::resolve_imports, MatchGroup}, group::{path::resolve_imports, MatchGroup},
Match, UpperCasingStyle, Variable, Match, UpperCasingStyle, Variable, Params, Value,
}, },
}; };
use anyhow::Result; use anyhow::Result;
use log::{error, warn}; use log::{error, warn};
use parse::YAMLMatchGroup; use parse::YAMLMatchGroup;
use regex::{Regex, Captures};
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};
use self::{ use self::{
@ -40,6 +41,10 @@ use super::Importer;
pub(crate) mod parse; pub(crate) mod parse;
mod util; mod util;
lazy_static! {
static ref VAR_REGEX: Regex = Regex::new("\\{\\{\\s*(\\w+)(\\.\\w+)?\\s*\\}\\}").unwrap();
}
pub(crate) struct YAMLImporter {} pub(crate) struct YAMLImporter {}
impl YAMLImporter { impl YAMLImporter {
@ -153,6 +158,35 @@ impl TryFrom<YAMLMatch> for Match {
replace, replace,
vars: vars?, 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 { } else {
MatchEffect::None MatchEffect::None
}; };

View File

@ -71,10 +71,10 @@ pub struct YAMLMatch {
pub image_path: Option<String>, // TODO: map pub image_path: Option<String>, // TODO: map
#[serde(default)] #[serde(default)]
pub form: Option<String>, // TODO: map pub form: Option<String>,
#[serde(default)] #[serde(default)]
pub form_fields: Option<HashMap<String, Value>>, // TODO: map pub form_fields: Option<Mapping>,
#[serde(default)] #[serde(default)]
pub vars: Option<Vec<YAMLVariable>>, pub vars: Option<Vec<YAMLVariable>>,