mm1_server/
outcome.rs

1use std::ops::ControlFlow;
2
3use mm1_address::address::Address;
4use mm1_common::types::Never;
5
6#[derive(derive_more::Debug)]
7pub struct Outcome<Rq, Rs = Never> {
8    pub(crate) action: Action<Rq, Rs>,
9    pub(crate) then:   ControlFlow<()>,
10}
11
12#[derive(Default, derive_more::Debug, derive_more::From)]
13pub(crate) enum Action<Rq, Rs> {
14    #[default]
15    Nothing,
16    Forward(OutcomeForward<Rq>),
17    Reply(OutcomeReply<Rs>),
18}
19
20#[derive(derive_more::Debug)]
21pub(crate) struct OutcomeForward<Rq> {
22    pub(crate) to:      Address,
23    #[debug(skip)]
24    pub(crate) request: Rq,
25}
26
27#[derive(derive_more::Debug)]
28pub(crate) struct OutcomeReply<Rs> {
29    #[debug(skip)]
30    pub(crate) response: Rs,
31}
32
33impl<Rq, Rs> Outcome<Rq, Rs> {
34    pub fn forward(to: Address, request: Rq) -> Self {
35        let forward = OutcomeForward { to, request };
36        Self {
37            action: forward.into(),
38            then:   ControlFlow::Continue(()),
39        }
40    }
41
42    pub fn reply(response: Rs) -> Self {
43        let reply = OutcomeReply { response };
44        Self {
45            action: reply.into(),
46            then:   ControlFlow::Continue(()),
47        }
48    }
49
50    pub fn no_reply() -> Self {
51        Self {
52            action: Default::default(),
53            then:   ControlFlow::Continue(()),
54        }
55    }
56
57    pub fn then_stop(self) -> Self {
58        Self {
59            then: ControlFlow::Break(()),
60            ..self
61        }
62    }
63}