summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2021-08-11 19:14:30 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2021-09-07 17:32:19 +0100
commit8f58565372ce664bf9f9e619ba909fcf13f3a84d (patch)
tree71d082bcb33ef1f7de3c698ffc97240db50f7e19 /src
parentba7c85fe9f45882b6f3a6522ee6c54e56f5318df (diff)
downloadsubplot-8f58565372ce664bf9f9e619ba909fcf13f3a84d.tar.gz
bindings: Refactor function+cleanup into a struct
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'src')
-rw-r--r--src/bindings.rs65
1 files changed, 59 insertions, 6 deletions
diff --git a/src/bindings.rs b/src/bindings.rs
index c805965..0291e68 100644
--- a/src/bindings.rs
+++ b/src/bindings.rs
@@ -90,6 +90,61 @@ impl CaptureType {
}
}
}
+
+/// A link from a binding to its implementation in a given language.
+///
+/// Such a link comprises a function name to call for the step and
+/// an optional function name to call to clean the step up at the end.
+#[derive(Debug, Clone)]
+pub struct BindingImpl {
+ function: String,
+ cleanup: Option<String>,
+}
+
+impl BindingImpl {
+ /// Create a new binding implementation
+ ///
+ /// ```
+ /// # use subplot::bindings::BindingImpl;
+ ///
+ /// let bimpl = BindingImpl::new("foo::bar::func", Some("foo::bar::func_cleanup"));
+ /// ```
+ pub fn new(function: &str, cleanup: Option<&str>) -> Self {
+ Self {
+ function: function.to_string(),
+ cleanup: cleanup.map(str::to_string),
+ }
+ }
+
+ /// Retrieve the function name in this binding
+ ///
+ /// ```
+ /// # use subplot::bindings::BindingImpl;
+ /// let bimpl = BindingImpl::new("foo::bar::func", None);
+ ///
+ /// assert_eq!(bimpl.function(), "foo::bar::func");
+ /// ```
+ pub fn function(&self) -> &str {
+ &self.function
+ }
+
+ /// Retrieve the cleanup function name in this binding
+ ///
+ /// ```
+ /// # use subplot::bindings::BindingImpl;
+ /// let bimpl = BindingImpl::new("foo::bar::func", None);
+ ///
+ /// assert_eq!(bimpl.cleanup(), None);
+ ///
+ /// let bimpl = BindingImpl::new("foo::bar::func", Some("foo::bar::func_cleanup"));
+ ///
+ /// assert_eq!(bimpl.cleanup(), Some("foo::bar::func_cleanup"));
+ /// ```
+ pub fn cleanup(&self) -> Option<&str> {
+ self.cleanup.as_deref()
+ }
+}
+
/// A binding of a scenario step to its implementation.
///
/// Contains the pattern used to match against scenario steps,
@@ -100,8 +155,7 @@ pub struct Binding {
kind: StepKind,
pattern: String,
regex: Regex,
- function: String,
- cleanup: Option<String>,
+ impls: BindingImpl,
types: HashMap<String, CaptureType>,
}
@@ -129,8 +183,7 @@ impl Binding {
kind,
pattern: pattern.to_owned(),
regex,
- function: function.to_string(),
- cleanup: cleanup.map(String::from),
+ impls: BindingImpl::new(function, cleanup),
types,
})
}
@@ -147,12 +200,12 @@ impl Binding {
/// Return name of function that implements step.
pub fn function(&self) -> &str {
- &self.function
+ self.impls.function()
}
/// Return name of function that implements cleanup.
pub fn cleanup(&self) -> Option<&str> {
- self.cleanup.as_deref()
+ self.impls.cleanup()
}
/// Return the compiled regular expression for the pattern of the