Skip to main content

commy_sdk_rust/
service.rs

1//! Service abstraction for remote data access
2
3use crate::message::VariableMetadata;
4use std::collections::HashMap;
5
6/// Represents a service on the server
7#[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    /// Create a new service instance
18    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    /// Get the service ID
29    pub fn id(&self) -> &str {
30        &self.id
31    }
32
33    /// Get the service name
34    pub fn name(&self) -> &str {
35        &self.name
36    }
37
38    /// Get the tenant ID
39    pub fn tenant_id(&self) -> &str {
40        &self.tenant_id
41    }
42
43    /// Check if this service supports direct memory mapping
44    pub fn supports_memory_mapping(&self) -> bool {
45        self.file_path.is_some()
46    }
47
48    /// Get the file path for local memory mapping
49    pub fn file_path(&self) -> Option<&str> {
50        self.file_path.as_deref()
51    }
52
53    /// Add variable metadata
54    pub fn add_variable(&mut self, meta: VariableMetadata) {
55        self.variables.insert(meta.name.clone(), meta);
56    }
57
58    /// Get variable metadata
59    pub fn get_variable(&self, name: &str) -> Option<&VariableMetadata> {
60        self.variables.get(name)
61    }
62
63    /// List all variables
64    pub fn variables(&self) -> &HashMap<String, VariableMetadata> {
65        &self.variables
66    }
67
68    /// Clear variable cache
69    pub fn clear_variables(&mut self) {
70        self.variables.clear();
71    }
72}
73
74/// Service manager for client operations
75#[derive(Debug)]
76pub struct ServiceManager {
77    services: HashMap<String, Service>,
78}
79
80impl ServiceManager {
81    /// Create a new service manager
82    pub fn new() -> Self {
83        Self {
84            services: HashMap::new(),
85        }
86    }
87
88    /// Register a service
89    pub fn register(&mut self, service: Service) {
90        self.services.insert(service.id.clone(), service);
91    }
92
93    /// Get a registered service
94    pub fn get(&self, service_id: &str) -> Option<&Service> {
95        self.services.get(service_id)
96    }
97
98    /// Get a mutable service reference
99    pub fn get_mut(&mut self, service_id: &str) -> Option<&mut Service> {
100        self.services.get_mut(service_id)
101    }
102
103    /// List all registered services
104    pub fn list(&self) -> Vec<&Service> {
105        self.services.values().collect()
106    }
107
108    /// Clear all services
109    pub fn clear(&mut self) {
110        self.services.clear();
111    }
112
113    /// Get service count
114    pub fn len(&self) -> usize {
115        self.services.len()
116    }
117
118    /// Check if empty
119    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}