intrepid_core/action/candidate/
candidate_action.rs

1use tower::Service;
2
3use crate::{ActionService, Actionable, Frame, FrameFuture, Handler};
4
5use super::{Action, Active, Ready, Stateless};
6
7/// A convenience type for an action candidate
8pub type CandidateAction<ActionHandler, Args, State> =
9    Action<super::Candidate<ActionHandler, Args, State>>;
10
11impl<ActionHandler, Args, State> CandidateAction<ActionHandler, Args, State>
12where
13    ActionHandler: Handler<Args, State> + Clone + Send + 'static,
14    Args: Clone + Send + 'static,
15    State: Clone + Send + 'static,
16{
17    /// Create an [`Actionable`] [`Candidate`] from a given `ActionHandler`.
18    pub fn new(action: ActionHandler) -> Self {
19        super::Candidate::new(action).into()
20    }
21
22    /// Create a new `Actionable` `Ready` from an action and a state.
23    pub fn with_state(self, state: State) -> Action<Ready<ActionHandler, Args, State>> {
24        Ready::new(self.action_state.action, state).into()
25    }
26}
27
28impl<ActionHandler, Args> CandidateAction<ActionHandler, Args, ()>
29where
30    ActionHandler: Handler<Args, ()> + Clone + Send + 'static,
31    Args: Clone + Send + 'static,
32{
33    /// Create a new `Actionable` `Stateless` from an action, with no state.
34    pub fn stateless(self) -> Action<Stateless<ActionHandler, Args>> {
35        Stateless::new(self.action_state.action).into()
36    }
37}
38
39impl<ActionHandler, Args, State> Actionable<State> for CandidateAction<ActionHandler, Args, State>
40where
41    ActionHandler: Handler<Args, State> + Clone + Send + Sync + 'static,
42    Args: Clone + Send + Sync + 'static,
43    State: Clone + Send + Sync + 'static,
44{
45    fn into_actionable(self: Box<Self>, state: State) -> Action<Active> {
46        let inner = self.with_state(state);
47        let service = ActionService::new(inner);
48
49        Action::active(service)
50    }
51}
52
53impl<IntoFrame, ActionHandler, Args> Service<IntoFrame> for CandidateAction<ActionHandler, Args, ()>
54where
55    IntoFrame: Into<Frame> + Clone + Send + 'static,
56    ActionHandler: Handler<Args, ()> + Clone + Send + 'static,
57    Args: Clone + Send + 'static,
58{
59    type Response = Frame;
60    type Error = crate::Error;
61    type Future = FrameFuture;
62
63    fn poll_ready(
64        &mut self,
65        cx: &mut std::task::Context<'_>,
66    ) -> std::task::Poll<Result<(), Self::Error>> {
67        let mut action = self.clone().stateless();
68
69        Service::<IntoFrame>::poll_ready(&mut action, cx)
70    }
71
72    fn call(&mut self, frame: IntoFrame) -> Self::Future {
73        let mut action = self.clone().stateless();
74
75        Service::<IntoFrame>::call(&mut action, frame)
76    }
77}