backbone-core 2.7.36

Backbone Framework Core - Foundation for generic CRUD system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Configuration Bus - Cross-Module Configuration Sharing
//!
//! The Configuration Bus provides a mechanism for modules to:
//! - Share configuration values across bounded contexts
//! - Subscribe to configuration changes
//! - Access module-specific configuration without tight coupling
//!
//! # Architecture
//!
//! ```text
//! ┌────────────────────────────────────────────────────────────────┐
//! │                    Configuration Bus                           │
//! │  ┌─────────────────────────────────────────────────────────┐  │
//! │  │              Shared Configuration Store                  │  │
//! │  │  sapiens.auth.jwt_ttl = 3600                            │  │
//! │  │  postman.smtp.host = "smtp.example.com"                 │  │
//! │  │  bucket.storage.max_size = 1073741824                 │  │
//! │  └─────────────────────────────────────────────────────────┘  │
//! │                              │                                 │
//! │        ┌────────────────────┼────────────────────┐            │
//! │        │                    │                    │            │
//! │        ▼                    ▼                    ▼            │
//! │   ┌─────────┐         ┌─────────┐         ┌─────────┐        │
//! │   │ Sapiens │         │ Postman │         │Bucket │        │
//! │   │ Module  │         │ Module  │         │ Module  │        │
//! │   └─────────┘         └─────────┘         └─────────┘        │
//! └────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Usage
//!
//! ```rust,ignore
//! use backbone_core::config::ConfigurationBus;
//!
//! // Create shared configuration bus
//! let config_bus = ConfigurationBus::new();
//!
//! // Set module configuration
//! config_bus.set("sapiens.auth.jwt_ttl", ConfigValue::Integer(3600)).await;
//! config_bus.set("postman.smtp.host", ConfigValue::String("smtp.example.com".into())).await;
//!
//! // Get configuration from any module
//! if let Some(ttl) = config_bus.get_integer("sapiens.auth.jwt_ttl").await {
//!     println!("JWT TTL: {}", ttl);
//! }
//!
//! // Subscribe to configuration changes
//! let rx = config_bus.subscribe("sapiens.*").await;
//! ```

use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::{RwLock, broadcast};
use serde::{Deserialize, Serialize};

// ============================================================
// Configuration Value Types
// ============================================================

/// Configuration value that can hold different types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum ConfigValue {
    String(String),
    Integer(i64),
    Float(f64),
    Boolean(bool),
    Array(Vec<ConfigValue>),
    Object(HashMap<String, ConfigValue>),
    Null,
}

impl ConfigValue {
    /// Get as string
    pub fn as_string(&self) -> Option<&str> {
        match self {
            ConfigValue::String(s) => Some(s),
            _ => None,
        }
    }

    /// Get as integer
    pub fn as_integer(&self) -> Option<i64> {
        match self {
            ConfigValue::Integer(i) => Some(*i),
            _ => None,
        }
    }

    /// Get as float
    pub fn as_float(&self) -> Option<f64> {
        match self {
            ConfigValue::Float(f) => Some(*f),
            ConfigValue::Integer(i) => Some(*i as f64),
            _ => None,
        }
    }

    /// Get as boolean
    pub fn as_boolean(&self) -> Option<bool> {
        match self {
            ConfigValue::Boolean(b) => Some(*b),
            _ => None,
        }
    }

    /// Check if null
    pub fn is_null(&self) -> bool {
        matches!(self, ConfigValue::Null)
    }
}

impl From<String> for ConfigValue {
    fn from(s: String) -> Self {
        ConfigValue::String(s)
    }
}

impl From<&str> for ConfigValue {
    fn from(s: &str) -> Self {
        ConfigValue::String(s.to_string())
    }
}

impl From<i64> for ConfigValue {
    fn from(i: i64) -> Self {
        ConfigValue::Integer(i)
    }
}

impl From<i32> for ConfigValue {
    fn from(i: i32) -> Self {
        ConfigValue::Integer(i as i64)
    }
}

impl From<f64> for ConfigValue {
    fn from(f: f64) -> Self {
        ConfigValue::Float(f)
    }
}

impl From<bool> for ConfigValue {
    fn from(b: bool) -> Self {
        ConfigValue::Boolean(b)
    }
}

// ============================================================
// Configuration Change Event
// ============================================================

