commy_sdk_rust/
service.rs1use crate::message::VariableMetadata;
4use std::collections::HashMap;
5
6#[derive(Debug, Clone)]
8pub struct Service {
9 pub id: String,
10 pub name: String,
11 pub tenant_id: String,
12 pub file_path: Option<String>,
13 variables: HashMap<String, VariableMetadata>,
14}
15
16impl Service {
17 pub fn new(id: String, name: String, tenant_id: String, file_path: Option<String>) -> Self {
19 Self {
20 id,
21 name,
22 tenant_id,
23 file_path,
24 variables: HashMap::new(),
25 }
26 }
27
28 pub fn id(&self) -> &str {
30 &self.id
31 }
32
33 pub fn name(&self) -> &str {
35 &self.name
36 }
37
38 pub fn tenant_id(&self) -> &str {
40 &self.tenant_id
41 }
42
43 pub fn supports_memory_mapping(&self) -> bool {
45 self.file_path.is_some()
46 }
47
48 pub fn file_path(&self) -> Option<&str> {
50 self.file_path.as_deref()
51 }
52
53 pub fn add_variable(&mut self, meta: VariableMetadata) {
55 self.variables.insert(meta.name.clone(), meta);
56 }
57
58 pub fn get_variable(&self, name: &str) -> Option<&VariableMetadata> {
60 self.variables.get(name)
61 }
62
63 pub fn variables(&self) -> &HashMap<String, VariableMetadata> {
65 &self.variables
66 }
67
68 pub fn clear_variables(&mut self) {
70 self.variables.clear();
71 }
72}
73
74#[derive(Debug)]
76pub struct ServiceManager {
77 services: HashMap<String, Service>,
78}
79
80impl ServiceManager {
81 pub fn new() -> Self {
83 Self {
84 services: HashMap::new(),
85 }
86 }
87
88 pub fn register(&mut self, service: Service) {
90 self.services.insert(service.id.clone(), service);
91 }
92
93 pub fn get(&self, service_id: &str) -> Option<&Service> {
95 self.services.get(service_id)
96 }
97
98 pub fn get_mut(&mut self, service_id: &str) -> Option<&mut Service> {
100 self.services.get_mut(service_id)
101 }
102
103 pub fn list(&self) -> Vec<&Service> {
105 self.services.values().collect()
106 }
107
108 pub fn clear(&mut self) {
110 self.services.clear();
111 }
112
113 pub fn len(&self) -> usize {
115 self.services.len()
116 }
117
118 pub fn is_empty(&self) -> bool {
120 self.services.is_empty()
121 }
122}
123
124impl Default for ServiceManager {
125 fn default() -> Self {
126 Self::new()
127 }
128}
129
130#[cfg(test)]
131mod tests {
132 use super::*;
133
134 #[test]
135 fn test_service_creation() {
136 let service = Service::new(
137 "svc_1".to_string(),
138 "config".to_string(),
139 "tenant_1".to_string(),
140 Some("/tmp/service.mmap".to_string()),
141 );
142
143 assert_eq!(service.id(), "svc_1");
144 assert_eq!(service.name(), "config");
145 assert!(service.supports_memory_mapping());
146 }
147
148 #[test]
149 fn test_service_manager() {
150 let mut manager = ServiceManager::new();
151 let service = Service::new(
152 "svc_1".to_string(),
153 "config".to_string(),
154 "tenant_1".to_string(),
155 None,
156 );
157
158 manager.register(service);
159 assert_eq!(manager.len(), 1);
160 assert!(manager.get("svc_1").is_some());
161 }
162}