restxst 0.0.1

REST-first end-to-end / black-box API testing for Rust
Documentation
pub mod custom;
pub mod http;

use crate::test::TestState;
use futures::future::BoxFuture;
use std::fmt;
use std::sync::Arc;

/// The core trait every step must implement.
///
/// Implement this on your own types to create custom steps, or use the
/// built-in [`http::HttpStep`] and [`custom::CustomStep`] implementations.
pub trait Step: Send + Sync + fmt::Debug {
    fn execute<'a>(&'a self, state: &'a TestState) -> BoxFuture<'a, anyhow::Result<()>>;
}

/// A heap-allocated, type-erased step that can be cloned and stored.
///
/// Any type implementing [`Step`] can be converted into a `BoxedStep` via
/// [`From`] / [`Into`], so call sites stay ergonomic:
#[derive(Clone)]
pub struct BoxedStep(Arc<dyn Step>);

impl BoxedStep {
    pub fn new<S: Step + 'static>(step: S) -> Self {
        BoxedStep(Arc::new(step))
    }

    pub fn execute<'a>(&'a self, state: &'a TestState) -> BoxFuture<'a, anyhow::Result<()>> {
        self.0.execute(state)
    }
}

impl<S: Step + 'static> From<S> for BoxedStep {
    fn from(step: S) -> Self {
        BoxedStep::new(step)
    }
}

impl fmt::Debug for BoxedStep {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}