diff --git a/finito-core/Cargo.toml b/finito-core/Cargo.toml index c3a377dd2..1d7bdb8b0 100644 --- a/finito-core/Cargo.toml +++ b/finito-core/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" authors = ["Vincent Ambo "] [dependencies] +serde = "1.0" diff --git a/finito-core/src/lib.rs b/finito-core/src/lib.rs index 0ce17cc28..0dda30ae9 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(state: S, event: S::Event) -> (S, Vec) { /// /// 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 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 { /// 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(&self, initial: S) -> Result; + fn insert_machine(&self, initial: F) -> Result + where F: FSM + Serialize + DeserializeOwned; /// Retrieve the current state of an FSM by its key. - fn get_machine(&self, key: Self::Key) -> Result; + fn get_machine(&self, key: Self::Key) -> Result + 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(&self, key: Self::Key, event: S::Event) -> Result; + fn advance(&self, key: Self::Key, event: F::Event) -> Result + where F: FSM + Serialize + DeserializeOwned, + F::State: From, + F::Event: Serialize + DeserializeOwned, + F::Action: Serialize + DeserializeOwned; }