-
Notifications
You must be signed in to change notification settings - Fork 1
Add basic command task templating #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,3 +13,4 @@ serde_json = "1.0" | |
| find_folder = "0.3.0" | ||
| directories = "1.0" | ||
| structopt = "0.2" | ||
| regex = "1" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,9 @@ | ||
| use std::collections::HashMap; | ||
| use std::fmt::{self, Display}; | ||
| use std::str::FromStr; | ||
|
|
||
| use regex::{Regex, RegexBuilder}; | ||
| use serde::de; | ||
| use serde_derive::Deserialize; | ||
|
|
||
| use crate::bindings::Shortcut; | ||
|
|
@@ -6,15 +12,15 @@ use crate::bindings::Shortcut; | |
| pub struct CommandNode { | ||
| pub shortcut: Shortcut, | ||
| pub name: String, | ||
| pub cmd: Option<String>, | ||
| pub cmd: Option<CommandTask>, | ||
| pub children: Vec<Command>, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Deserialize)] | ||
| pub struct CommandLeaf { | ||
| pub shortcut: Shortcut, | ||
| pub name: String, | ||
| pub cmd: String, | ||
| pub cmd: CommandTask, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Deserialize)] | ||
|
|
@@ -24,6 +30,107 @@ pub enum Command { | |
| Leaf(CommandLeaf), | ||
| } | ||
|
|
||
| #[derive(Copy, Clone)] | ||
| pub struct CommandTaskParseError; | ||
| impl Display for CommandTaskParseError { | ||
LinuCC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "Failed to parse a command task") | ||
| } | ||
| } | ||
| impl fmt::Debug for CommandTaskParseError { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "Failed to parse a command task") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The executable part of a command. | ||
| */ | ||
| #[derive(Debug, Clone)] | ||
| pub struct CommandTask { | ||
| pub base: String, | ||
| pub variables: Vec<CommandTaskVariable>, | ||
| } | ||
|
|
||
| impl Display for CommandTask { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "{}", self.base) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct CommandTaskReplaceValuesError; | ||
| impl std::error::Error for CommandTaskReplaceValuesError {} | ||
| impl fmt::Display for CommandTaskReplaceValuesError { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!( | ||
| f, | ||
| "Could not replace the placeholders in the CommandTask string" | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for CommandTask { | ||
| type Err = CommandTaskParseError; | ||
|
|
||
| fn from_str(value: &str) -> Result<Self, Self::Err> { | ||
| let template_regex = Regex::new(r"\{\{(.+?)\}\}").unwrap(); | ||
|
|
||
| let variables = template_regex | ||
| .captures_iter(value) | ||
| .map(|template_data| { | ||
| CommandTaskVariable { | ||
| name: template_data[1].into(), | ||
| // TODO Add default value parsing | ||
| default_value: None, | ||
| } | ||
| }) | ||
| .collect(); | ||
|
|
||
| Ok(CommandTask { | ||
| base: value.into(), | ||
| variables: variables, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl CommandTask { | ||
| pub fn to_executable_string( | ||
| &self, | ||
| variables: &HashMap<String, String>, | ||
| ) -> Result<String, Box<std::error::Error>> { | ||
| let mut output = self.base.clone(); | ||
| for task_variable in &self.variables { | ||
| let value = variables | ||
| .get(&task_variable.name) | ||
| .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(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. du kannst als replacer eine
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? 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/.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| } | ||
| Ok(output) | ||
| } | ||
| } | ||
|
|
||
| impl<'de> de::Deserialize<'de> for CommandTask { | ||
| fn deserialize<D>(deserializer: D) -> Result<CommandTask, D::Error> | ||
| where | ||
| D: de::Deserializer<'de>, | ||
| { | ||
| let s = String::deserialize(deserializer)?; | ||
| FromStr::from_str(&s).map_err(de::Error::custom) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A variable value in a command task, to be filled in e.g. by a form | ||
| */ | ||
| #[derive(Debug, Clone, Deserialize)] | ||
| pub struct CommandTaskVariable { | ||
| pub name: String, | ||
| pub default_value: Option<String>, | ||
| } | ||
|
|
||
| /** | ||
| * Easily displayable command | ||
| */ | ||
|
|
@@ -55,7 +162,7 @@ impl From<CommandLeaf> for CommandDisplay { | |
| fn from(node: CommandLeaf) -> Self { | ||
| CommandDisplay { | ||
| shortcut: node.shortcut, | ||
| name: node.name, | ||
| name: node.name.to_string(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.