netget/protocol/
easy_registry.rs1use crate::llm::actions::Easy;
7use std::collections::HashMap;
8use std::sync::{Arc, LazyLock};
9
10pub struct EasyRegistry {
12 protocols: HashMap<String, Arc<dyn Easy>>,
14 event_routing: HashMap<String, String>,
17}
18
19impl EasyRegistry {
20 fn new() -> Self {
22 let mut registry = Self {
23 protocols: HashMap::new(),
24 event_routing: HashMap::new(),
25 };
26
27 registry.register_protocols();
29 registry.build_event_routing();
30
31 registry
32 }
33
34 fn register_protocols(&mut self) {
36 self.register(Arc::new(crate::easy::http::HttpEasyProtocol));
37 }
38
39 #[allow(dead_code)]
41 fn register(&mut self, protocol: Arc<dyn Easy>) {
42 let name = protocol.protocol_name().to_string();
43 self.protocols.insert(name, protocol);
44 }
45
46 fn build_event_routing(&mut self) {
48 for (protocol_name, protocol) in &self.protocols {
49 for event_type_id in protocol.get_handled_event_type_ids() {
50 self.event_routing
51 .insert(event_type_id.to_string(), protocol_name.clone());
52 }
53 }
54 }
55
56 pub fn get_by_name(&self, name: &str) -> Option<Arc<dyn Easy>> {
58 self.protocols.get(name).cloned()
59 }
60
61 pub fn get_by_event_type(&self, event_type_id: &str) -> Option<Arc<dyn Easy>> {
66 self.event_routing
67 .get(event_type_id)
68 .and_then(|protocol_name| self.get_by_name(protocol_name))
69 }
70
71 pub fn get_all_names(&self) -> Vec<String> {
73 self.protocols.keys().cloned().collect()
74 }
75
76 pub fn get_all(&self) -> Vec<Arc<dyn Easy>> {
78 self.protocols.values().cloned().collect()
79 }
80}
81
82pub static EASY_REGISTRY: LazyLock<EasyRegistry> = LazyLock::new(EasyRegistry::new);