diff options
author | Daniel Silverstone <dsilvers+gitlab@digital-scurf.org> | 2022-05-06 19:28:34 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers+gitlab@digital-scurf.org> | 2022-05-06 19:28:34 +0000 |
commit | 27b617cbaa9e6d4fb58a0b971b428a05da8ec5d9 (patch) | |
tree | 08067bf4290982e4a717ba55a660714084370158 /src/bindings.rs | |
parent | c2cbccd45a2c356e458abc8bbef3193434e8572a (diff) | |
parent | 52ccfa5a1bf2893bdc63ff1bad2b2a88ca86ef95 (diff) | |
download | subplot-27b617cbaa9e6d4fb58a0b971b428a05da8ec5d9.tar.gz |
Merge branch 'liw/tidy-up-error' into 'main'
tidy up error handling a bit
See merge request subplot/subplot!275
Diffstat (limited to 'src/bindings.rs')
-rw-r--r-- | src/bindings.rs | 26 |
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; |