diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2020-06-06 11:59:40 +0100 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2020-06-06 15:53:15 +0100 |
commit | 1187eff8a21ff6938d4d00c2a91232fde2d1a796 (patch) | |
tree | 56130467d472af0430105569a5e6d34c249ea2db /src/bindings.rs | |
parent | bba22e1f86967feebf94ebac327f7489c1029afd (diff) | |
download | subplot-1187eff8a21ff6938d4d00c2a91232fde2d1a796.tar.gz |
feat: Support reporting error when more than one binding matches a step
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'src/bindings.rs')
-rw-r--r-- | src/bindings.rs | 23 |
1 files changed, 10 insertions, 13 deletions
diff --git a/src/bindings.rs b/src/bindings.rs index 4233528..ea35f57 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -294,20 +294,17 @@ impl Bindings { /// Find the binding matching a given scenario step, if there is /// exactly one. - pub fn find(&self, step: &ScenarioStep) -> Option<MatchedStep> { + pub fn find(&self, step: &ScenarioStep) -> Result<MatchedStep> { let mut matches = self .bindings() .iter() - .map(|b| b.match_with_step(step)) - .filter(|o| o.is_some()); - if let Some(m) = matches.next() { - if matches.count() == 0 { - m - } else { - None - } - } else { - None + .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())), + }, } } @@ -441,7 +438,7 @@ mod test_bindings { let binding = Binding::new(StepKind::When, r"I am Tomjon", "set_foo", None).unwrap(); let mut bindings = Bindings::new(); bindings.add(&binding); - assert!(bindings.find(&step).is_none()); + assert!(bindings.find(&step).is_err()); } #[test] @@ -451,7 +448,7 @@ mod test_bindings { Binding::new(StepKind::Given, r"I am Tomjon of Lancre", "set_foo", None).unwrap(); let mut bindings = Bindings::new(); bindings.add(&binding); - assert!(bindings.find(&step).is_none()); + assert!(bindings.find(&step).is_err()); } #[test] |