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
pub use self::location::Location;
mod location;

/// Demon trait
///
/// Demons are actors in the apocalypse framework. Implement this trait in your actors to allow them to reply to messages.
#[async_trait::async_trait]
pub trait Demon: std::marker::Send {
    type Input;
    type Output;

    /// Function that is called when a demon is spawned
    ///
    /// By default, the function does nothing.
    ///
    /// ```rust,no_run
    /// use apocalypse::{Demon, Location};
    ///
    /// struct EchoBot;
    ///
    /// #[async_trait::async_trait]
    /// impl Demon for EchoBot {
    ///     type Input = String;
    ///     type Output = String;
    ///     
    ///     // Callback for demon spawning
    ///     async fn spawned(&mut self, location: Location<Self>) {
    ///         log::debug!("Spawned echo bot with location {}", location);
    ///     }
    ///     
    ///     // Basic implementation of an echo handle function
    ///     async fn handle(&mut self, message: Self::Input) -> Self::Output {
    ///         message
    ///     }
    /// }
    /// ```
    async fn spawned(&mut self, _location: Location<Self>) {
        ()
    }

    /// Handler function for messages
    ///
    /// This is the main function, called for every message that the broker receives.
    ///
    /// ```rust,no_run
    /// use apocalypse::Demon;
    ///
    /// struct EchoBot;
    ///
    /// #[async_trait::async_trait]
    /// impl Demon for EchoBot {
    ///     type Input = String;
    ///     type Output = String;
    /// 
    ///     // Basic implementation of an echo handle function
    ///     async fn handle(&mut self, message: Self::Input) -> Self::Output {
    ///         message
    ///     }
    /// }
    /// ```
    async fn handle(&mut self, message: Self::Input) -> Self::Output;

    /// Function that is called when a demon is removed
    ///
    /// By default, the function does nothing.
    ///
    /// ```rust,no_run
    /// use apocalypse::{Demon, Location};
    ///
    /// struct EchoBot;
    ///
    /// #[async_trait::async_trait]
    /// impl Demon for EchoBot {
    ///     type Input = String;
    ///     type Output = String;
    ///     
    ///     // Basic implementation of an echo handle function
    ///     async fn handle(&mut self, message: Self::Input) -> Self::Output {
    ///         message
    ///     }
    ///
    ///     // Callback function 
    ///     async fn vanquished(&mut self, location: Location<Self>) {
    ///         log::debug!("Killed echo bot with location {}", location);
    ///     }
    /// }
    /// ```
    async fn vanquished(&mut self, _location: Location<Self>) {
        ()
    }

    /// This id will be printed in the debug logs of the demon's thread.
    ///
    /// It is useful when some lockup is happening and you have trouble to find it.
    #[cfg(feature = "full_log")]
    fn id(&self) -> String {
        "".to_string()
    }
}