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
use async_trait::async_trait;
use std::fmt::Debug;

use crate::cache::Cacheable;

pub enum Completion<T, E>
where
    T: Clone + Send + Sync + 'static,
    E: Debug + Send + Sync + 'static,
{
    Total(T),
    Partial((T, E)),
    Error(E),
}

pub struct OutputEvent<T, E>
where
    T: Clone + Send + Sync + 'static,
    E: Debug + Send + Sync + 'static,
{
    pub completed_event: Completion<T, E>,
    pub identities: Vec<Vec<u8>>,
}

impl<T, E> OutputEvent<T, E>
where
    T: Clone + Send + Sync + 'static,
    E: Debug + Send + Sync + 'static,
{
    pub fn new(completed_event: Completion<T, E>) -> Self {
        Self {
            completed_event,
            identities: Vec::new(),
        }
    }

    pub fn add_identity(&mut self, identity: impl Cacheable) {
        self.identities.push(identity.identity())
    }
}

#[async_trait]
pub trait EventHandler {
    type InputEvent;
    type OutputEvent: Clone + Send + Sync + 'static;
    type Error: Debug + Send + Sync + 'static;

    async fn handle_event(
        &mut self,
        input: Self::InputEvent,
    ) -> OutputEvent<Self::OutputEvent, Self::Error>;
}