mm1_sup/mixed/
decider.rs

1use std::fmt;
2
3use mm1_address::address::Address;
4use mm1_proto::message;
5
6use crate::mixed::ChildType;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[message(base_path = ::mm1_proto)]
10pub enum DeciderErrorKind {}
11
12pub trait Decider {
13    type Key;
14    type Error: fmt::Display;
15
16    fn add(&mut self, key: Self::Key, child_type: ChildType) -> Result<(), Self::Error>;
17    fn rm(&mut self, key: &Self::Key) -> Result<(), Self::Error>;
18
19    fn started(&mut self, key: &Self::Key, addr: Address, at: tokio::time::Instant);
20    fn exited(&mut self, addr: Address, normal_exit: bool, at: tokio::time::Instant);
21    fn failed(&mut self, key: &Self::Key, at: tokio::time::Instant);
22    fn quit(&mut self, normal_exit: bool);
23
24    fn address(&self, key: &Self::Key) -> Result<Option<Address>, Self::Error>;
25
26    fn next_action(
27        &mut self,
28        at: tokio::time::Instant,
29    ) -> Result<Option<Action<'_, Self::Key>>, Self::Error>;
30}
31
32#[derive(Debug)]
33pub enum Action<'a, ID> {
34    Noop,
35    InitDone,
36    Start {
37        child_id: &'a ID,
38    },
39    Stop {
40        address:  Address,
41        child_id: Option<&'a ID>,
42    },
43    Quit {
44        normal_exit: bool,
45    },
46}
47
48impl<ID> fmt::Display for Action<'_, ID>
49where
50    ID: fmt::Display,
51{
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        match self {
54            Self::Noop => write!(f, "Noop"),
55            Self::InitDone => write!(f, "InitDone"),
56            Self::Start { child_id } => write!(f, "Start({child_id})"),
57            Self::Stop { address, child_id } => {
58                write!(
59                    f,
60                    "Stop({}, {:?})",
61                    address,
62                    child_id.map(|s| s.to_string())
63                )
64            },
65            Self::Quit { normal_exit } => write!(f, "Quit(normal={normal_exit})"),
66        }
67    }
68}