summaryrefslogtreecommitdiff
path: root/src/bindings.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-07-20 15:47:04 +0300
committerLars Wirzenius <liw@liw.fi>2021-07-20 15:47:40 +0300
commit258ee4398bde99009663ff23521cbfd8324312a0 (patch)
tree0ef9f779477eb2a8b02161ce1255e5283a6c69db /src/bindings.rs
parent08063f6d7241817cf1a04367be25b0c43202c15b (diff)
downloadsubplot-258ee4398bde99009663ff23521cbfd8324312a0.tar.gz
feat: when more than one bindings match a step, list all of them
Sponsored-by: author
Diffstat (limited to 'src/bindings.rs')
-rw-r--r--src/bindings.rs28
1 files changed, 18 insertions, 10 deletions
diff --git a/src/bindings.rs b/src/bindings.rs
index d17460f..2e927cd 100644
--- a/src/bindings.rs
+++ b/src/bindings.rs
@@ -1,4 +1,5 @@
use super::MatchedStep;
+use super::MatchedSteps;
use super::PartialStep;
use super::ScenarioStep;
use super::StepKind;
@@ -170,7 +171,7 @@ impl Binding {
let caps = self.regex.captures(step_text)?;
// If there is only one capture, it's the whole string.
- let mut m = MatchedStep::new(self.kind(), &self.function, self.cleanup(), &self.types);
+ let mut m = MatchedStep::new(self, &self.types);
if caps.len() == 1 {
m.append_part(PartialStep::uncaptured(step_text));
return Some(m);
@@ -480,16 +481,23 @@ impl Bindings {
/// Find the binding matching a given scenario step, if there is
/// exactly one.
pub fn find(&self, step: &ScenarioStep) -> Result<MatchedStep> {
- let mut matches = self
+ let mut matches: Vec<MatchedStep> = self
.bindings()
.iter()
- .filter_map(|b| b.match_with_step(step));
- match matches.next() {
- None => Err(SubplotError::BindingUnknown(step.to_string())),
- Some(matched) => match matches.next() {
- None => Ok(matched),
- Some(_) => Err(SubplotError::BindingNotUnique(step.to_string())),
- },
+ .filter_map(|b| b.match_with_step(step))
+ .collect();
+ if matches.len() > 1 {
+ // Too many matching bindings.
+ Err(SubplotError::BindingNotUnique(
+ step.to_string(),
+ MatchedSteps::new(matches),
+ ))
+ } else if let Some(m) = matches.pop() {
+ // Exactly one matching binding.
+ Ok(m)
+ } else {
+ // No matching bindings.
+ Err(SubplotError::BindingUnknown(step.to_string()))
}
}
@@ -719,7 +727,7 @@ mod test_bindings {
);
assert!(matches!(
bindings.find(&step),
- Err(SubplotError::BindingNotUnique(_))
+ Err(SubplotError::BindingNotUnique(_, _))
));
}