summaryrefslogtreecommitdiff
path: root/src/bindings.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bindings.rs')
-rw-r--r--src/bindings.rs26
1 files changed, 16 insertions, 10 deletions
diff --git a/src/bindings.rs b/src/bindings.rs
index 0837553..5db63c9 100644
--- a/src/bindings.rs
+++ b/src/bindings.rs
@@ -3,7 +3,7 @@ use super::MatchedSteps;
use super::PartialStep;
use super::ScenarioStep;
use super::StepKind;
-use crate::{resource, Result, SubplotError};
+use crate::{resource, SubplotError};
use serde::{Deserialize, Serialize};
use serde_aux::prelude::*;
@@ -54,7 +54,7 @@ pub enum CaptureType {
impl FromStr for CaptureType {
type Err = SubplotError;
- fn from_str(value: &str) -> Result<Self> {
+ fn from_str(value: &str) -> Result<Self, SubplotError> {
match value.to_ascii_lowercase().as_str() {
"word" => Ok(Self::Word),
"text" => Ok(Self::Text),
@@ -168,10 +168,11 @@ impl Binding {
pattern: &str,
case_sensitive: bool,
mut types: HashMap<String, CaptureType>,
- ) -> Result<Binding> {
+ ) -> Result<Binding, SubplotError> {
let regex = RegexBuilder::new(&format!("^{}$", pattern))
.case_insensitive(!case_sensitive)
- .build()?;
+ .build()
+ .map_err(|err| SubplotError::Regex(pattern.to_string(), err))?;
// For every named capture, ensure we have a known type for it.
// If the type is missing from the map, we default to `text` which is
// the .* pattern
@@ -462,8 +463,9 @@ impl Bindings {
}
/// Add bindings from a YAML string
- pub fn add_from_yaml(&mut self, yaml: &str) -> Result<()> {
- let bindings: Vec<ParsedBindingWrapper> = serde_yaml::from_str(yaml)?;
+ pub fn add_from_yaml(&mut self, yaml: &str) -> Result<(), SubplotError> {
+ let bindings: Vec<ParsedBindingWrapper> =
+ serde_yaml::from_str(yaml).map_err(SubplotError::Metadata)?;
for wrapper in bindings {
self.add(from_hashmap(&wrapper.binding)?);
}
@@ -477,7 +479,7 @@ impl Bindings {
/// Find the binding matching a given scenario step, if there is
/// exactly one.
- pub fn find(&self, template: &str, step: &ScenarioStep) -> Result<MatchedStep> {
+ pub fn find(&self, template: &str, step: &ScenarioStep) -> Result<MatchedStep, SubplotError> {
let mut matches: Vec<MatchedStep> = self
.bindings()
.iter()
@@ -499,7 +501,11 @@ impl Bindings {
}
/// Add bindings from a file.
- pub fn add_from_file<P>(&mut self, filename: P, template: Option<&str>) -> Result<()>
+ pub fn add_from_file<P>(
+ &mut self,
+ filename: P,
+ template: Option<&str>,
+ ) -> Result<(), SubplotError>
where
P: AsRef<Path> + Debug,
{
@@ -522,7 +528,7 @@ impl Bindings {
}
}
-fn from_hashmap(parsed: &ParsedBinding) -> Result<Binding> {
+fn from_hashmap(parsed: &ParsedBinding) -> Result<Binding, SubplotError> {
let given: i32 = parsed.given.is_some().into();
let when: i32 = parsed.when.is_some().into();
let then: i32 = parsed.then.is_some().into();
@@ -796,7 +802,7 @@ fn regex_from_simple_pattern(
pattern: &str,
explicit_plain: bool,
types: &mut HashMap<String, CaptureType>,
-) -> Result<String> {
+) -> Result<String, SubplotError> {
let pat = Regex::new(r"\{[^\s\{\}]+\}").unwrap();
let mut r = String::new();
let mut end = 0;