restxst 0.0.1

REST-first end-to-end / black-box API testing for Rust
Documentation
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>;

/// A closure-based step for quick inline async logic.
///
/// # Example
/// ```rust,ignore
/// CustomStep::new("seed-user", |state| Box::pin(async move {
///     state.store.insert(StoreKey::global("token"), "abc123".to_string())?;
///     Ok(())
/// }))
/// ```
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)
    }
}