use futures::future::{BoxFuture, ready};
pub trait ActivityDispatcher: Send + Sync + 'static {
fn dispatch(&self, name: &str, input: &str, config: &str) -> Result<String, String>;
fn dispatch_from_process(
&self,
name: &str,
input: &str,
config: &str,
caller_pid: Option<u64>,
) -> Result<String, String> {
let _ = caller_pid;
self.dispatch(name, input, config)
}
fn dispatch_async_from_process<'a>(
&'a self,
name: &'a str,
input: &'a str,
config: &'a str,
caller_pid: Option<u64>,
) -> BoxFuture<'a, Result<String, String>> {
Box::pin(ready(
self.dispatch_from_process(name, input, config, caller_pid),
))
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use super::ActivityDispatcher;
use crate::runtime::EngineNifState;
struct Echo;
impl ActivityDispatcher for Echo {
fn dispatch(&self, _name: &str, input: &str, _config: &str) -> Result<String, String> {
Ok(input.to_owned())
}
}
#[test]
fn dispatcher_is_accessible_after_install_on_engine_state() {
let state = EngineNifState::default();
state.set_activity_dispatcher(Arc::new(Echo));
let dispatcher = state.activity_dispatcher();
assert!(dispatcher.is_some());
assert_eq!(
dispatcher
.as_ref()
.and_then(|d| d.dispatch("test", "hello", "{}").ok()),
Some("hello".to_owned())
);
}
}