Skip to content

Comments

Add basic command task templating#12

Open
LinuCC wants to merge 3 commits intomasterfrom
add_basic_command_task_templating
Open

Add basic command task templating#12
LinuCC wants to merge 3 commits intomasterfrom
add_basic_command_task_templating

Conversation

@LinuCC
Copy link
Owner

@LinuCC LinuCC commented Jan 15, 2019

  • Parse the command.cmd JSON string as a CommandTask with CommandTaskVariables instead of a simple String
  • Add rendering method replacing those variables (with some kind of default, e.g. empty string)
  • Add form to input all the template variables

@LinuCC LinuCC requested a review from flodiebold January 29, 2019 22:05
.ok_or(CommandTaskReplaceValuesError)?;
let match_string = format!("\\{{\\{{{}\\}}\\}}", task_variable.name);
let re = RegexBuilder::new(&match_string).build()?;
output = re.replace_all(&output, value.as_str()).to_string();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

du kannst als replacer eine FnMut(&Captures) -> String angeben, d.h. du könntest alle variablen auf einmal abhandeln (und die gleiche regex wie weiter oben verwenden)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, in that case how would I return the error from the closure?
E.g.

    pub fn to_executable_string(<···>) -> Result<String, Box<std::error::Error>> {
        let template_regex = Regex::new(r"\{\{(.+?)\}\}").unwrap();
        output = template_regex
            .replace_all(&output, |caps: &Captures| {
                variables.get(&caps[0]).ok_or(CommandTaskReplaceValuesError)? // <- Error
            })
            .to_string();
    // <···>

Does not compile because the error now gets returned inside the closure/.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you can't... you could first check that all the variables have values, and then do the actual replacing, though

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it actually possible that variables are missing from the hash map though?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants