intrepid-core 0.2.0

Manage complex async business logic with ease
Documentation
mod ready_action;

pub use ready_action::ReadyAction;

/// A `Ready` type state represents an action which has a handler and a state, and can be
/// invoked
#[derive(Clone)]
pub struct Ready<ActionHandler: Clone, Args: Clone, State: Clone> {
    /// The action handler for this ready status struct.
    pub action: ActionHandler,
    /// The state the ready status contains.
    pub state: State,
    _args: std::marker::PhantomData<Args>,
}

#[tokio::test]
async fn creating_and_resuming_statelessly() -> Result<(), tower::BoxError> {
    use crate::Handler;
    use tower::Service;

    let action = || async {};
    let action = action.candidate();

    assert_eq!(action.stateless().call(()).await?, ().into());

    Ok(())
}

#[tokio::test]
async fn creating_and_resuming_with_state() -> Result<(), tower::BoxError> {
    use crate::{Frame, Handler, State};
    use tower::Service;

    #[derive(Clone)]
    struct ArbitraryState;

    impl From<ArbitraryState> for Frame {
        fn from(_: ArbitraryState) -> Self {
            "ArbitraryState".to_owned().into()
        }
    }

    let action = |State(state): State<ArbitraryState>| async { state };
    let action = action.candidate();

    assert_eq!(
        action.with_state(ArbitraryState).call(()).await?,
        "ArbitraryState".to_string().into()
    );

    Ok(())
}