Skip to main content

dog_core/
registry.rs

1use std::collections::HashMap;
2use std::sync::Arc;
3
4use crate::DogService;
5
6/// A simple registry that maps service names to DogService instances.
7///
8/// This is the core of DogRS: named services that can be called
9/// from any transport (HTTP, CLI, jobs, P2P, etc.).
10pub struct DogServiceRegistry<R, P = ()>
11where
12    R: Send + 'static,
13    P: Send + 'static,
14{
15    services: HashMap<String, Arc<dyn DogService<R, P>>>,
16}
17
18impl<R, P> DogServiceRegistry<R, P>
19where
20    R: Send + 'static,
21    P: Send + 'static,
22{
23    pub fn new() -> Self {
24        Self {
25            services: HashMap::new(),
26        }
27    }
28
29    pub fn register<S>(&mut self, name: S, service: Arc<dyn DogService<R, P>>)
30    where
31        S: Into<String>,
32    {
33        self.services.insert(name.into(), service);
34    }
35
36    pub fn get(&self, name: &str) -> Option<&Arc<dyn DogService<R, P>>> {
37        self.services.get(name)
38    }
39}
40
41impl<R, P> Default for DogServiceRegistry<R, P>
42where
43    R: Send + 'static,
44    P: Send + 'static,
45{
46    fn default() -> Self {
47        Self::new()
48    }
49}