1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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(())
}