about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--finito-core/Cargo.toml1
-rw-r--r--finito-core/src/lib.rs29
2 files changed, 20 insertions, 10 deletions
diff --git a/finito-core/Cargo.toml b/finito-core/Cargo.toml
index c3a377dd2417..1d7bdb8b01fe 100644
--- a/finito-core/Cargo.toml
+++ b/finito-core/Cargo.toml
@@ -4,3 +4,4 @@ version = "0.1.0"
 authors = ["Vincent Ambo <mail@tazj.in>"]
 
 [dependencies]
+serde = "1.0"
diff --git a/finito-core/src/lib.rs b/finito-core/src/lib.rs
index 0ce17cc28d13..0dda30ae9a52 100644
--- a/finito-core/src/lib.rs
+++ b/finito-core/src/lib.rs
@@ -108,6 +108,10 @@
 //!
 //! Please reach out! I want to know why!
 
+extern crate serde;
+
+use serde::Serialize;
+use serde::de::DeserializeOwned;
 use std::fmt::Debug;
 use std::mem;
 
@@ -199,13 +203,12 @@ pub fn advance<S: FSM>(state: S, event: S::Event) -> (S, Vec<S::Action>) {
 ///
 /// See the `finito-postgres` and `finito-in-mem` crates for example
 /// implementations of this trait.
-pub trait FSMBackend {
-    /// Custom state type that is made available to action handlers by
-    /// the backend.
-    ///
-    /// TODO: Something something `Into<FSM::State> for State`.
-    type State;
-
+///
+/// Backends must be parameterised over an additional (user-supplied)
+/// state type which can be used to track application state that must
+/// be made available to action handlers, for example to pass along
+/// database connections.
+pub trait FSMBackend<S> {
     /// Key type used to identify individual state machines in this
     /// backend.
     ///
@@ -219,10 +222,12 @@ pub trait FSMBackend {
 
     /// Insert a new state-machine into the backend's storage and
     /// return its newly allocated key.
-    fn insert_machine<S: FSM>(&self, initial: S) -> Result<Self::Key, Self::Error>;
+    fn insert_machine<F>(&self, initial: F) -> Result<Self::Key, Self::Error>
+    where F: FSM + Serialize + DeserializeOwned;
 
     /// Retrieve the current state of an FSM by its key.
-    fn get_machine<S: FSM>(&self, key: Self::Key) -> Result<S, Self::Error>;
+    fn get_machine<F: FSM>(&self, key: Self::Key) -> Result<F, Self::Error>
+    where F: FSM + Serialize + DeserializeOwned;
 
     /// Advance a state machine by applying an event and persisting it
     /// as well as any resulting actions.
@@ -230,5 +235,9 @@ pub trait FSMBackend {
     /// **Note**: Whether actions are automatically executed depends
     /// on the backend used. Please consult the backend's
     /// documentation for details.
-    fn advance<S: FSM>(&self, key: Self::Key, event: S::Event) -> Result<S, Self::Error>;
+    fn advance<F: FSM>(&self, key: Self::Key, event: F::Event) -> Result<F, Self::Error>
+    where F: FSM + Serialize + DeserializeOwned,
+          F::State: From<S>,
+          F::Event: Serialize + DeserializeOwned,
+          F::Action: Serialize + DeserializeOwned;
 }