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
use std::collections::HashMap;

use crate::JobHandler;

pub struct Consumer {
    handlers: HashMap<String, HashMap<String, Box<dyn JobHandler>>>,
}

impl Consumer {
    pub fn new() -> Self {
        Self {
            handlers: HashMap::new(),
        }
    }

    pub fn register(mut self, handler: impl JobHandler + 'static) -> Self {
        if !self.handlers.contains_key(handler.queue()) {
            self.handlers.insert(handler.queue().into(), HashMap::new());
        }

        self.handlers
            .get_mut(handler.queue())
            .unwrap()
            .insert(handler.kind().into(), Box::new(handler));

        self
    }

    pub fn handlers(&self) -> &HashMap<String, HashMap<String, Box<dyn JobHandler>>> {
        &self.handlers
    }
}