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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use std::{fmt::Display, sync::Arc};

use derive_more::Constructor;

use crate::{
    actor::{ActorMeta, ActorStatus},
    config::AnyConfig,
    message,
};

/// A helper type for using in generic code (e.g. as an associated type) to
/// indicate a message that cannot be constructed.
///
/// Feel free to define a custom `Impossible` type as an empty enum if more
/// implemented traits are needed.
#[message]
#[derive(Eq, PartialEq, Hash, Copy)]
pub enum Impossible {}

/// Checks that the actor is able to handle messages.
/// Routed to all actors in a group by default and handled implicitly by actors.
#[message(ret = ())]
#[derive(Default)]
#[non_exhaustive]
pub struct Ping;

#[message(ret = Result<(), ConfigRejected>)]
#[derive(Constructor)]
#[non_exhaustive]
pub struct ValidateConfig {
    pub config: AnyConfig,
}

#[message(ret = Result<(), ConfigRejected>)]
#[derive(Constructor)]
#[non_exhaustive]
pub struct UpdateConfig {
    pub config: AnyConfig,
}

#[message]
#[non_exhaustive]
pub struct ConfigRejected {
    pub reason: String,
}

impl<R: Display> From<R> for ConfigRejected {
    fn from(reason: R) -> Self {
        Self {
            reason: reason.to_string(),
        }
    }
}

#[message(ret = Result<(), StartEntrypointRejected>)]
#[derive(Constructor)]
#[non_exhaustive]
pub struct StartEntrypoint {
    pub is_check_only: bool,
}

#[message(part)]
#[derive(Constructor)]
#[non_exhaustive]
pub struct StartEntrypointRejected {
    pub errors: Vec<EntrypointError>,
}

#[message(part)]
#[derive(Constructor)]
#[non_exhaustive]
pub struct EntrypointError {
    pub group: String,
    pub reason: String,
}

#[message]
#[non_exhaustive]
pub struct ConfigUpdated {
    // TODO: add `old_config`.
}

#[message]
#[derive(Default)]
#[non_exhaustive]
pub struct Terminate {
    pub(crate) closing: bool,
}

impl Terminate {
    /// The message closes a target's mailbox ignoring `TerminationPolicy`.
    pub fn closing() -> Self {
        Self { closing: true }
    }
}

// === Status ===

// TODO: should it be a request?
#[message]
#[derive(Default)]
#[non_exhaustive]
pub struct SubscribeToActorStatuses {
    pub(crate) forcing: bool,
}

impl SubscribeToActorStatuses {
    /// Statuses will be sent even if a subscriber is already registered.
    pub fn forcing() -> Self {
        Self { forcing: true }
    }
}

#[message]
#[non_exhaustive]
pub struct ActorStatusReport {
    pub meta: Arc<ActorMeta>,
    pub status: ActorStatus,
}