Skip to main content

rns_net/interface/
registry.rs

1//! Interface factory registry.
2//!
3//! Holds a set of [`InterfaceFactory`] implementations keyed by their
4//! config-file type name (e.g. "TCPClientInterface").
5
6use std::collections::HashMap;
7
8use super::InterfaceFactory;
9
10/// Registry of interface factories keyed by type name.
11pub struct InterfaceRegistry {
12    factories: HashMap<String, Box<dyn InterfaceFactory>>,
13}
14
15impl InterfaceRegistry {
16    /// Create an empty registry.
17    pub fn new() -> Self {
18        InterfaceRegistry {
19            factories: HashMap::new(),
20        }
21    }
22
23    /// Register a factory. The factory's `type_name()` is used as the key.
24    pub fn register(&mut self, factory: Box<dyn InterfaceFactory>) {
25        let name = factory.type_name().to_string();
26        self.factories.insert(name, factory);
27    }
28
29    /// Look up a factory by config-file type name.
30    pub fn get(&self, type_name: &str) -> Option<&dyn InterfaceFactory> {
31        self.factories.get(type_name).map(|f| f.as_ref())
32    }
33
34    /// Create a registry pre-populated with all built-in interface types.
35    pub fn with_builtins() -> Self {
36        let mut reg = Self::new();
37        #[cfg(feature = "iface-tcp")]
38        {
39            reg.register(Box::new(super::tcp::TcpClientFactory));
40            reg.register(Box::new(super::tcp_server::TcpServerFactory));
41        }
42        #[cfg(feature = "iface-udp")]
43        reg.register(Box::new(super::udp::UdpFactory));
44        #[cfg(feature = "iface-serial")]
45        reg.register(Box::new(super::serial_iface::SerialFactory));
46        #[cfg(feature = "iface-kiss")]
47        reg.register(Box::new(super::kiss_iface::KissFactory));
48        #[cfg(feature = "iface-pipe")]
49        reg.register(Box::new(super::pipe::PipeFactory));
50        #[cfg(feature = "iface-local")]
51        {
52            reg.register(Box::new(super::local::LocalServerFactory));
53            reg.register(Box::new(super::local::LocalClientFactory));
54        }
55        #[cfg(feature = "iface-backbone")]
56        reg.register(Box::new(super::backbone::BackboneInterfaceFactory));
57        #[cfg(feature = "iface-auto")]
58        reg.register(Box::new(super::auto::AutoFactory));
59        #[cfg(feature = "iface-i2p")]
60        reg.register(Box::new(super::i2p::I2pFactory));
61        #[cfg(feature = "iface-rnode")]
62        reg.register(Box::new(super::rnode::RNodeFactory));
63        reg
64    }
65}
66
67impl Default for InterfaceRegistry {
68    fn default() -> Self {
69        Self::new()
70    }
71}