use crate::step::Step;
use crate::test::TestState;
use futures::future::BoxFuture;
use std::fmt;
use std::sync::Arc;
type Handler =
Arc<dyn for<'a> Fn(&'a TestState) -> BoxFuture<'a, anyhow::Result<()>> + Send + Sync>;
pub struct CustomStep {
name: String,
handler: Handler,
}
impl CustomStep {
pub fn new(
name: impl Into<String>,
handler: impl for<'a> Fn(&'a TestState) -> BoxFuture<'a, anyhow::Result<()>>
+ Send
+ Sync
+ 'static,
) -> Self {
Self {
name: name.into(),
handler: Arc::new(handler),
}
}
}
impl Clone for CustomStep {
fn clone(&self) -> Self {
Self {
name: self.name.clone(),
handler: Arc::clone(&self.handler),
}
}
}
impl fmt::Debug for CustomStep {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CustomStep")
.field("name", &self.name)
.finish()
}
}
impl Step for CustomStep {
fn execute<'a>(&'a self, state: &'a TestState) -> BoxFuture<'a, anyhow::Result<()>> {
(self.handler)(state)
}
}