Skip to content

Commit 8803e70

Browse files
fix: interpolated domain string bug in robopage function (#28)
* fix: interpolated domain string in function * fix: indentation
1 parent 9b953e3 commit 8803e70

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

src/book/runtime.rs

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -187,21 +187,23 @@ impl<'a> FunctionRef<'a> {
187187
let command_line = self.function.execution.get_command_line()?;
188188
let mut env = BTreeMap::new();
189189

190-
// interpolate the arguments
191-
let command_line = {
192-
let mut interpolated = Vec::new();
193-
for arg in command_line {
194-
interpolated.push(if let Some(caps) = ARG_VALUE_PARSER.captures(&arg) {
195-
let var_name = caps
196-
.get(1)
197-
.ok_or(ARG_EXPRESSION_ERROR)
198-
.map_err(|e| anyhow!(e))?
199-
.as_str();
190+
// interpolate the arguments
191+
let command_line = {
192+
let mut interpolated = Vec::new();
193+
for arg in command_line {
194+
if ARG_VALUE_PARSER.is_match(&arg) {
195+
// Process args with placeholders by replacing only the matched patterns
196+
let mut processed_arg = arg.clone();
197+
198+
// Find all matches and collect the replacements
199+
let mut replacements = Vec::new();
200+
for caps in ARG_VALUE_PARSER.captures_iter(&arg) {
201+
let full_match = caps.get(0).unwrap().as_str();
202+
let var_name = caps.get(1).ok_or(ARG_EXPRESSION_ERROR).map_err(| e| anyhow!(e))?.as_str();
200203
let var_default = caps.get(3).map(|m| m.as_str());
201204

202-
// read variable from the environment if the name starts with env. or ENV.
203-
if var_name.starts_with("env.") || var_name.starts_with("ENV.") {
204-
let env_var_name = var_name.replace("env.", "").replace("ENV.", "");
205+
let replacement = if var_name.starts_with("env.") || var_name. starts_with("ENV.") {
206+
let env_var_name = var_name.replace("env.", "").replace ("ENV.", "");
205207
let env_var = std::env::var(&env_var_name);
206208
let env_var_value = if let Ok(value) = env_var {
207209
value
@@ -214,39 +216,46 @@ impl<'a> FunctionRef<'a> {
214216
));
215217
};
216218

217-
// add the environment variable to the command line for later use
219+
// add the environment variable to the command line for later use
218220
env.insert(env_var_name, env_var_value.to_owned());
219221

220222
env_var_value
221223
} else if let Some(value) = arguments.get(var_name) {
222-
// if the value is empty and there's a default value, use the default value
223224
if value.is_empty() {
224225
if let Some(def) = var_default {
225226
def.to_string()
226227
} else {
227228
value.to_string()
228229
}
229230
} else {
230-
// otherwise, use the provided value
231231
value.to_string()
232232
}
233233
} else if let Some(default_value) = var_default {
234-
// if the value is not provided and there's a default value, use the default value
235234
default_value.to_string()
236235
} else {
237-
return Err(anyhow::anyhow!("argument {} not provided", var_name));
238-
}
239-
} else {
240-
arg
241-
});
242-
}
243-
interpolated
244-
};
236+
return Err(anyhow::anyhow!("argument {} not provided", var_name));
237+
};
245238

246-
// final parsing
247-
CommandLine::from_vec_with_env(&command_line, env)
239+
replacements.push((full_match, replacement));
240+
}
241+
242+
// Apply all replacements to the arg string
243+
for (pattern, replacement) in replacements {
244+
processed_arg = processed_arg.replace(pattern, &replacement);
245+
}
246+
247+
interpolated.push(processed_arg);
248+
} else {
249+
// For args without placeholders, use as-is
250+
interpolated.push(arg);
251+
}
252+
}
253+
interpolated
254+
};
255+
// final parsing
256+
CommandLine::from_vec_with_env(&command_line, env)
257+
}
248258
}
249-
}
250259

251260
#[cfg(test)]
252261
mod tests {

0 commit comments

Comments
 (0)