/// Event emitted when configuration changes
#[derive(Debug, Clone)]
pub struct ConfigChangeEvent {
    /// The configuration key that changed
    pub key: String,
    /// The old value (None if newly created)
    pub old_value: Option<ConfigValue>,
    /// The new value (None if deleted)
    pub new_value: Option<ConfigValue>,
    /// Timestamp of the change
    pub timestamp: chrono::DateTime<chrono::Utc>,
}

// ============================================================
// Configuration Bus
// ============================================================

/// Shared configuration bus for cross-module configuration
///
/// Provides a thread-safe, async-friendly way for modules to share
/// configuration without direct dependencies.
#[derive(Clone)]
pub struct ConfigurationBus {
    /// The configuration store
    store: Arc<RwLock<HashMap<String, ConfigValue>>>,
    /// Broadcast channel for configuration changes
    change_tx: broadcast::Sender<ConfigChangeEvent>,
}

impl ConfigurationBus {
    /// Create a new configuration bus
    pub fn new() -> Self {
        let (change_tx, _) = broadcast::channel(1000);
        Self {
            store: Arc::new(RwLock::new(HashMap::new())),
            change_tx,
        }
    }

    /// Set a configuration value
    ///
    /// Notifies all subscribers of the change.
    pub async fn set(&self, key: impl Into<String>, value: impl Into<ConfigValue>) {
        let key = key.into();
        let value = value.into();

        let old_value = {
            let mut store = self.store.write().await;
            store.insert(key.clone(), value.clone())
        };

        // Emit change event
        let event = ConfigChangeEvent {
            key,
            old_value,
            new_value: Some(value),
            timestamp: chrono::Utc::now(),
        };
        let _ = self.change_tx.send(event);
    }

    /// Set multiple configuration values at once
    pub async fn set_many(&self, values: HashMap<String, ConfigValue>) {
        let mut store = self.store.write().await;
        for (key, value) in values {
            let old_value = store.insert(key.clone(), value.clone());
            let event = ConfigChangeEvent {
                key,
                old_value,
                new_value: Some(value),
                timestamp: chrono::Utc::now(),
            };
            let _ = self.change_tx.send(event);
        }
    }

    /// Get a configuration value
    pub async fn get(&self, key: &str) -> Option<ConfigValue> {
        self.store.read().await.get(key).cloned()
    }

    /// Get a string configuration value
    pub async fn get_string(&self, key: &str) -> Option<String> {
        self.get(key).await.and_then(|v| v.as_string().map(|s| s.to_string()))
    }

    /// Get an integer configuration value
    pub async fn get_integer(&self, key: &str) -> Option<i64> {
        self.get(key).await.and_then(|v| v.as_integer())
    }

    /// Get a float configuration value
    pub async fn get_float(&self, key: &str) -> Option<f64> {
        self.get(key).await.and_then(|v| v.as_float())
    }

    /// Get a boolean configuration value
    pub async fn get_boolean(&self, key: &str) -> Option<bool> {
        self.get(key).await.and_then(|v| v.as_boolean())
    }

    /// Get a configuration value with default
    pub async fn get_or_default(&self, key: &str, default: ConfigValue) -> ConfigValue {
        self.get(key).await.unwrap_or(default)
    }

    /// Delete a configuration value
    ///
    /// Returns the deleted value if it existed.
    pub async fn delete(&self, key: &str) -> Option<ConfigValue> {
        let old_value = {
            let mut store = self.store.write().await;
            store.remove(key)
        };

        if let Some(ref old) = old_value {
            let event = ConfigChangeEvent {
                key: key.to_string(),
                old_value: Some(old.clone()),
                new_value: None,
                timestamp: chrono::Utc::now(),
            };
            let _ = self.change_tx.send(event);
        }

        old_value
    }

    /// Check if a configuration key exists
    pub async fn contains(&self, key: &str) -> bool {
        self.store.read().await.contains_key(key)
    }

    /// List all configuration keys
    pub async fn keys(&self) -> Vec<String> {
        self.store.read().await.keys().cloned().collect()
    }

    /// List configuration keys matching a prefix
    ///
    /// For example, `keys_with_prefix("sapiens.")` returns all sapiens configuration.
    pub async fn keys_with_prefix(&self, prefix: &str) -> Vec<String> {
        self.store
            .read()
            .await
            .keys()
            .filter(|k| k.starts_with(prefix))
            .cloned()
            .collect()
    }

    /// Get all configuration values matching a prefix
    pub async fn get_with_prefix(&self, prefix: &str) -> HashMap<String, ConfigValue> {
        self.store
            .read()
            .await
            .iter()
            .filter(|(k, _)| k.starts_with(prefix))
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect()
    }

