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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! Dynamic Configuration Module
//!
//! This module provides stateless dynamic configuration utilities for `OpenAI` API responses.
//! Following the "Thin Client, Rich API" principle, this module offers configuration management
//! patterns and validation tools without maintaining persistent configuration state.
mod private
{
use std::
{
collections ::HashMap,
sync ::{ Arc, RwLock },
time ::Instant,
};
use core::time::Duration;
use serde::{ Deserialize, Serialize };
use tokio::sync::{ mpsc, watch };
/// Configuration value that can be dynamically updated
#[ derive( Debug, Clone, PartialEq, Serialize, Deserialize ) ]
pub enum ConfigValue
{
/// String configuration value
String( String ),
/// Integer configuration value
Integer( i64 ),
/// Float configuration value
Float( f64 ),
/// Boolean configuration value
Boolean( bool ),
/// Duration configuration value (in milliseconds)
Duration( u64 ),
}
impl ConfigValue
{
/// Convert to string if possible
#[ inline ]
#[ must_use ]
pub fn as_string( &self ) -> Option< String >
{
match self
{
ConfigValue::String( s ) => Some( s.clone() ),
_ => None,
}
}
/// Convert to integer if possible
#[ inline ]
#[ must_use ]
pub fn as_integer( &self ) -> Option< i64 >
{
match self
{
ConfigValue::Integer( i ) => Some( *i ),
_ => None,
}
}
/// Convert to float if possible
#[ inline ]
#[ must_use ]
pub fn as_float( &self ) -> Option< f64 >
{
match self
{
ConfigValue::Float( f ) => Some( *f ),
_ => None,
}
}
/// Convert to boolean if possible
#[ inline ]
#[ must_use ]
pub fn as_boolean( &self ) -> Option< bool >
{
match self
{
ConfigValue::Boolean( b ) => Some( *b ),
_ => None,
}
}
/// Convert to duration if possible
#[ inline ]
#[ must_use ]
pub fn as_duration( &self ) -> Option< Duration >
{
match self
{
ConfigValue::Duration( ms ) => Some( Duration::from_millis( *ms ) ),
_ => None,
}
}
}
/// Configuration change event
#[ derive( Debug, Clone, PartialEq ) ]
pub struct ConfigChangeEvent
{
/// The configuration key that changed
pub key : String,
/// Old value (if any)
pub old_value : Option< ConfigValue >,
/// New value
pub new_value : ConfigValue,
/// Timestamp of the change
pub timestamp : Instant,
}
/// Configuration snapshot representing a point-in-time configuration state
#[ derive( Debug, Clone, Serialize, Deserialize ) ]
pub struct ConfigSnapshot
{
/// Configuration values
pub values : HashMap< String, ConfigValue >,
/// Version number for this snapshot
pub version : u64,
/// Timestamp when snapshot was created (not serialized)
#[ serde( skip, default = "Instant::now" ) ]
pub created_at : Instant,
}
impl ConfigSnapshot
{
/// Create a new empty configuration snapshot
#[ inline ]
#[ must_use ]
pub fn new() -> Self
{
Self
{
values : HashMap::new(),
version : 1,
created_at : Instant::now(),
}
}
/// Create a new snapshot with incremented version
#[ inline ]
#[ must_use ]
pub fn next_version( &self ) -> Self
{
Self
{
values : self.values.clone(),
version : self.version + 1,
created_at : Instant::now(),
}
}
/// Get a configuration value
#[ inline ]
#[ must_use ]
pub fn get( &self, key : &str ) -> Option< &ConfigValue >
{
self.values.get( key )
}
/// Set a configuration value, returning a new snapshot
#[ inline ]
#[ must_use ]
pub fn with_value( mut self, key : String, value : ConfigValue ) -> Self
{
self.values.insert( key, value );
self
}
/// Remove a configuration value, returning a new snapshot
#[ inline ]
#[ must_use ]
pub fn without_value( mut self, key : &str ) -> Self
{
self.values.remove( key );
self
}
/// Validate the configuration snapshot
///
/// # Errors
///
/// Returns an error if any validation rules fail for the configuration values.
#[ inline ]
pub fn validate( &self, validator : &ConfigValidator ) -> Result< (), Vec< String > >
{
validator.validate_snapshot( self )
}
}
impl Default for ConfigSnapshot
{
#[ inline ]
fn default() -> Self
{
Self
{
values : HashMap::new(),
version : 1,
created_at : Instant::now(),
}
}
}
/// Configuration validation rule
#[ derive( Debug, Clone ) ]
pub enum ValidationRule
{
/// Value must be present
Required,
/// String must match pattern
StringPattern( String ),
/// Integer must be within range
IntegerRange( i64, i64 ),
/// Float must be within range
FloatRange( f64, f64 ),
/// Duration must be within range (in milliseconds)
DurationRange( u64, u64 ),
}
/// Configuration validator
#[ derive( Debug, Clone ) ]
pub struct ConfigValidator
{
/// Validation rules for each configuration key
rules : HashMap< String, Vec< ValidationRule > >,
}
impl ConfigValidator
{
/// Create a new empty validator
#[ inline ]
#[ must_use ]
pub fn new() -> Self
{
Self
{
rules : HashMap::new(),
}
}
/// Add a validation rule for a key
#[ inline ]
#[ must_use ]
pub fn add_rule( mut self, key : String, rule : ValidationRule ) -> Self
{
self.rules.entry( key ).or_default().push( rule );
self
}
/// Validate a configuration snapshot
///
/// # Errors
///
/// Returns an error if any validation rules fail for the configuration values.
#[ inline ]
pub fn validate_snapshot( &self, snapshot : &ConfigSnapshot ) -> Result< (), Vec< String > >
{
let mut errors = Vec::new();
for ( key, rules ) in &self.rules
{
if let Some( value ) = snapshot.get( key )
{
for rule in rules
{
if let Err( error ) = Self::validate_value( key, value, rule )
{
errors.push( error );
}
}
}
else
{
// Check if any rule requires this key to be present
if rules.iter().any( | r | matches!( r, ValidationRule::Required ) )
{
errors.push( format!( "Required key '{key}' is missing" ) );
}
}
}
if errors.is_empty()
{
Ok( () )
}
else
{
Err( errors )
}
}
/// Validate a single value against a rule
fn validate_value( key : &str, value : &ConfigValue, rule : &ValidationRule ) -> Result< (), String >
{
match ( value, rule )
{
( _, ValidationRule::Required ) => Ok( () ), // Value exists, so it's valid
( ConfigValue::String( s ), ValidationRule::StringPattern( pattern ) ) =>
{
// Simple pattern matching (could be enhanced with regex)
if s.contains( pattern )
{
Ok( () )
}
else
{
Err( format!( "Key '{key}' string value '{s}' doesn't match pattern '{pattern}'" ) )
}
}
( ConfigValue::Integer( i ), ValidationRule::IntegerRange( min, max ) ) =>
{
if i >= min && i <= max
{
Ok( () )
}
else
{
Err( format!( "Key '{key}' integer value {i} is not in range [{min}, {max}]" ) )
}
}
( ConfigValue::Float( f ), ValidationRule::FloatRange( min, max ) ) =>
{
if f >= min && f <= max
{
Ok( () )
}
else
{
Err( format!( "Key '{key}' float value {f} is not in range [{min}, {max}]" ) )
}
}
( ConfigValue::Duration( ms ), ValidationRule::DurationRange( min_ms, max_ms ) ) =>
{
if ms >= min_ms && ms <= max_ms
{
Ok( () )
}
else
{
Err( format!( "Key '{key}' duration value {ms}ms is not in range [{min_ms}ms, {max_ms}ms]" ) )
}
}
_ =>
{
Err( format!( "Key '{key}' value type doesn't match validation rule" ) )
}
}
}
}
impl Default for ConfigValidator
{
#[ inline ]
fn default() -> Self
{
Self::new()
}
}
/// Configuration manager for stateless configuration operations
#[ derive( Debug ) ]
pub struct ConfigManager
{
/// Current configuration snapshot
current : Arc< RwLock< ConfigSnapshot > >,
/// Configuration validator
validator : ConfigValidator,
}
impl ConfigManager
{
/// Create a new configuration manager
#[ inline ]
#[ must_use ]
pub fn new( validator : ConfigValidator ) -> Self
{
Self
{
current : Arc::new( RwLock::new( ConfigSnapshot::new() ) ),
validator,
}
}
/// Get current configuration snapshot
///
/// # Panics
///
/// Panics if the internal lock is poisoned.
#[ inline ]
#[ must_use ]
pub fn get_snapshot( &self ) -> ConfigSnapshot
{
self.current.read().unwrap().clone()
}
/// Update configuration with a new snapshot
///
/// # Errors
///
/// Returns an error if the new snapshot fails validation.
///
/// # Panics
///
/// Panics if the internal lock is poisoned.
#[ inline ]
pub fn update_snapshot( &self, new_snapshot : ConfigSnapshot ) -> Result< ConfigSnapshot, Vec< String > >
{
// Validate the new snapshot
new_snapshot.validate( &self.validator )?;
// Update the current snapshot
let mut current = self.current.write().unwrap();
*current = new_snapshot.clone();
Ok( new_snapshot )
}
/// Update a single configuration value
///
/// # Errors
///
/// Returns an error if the updated configuration fails validation.
#[ inline ]
pub fn update_value( &self, key : String, value : ConfigValue ) -> Result< ConfigSnapshot, Vec< String > >
{
let current_snapshot = self.get_snapshot();
let new_snapshot = current_snapshot.next_version().with_value( key, value );
self.update_snapshot( new_snapshot )
}
/// Remove a configuration value
///
/// # Errors
///
/// Returns an error if the updated configuration fails validation.
#[ inline ]
pub fn remove_value( &self, key : &str ) -> Result< ConfigSnapshot, Vec< String > >
{
let current_snapshot = self.get_snapshot();
let new_snapshot = current_snapshot.next_version().without_value( key );
self.update_snapshot( new_snapshot )
}
/// Get a specific configuration value
#[ inline ]
#[ must_use ]
pub fn get_value( &self, key : &str ) -> Option< ConfigValue >
{
self.get_snapshot().get( key ).cloned()
}
}
/// Utilities for dynamic configuration management
#[ derive( Debug ) ]
pub struct DynamicConfigManager;
impl DynamicConfigManager
{
/// Create a configuration change watcher
#[ inline ]
#[ must_use ]
pub fn create_change_watcher() -> ( ConfigChangeSender, ConfigChangeReceiver )
{
let ( tx, rx ) = mpsc::unbounded_channel();
( ConfigChangeSender { sender : tx }, ConfigChangeReceiver { receiver : rx } )
}
/// Create a configuration value watcher
#[ inline ]
#[ must_use ]
pub fn create_value_watcher< T : Clone + Send + 'static >( initial_value : T ) -> ( watch::Sender< T >, watch::Receiver< T > )
{
watch ::channel( initial_value )
}
/// Apply configuration changes atomically
///
/// # Errors
///
/// Returns an error if the updated configuration fails validation.
#[ inline ]
pub fn apply_atomic_changes(
manager : &ConfigManager,
changes : Vec< ( String, ConfigValue ) >,
) -> Result< ConfigSnapshot, Vec< String > >
{
let mut current_snapshot = manager.get_snapshot().next_version();
// Apply all changes to the snapshot
for ( key, value ) in changes
{
current_snapshot = current_snapshot.with_value( key, value );
}
// Validate and update as a single operation
manager.update_snapshot( current_snapshot )
}
/// Create a configuration backup
#[ inline ]
#[ must_use ]
pub fn create_backup( snapshot : &ConfigSnapshot ) -> String
{
serde_json ::to_string( snapshot ).unwrap_or_else( | _ | "{}".to_string() )
}
/// Restore configuration from backup
///
/// # Errors
///
/// Returns an error if the backup cannot be parsed as a valid configuration snapshot.
#[ inline ]
pub fn restore_from_backup( backup : &str ) -> Result< ConfigSnapshot, String >
{
serde_json ::from_str( backup ).map_err( | e | format!( "Failed to parse backup : {e}" ) )
}
/// Merge two configuration snapshots
#[ inline ]
#[ must_use ]
pub fn merge_snapshots( base : &ConfigSnapshot, overlay : ConfigSnapshot ) -> ConfigSnapshot
{
let mut merged = base.next_version();
for ( key, value ) in overlay.values
{
merged = merged.with_value( key, value );
}
merged
}
}
/// Sender for configuration change events
#[ derive( Debug ) ]
pub struct ConfigChangeSender
{
sender : mpsc::UnboundedSender< ConfigChangeEvent >,
}
impl ConfigChangeSender
{
/// Send a configuration change event
///
/// # Errors
///
/// Returns an error if the event cannot be sent (e.g., if the receiver has been dropped).
#[ inline ]
pub fn send_change( &self, event : ConfigChangeEvent ) -> Result< (), &'static str >
{
self.sender.send( event ).map_err( | _ | "Failed to send change event" )
}
/// Send a configuration update event
///
/// # Errors
///
/// Returns an error if the event cannot be sent (e.g., if the receiver has been dropped).
#[ inline ]
pub fn send_update( &self, key : String, old_value : Option< ConfigValue >, new_value : ConfigValue ) -> Result< (), &'static str >
{
let event = ConfigChangeEvent
{
key,
old_value,
new_value,
timestamp : Instant::now(),
};
self.send_change( event )
}
}
/// Receiver for configuration change events
#[ derive( Debug ) ]
pub struct ConfigChangeReceiver
{
receiver : mpsc::UnboundedReceiver< ConfigChangeEvent >,
}
impl ConfigChangeReceiver
{
/// Try to receive a configuration change event (non-blocking)
#[ inline ]
pub fn try_recv( &mut self ) -> Option< ConfigChangeEvent >
{
self.receiver.try_recv().ok()
}
/// Receive next configuration change event (blocking)
#[ inline ]
pub async fn recv( &mut self ) -> Option< ConfigChangeEvent >
{
self.receiver.recv().await
}
}
}
crate ::mod_interface!
{
exposed use private::ConfigValue;
exposed use private::ConfigChangeEvent;
exposed use private::ConfigSnapshot;
exposed use private::ValidationRule;
exposed use private::ConfigValidator;
exposed use private::ConfigManager;
exposed use private::DynamicConfigManager;
exposed use private::ConfigChangeSender;
exposed use private::ConfigChangeReceiver;
}