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
use tokio::sync::mpsc;

use crate::traits::Processor;

use super::Executor;

/// Simple implementation of [`crate::Executor`], process incomming [`crate::Command`]s one by one.
#[derive(Debug)]
pub struct SimpleExecutor<P>
where
    P: Processor,
{
    processor: P,
}

impl<P> SimpleExecutor<P>
where
    P: Processor,
{
    /// Constructor, takes a [`crate::Processor`] that will be used to handle incomming [`crate::Command`].
    pub fn new(processor: P) -> Self {
        Self { processor }
    }
}

#[async_trait::async_trait]
impl<P> Executor<P> for SimpleExecutor<P>
where
    P: Processor,
{
    async fn run(&mut self, receiver: mpsc::UnboundedReceiver<P::Command>) -> Result<(), P::Error> {
        let mut receiver = receiver;
        loop {
            let command = receiver.recv().await.unwrap();
            self.processor.handle(command).await?;
        }
    }
}

impl<P> From<P> for SimpleExecutor<P>
where
    P: Processor,
{
    fn from(input: P) -> Self {
        Self::new(input)
    }
}