    /// Subscribe to configuration changes
    ///
    /// Returns a broadcast receiver that receives all configuration changes.
    pub fn subscribe(&self) -> broadcast::Receiver<ConfigChangeEvent> {
        self.change_tx.subscribe()
    }

    /// Get the total number of configuration entries
    pub async fn len(&self) -> usize {
        self.store.read().await.len()
    }

    /// Check if the configuration store is empty
    pub async fn is_empty(&self) -> bool {
        self.store.read().await.is_empty()
    }

    /// Clear all configuration values
    pub async fn clear(&self) {
        let mut store = self.store.write().await;
        store.clear();
    }

    /// Dump all configuration as a HashMap
    pub async fn dump(&self) -> HashMap<String, ConfigValue> {
        self.store.read().await.clone()
    }

    /// Load configuration from a HashMap
    ///
    /// Merges with existing configuration (overwrites duplicates).
    pub async fn load(&self, values: HashMap<String, ConfigValue>) {
        let mut store = self.store.write().await;
        for (key, value) in values {
            store.insert(key, value);
        }
    }
}

impl Default for ConfigurationBus {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================
// Tests
// ============================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_set_and_get() {
        let bus = ConfigurationBus::new();

        bus.set("test.key", "value").await;
        let value = bus.get_string("test.key").await;

        assert_eq!(value, Some("value".to_string()));
    }

    #[tokio::test]
    async fn test_typed_getters() {
        let bus = ConfigurationBus::new();

        bus.set("string", ConfigValue::String("hello".into())).await;
        bus.set("integer", ConfigValue::Integer(42)).await;
        bus.set("float", ConfigValue::Float(3.14)).await;
        bus.set("boolean", ConfigValue::Boolean(true)).await;

        assert_eq!(bus.get_string("string").await, Some("hello".to_string()));
        assert_eq!(bus.get_integer("integer").await, Some(42));
        assert_eq!(bus.get_float("float").await, Some(3.14));
        assert_eq!(bus.get_boolean("boolean").await, Some(true));
    }

    #[tokio::test]
    async fn test_keys_with_prefix() {
        let bus = ConfigurationBus::new();

        bus.set("sapiens.auth.jwt_ttl", 3600).await;
        bus.set("sapiens.auth.jwt_secret", "secret").await;
        bus.set("postman.smtp.host", "smtp.example.com").await;

        let sapiens_keys = bus.keys_with_prefix("sapiens.").await;
        assert_eq!(sapiens_keys.len(), 2);

        let postman_keys = bus.keys_with_prefix("postman.").await;
        assert_eq!(postman_keys.len(), 1);
    }

    #[tokio::test]
    async fn test_delete() {
        let bus = ConfigurationBus::new();

        bus.set("to.delete", "value").await;
        assert!(bus.contains("to.delete").await);

        let deleted = bus.delete("to.delete").await;
        assert!(deleted.is_some());
        assert!(!bus.contains("to.delete").await);
    }

    #[tokio::test]
    async fn test_subscription() {
        let bus = ConfigurationBus::new();
        let mut rx = bus.subscribe();

        bus.set("new.key", "new_value").await;

        let event = rx.try_recv().unwrap();
        assert_eq!(event.key, "new.key");
        assert!(event.old_value.is_none());
        assert_eq!(event.new_value, Some(ConfigValue::String("new_value".into())));
    }

    #[tokio::test]
    async fn test_get_with_prefix() {
        let bus = ConfigurationBus::new();

        bus.set("app.name", "MyApp").await;
        bus.set("app.version", "1.0.0").await;
        bus.set("db.url", "postgres://localhost").await;

        let app_config = bus.get_with_prefix("app.").await;
        assert_eq!(app_config.len(), 2);
        assert_eq!(app_config.get("app.name"), Some(&ConfigValue::String("MyApp".into())));
    }

    #[test]
    fn test_config_value_conversions() {
        let s: ConfigValue = "hello".into();
        assert_eq!(s.as_string(), Some("hello"));

        let i: ConfigValue = 42i64.into();
        assert_eq!(i.as_integer(), Some(42));

        let f: ConfigValue = 3.14f64.into();
        assert_eq!(f.as_float(), Some(3.14));

        let b: ConfigValue = true.into();
        assert_eq!(b.as_boolean(), Some(true));
    }
}