mm1_sup/mixed/
decider.rs

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