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
53
54
55
56
57
use crate::mixed::child_spec::BoxedMixedChildSpec;

#[derive(Debug)]
pub struct SupSpec<ID, RS> {
    pub restart_strategy: RS,
    pub children: Vec<BoxedMixedChildSpec<ID>>,
}

impl<ID, RS> SupSpec<ID, RS> {
    pub fn new(restart_strategy: RS) -> Self {
        Self { restart_strategy, children: Default::default() }
    }

    pub fn with_child<CS>(mut self, child_spec: CS) -> Self
    where
        CS: Into<BoxedMixedChildSpec<ID>>,
    {
        self.children.push(child_spec.into());
        self
    }
}

#[tokio::test]
async fn ergonomics() {
    use std::convert::Infallible;
    use std::time::Duration;

    use agner_actors::{Context, System};

    use crate::common::InitType;
    use crate::mixed::{MixedChildSpec, OneForOne, RestartIntensity};

    async fn actor(_context: &mut Context<Infallible>, (): ()) {}

    let child_one = MixedChildSpec::mixed("first")
        .behaviour(actor)
        .args_clone(())
        .init_type(InitType::no_ack());
    let child_two = MixedChildSpec::mixed("second")
        .behaviour(actor)
        .args_clone(())
        .init_type(InitType::no_ack());

    let child_three = MixedChildSpec::mixed("third")
        .behaviour(actor)
        .args_clone(())
        .init_type(InitType::no_ack());

    let restart_intensity = RestartIntensity::new(5, Duration::from_secs(30));
    let restart_strategy = OneForOne::new(restart_intensity);
    let sup_spec = SupSpec::new(restart_strategy).with_child(child_one).with_child(child_two);

    let system = System::new(Default::default());
    let sup = system.spawn(crate::mixed::run, sup_spec, Default::default()).await.unwrap();

    crate::mixed::start_child(&system, sup, child_three).await.unwrap();
}