confers 0.4.0

Production-ready Rust configuration library with zero boilerplate
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
//! Configuration version migration module.
//!
//! This module provides:
//! - [`Versioned`] trait for versioned configuration types
//! - [`MigrationRegistry`] for managing and executing migrations
//! - [`MigrationOnReload`] enum for reload behavior control
//!
//! # Design
//!
//! The migration system uses a graph-based approach where:
//! - Nodes represent configuration versions
//! - Edges represent migration functions between versions
//! - Path precomputation uses BFS to find optimal migration paths
//!
//! # Example
//!
//! ```rust
//! use confers::migration::{MigrationRegistry, Versioned};
//! use confers::types::{AnnotatedValue, ConfigValue, SourceId};
//!
//! // Define versioned configs
//! struct ConfigV1;
//! struct ConfigV2;
//!
//! impl Versioned for ConfigV1 { const VERSION: u32 = 1; }
//! impl Versioned for ConfigV2 { const VERSION: u32 = 2; }
//!
//! // Create a test value
//! let value = AnnotatedValue::new(
//!     ConfigValue::null(),
//!     SourceId::new("test"),
//!     "test"
//! );
//!
//! // Create registry and register migrations
//! let mut registry = MigrationRegistry::new();
//! registry.register(1, 2, |mut v| {
//!     // Migrate from v1 to v2
//!     v.version = 2;
//!     Ok(v)
//! });
//!
//! // Precompute paths and migrate
//! registry.precompute_paths();
//! let result = registry.migrate(value, 1, 2);
//! ```

use crate::error::{ConfigError, ConfigResult};
use crate::types::AnnotatedValue;
use std::collections::{BTreeMap, HashMap};

/// Versioned configuration trait.
///
/// Implement this trait on your configuration structs to declare their version.
/// The version is used for migration tracking and automatic schema evolution.
///
/// # Example
///
/// ```rust
/// use confers::migration::Versioned;
///
/// struct AppConfigV1 { /* fields */ }
/// struct AppConfigV2 { /* fields */ }
///
/// impl Versioned for AppConfigV1 { const VERSION: u32 = 1; }
/// impl Versioned for AppConfigV2 { const VERSION: u32 = 2; }
/// ```
pub trait Versioned {
    /// The version number of this configuration schema.
    const VERSION: u32;
}

/// Migration function type.
///
/// Functions of this type transform a configuration from one version to another.
/// They receive the source annotated value and return the migrated value or an error.
///
/// Note: This is stored as a boxed closure internally to support both simple function
/// pointers and closures that capture environment variables.
pub type MigrationFn = Box<dyn FnMut(AnnotatedValue) -> ConfigResult<AnnotatedValue> + Send + Sync>;

/// Migration registry for managing version migrations.
///
/// This struct maintains a collection of migration functions and precomputes
/// optimal migration paths between versions using BFS.
///
/// # DI Patterns (following di.md)
///
/// - [`new()`][MigrationRegistry::new] - Zero-config default
/// - [`builder()`][MigrationRegistry::builder] - Builder pattern for fluent configuration
/// - [`with_migrations()`][MigrationRegistry::with_migrations] - Full dependency injection
pub struct MigrationRegistry {
    /// Migration functions keyed by (from_version, to_version).
    migrations: BTreeMap<(u32, u32), MigrationFn>,
    /// Precomputed migration paths for fast lookups.
    path_cache: HashMap<(u32, u32), Vec<(u32, u32)>>,
    /// All available versions (for path computation).
    versions: BTreeMap<u32, Vec<u32>>,
}

impl MigrationRegistry {
    /// Create a new empty migration registry.
    ///
    /// # Example
    ///
    /// ```rust
    /// use confers::migration::MigrationRegistry;
    ///
    /// let registry = MigrationRegistry::new();
    /// ```
    pub fn new() -> Self {
        Self {
            migrations: BTreeMap::new(),
            path_cache: HashMap::new(),
            versions: BTreeMap::new(),
        }
    }

    /// Create a builder for fluent registration.
    ///
    /// # Example
    ///
    /// ```rust
    /// use confers::migration::MigrationRegistry;
    ///
    /// let registry = MigrationRegistry::builder()
    ///     .register(1, 2, |v| Ok(v))
    ///     .register(2, 3, |v| Ok(v))
    ///     .build();
    /// ```
    pub fn builder() -> MigrationRegistryBuilder {
        MigrationRegistryBuilder::new()
    }

    /// Create a registry with pre-injected migrations.
    ///
    /// # Example
    ///
    /// ```rust
    /// use confers::migration::{MigrationRegistry, MigrationFn};
    /// use std::collections::HashMap;
    ///
    /// let mut migrations: HashMap<(u32, u32), MigrationFn> = HashMap::new();
    /// migrations.insert((1, 2), Box::new(|v| Ok(v)));
    ///
    /// let registry = MigrationRegistry::with_migrations(migrations);
    /// ```
    pub fn with_migrations(migrations: HashMap<(u32, u32), MigrationFn>) -> Self {
        let mut registry = Self::new();
        for ((from, to), f) in migrations {
            registry.register(from, to, f);
        }
        registry
    }

    /// Register a migration function from one version to another.
    ///
    /// Returns `self` for method chaining.
    ///
    /// # Example
    ///
    /// ```rust
    /// use confers::migration::MigrationRegistry;
    ///
    /// let mut registry = MigrationRegistry::new();
    /// registry
    ///     .register(1, 2, |mut v| { v.version = 2; Ok(v) })
    ///     .register(2, 3, |mut v| { v.version = 3; Ok(v) });
    /// ```
    pub fn register<F>(&mut self, from: u32, to: u32, f: F) -> &mut Self
    where
        F: FnMut(AnnotatedValue) -> ConfigResult<AnnotatedValue> + Send + Sync + 'static,
    {
        let boxed: MigrationFn = Box::new(f);
        self.migrations.insert((from, to), boxed);

        // Update adjacency list - add 'from' -> 'to'
        self.versions.entry(from).or_default().push(to);
        // Also ensure 'to' exists as a key (might be target-only)
        self.versions.entry(to).or_default();

        // Clear path cache when migrations change
        self.path_cache.clear();

        self
    }

    /// Get the migrations map (for testing/inspection).
    pub fn migrations(&self) -> &BTreeMap<(u32, u32), MigrationFn> {
        &self.migrations
    }

    /// Precompute all migration paths using BFS.
    ///
    /// This method should be called after all migrations are registered
    /// and before any migration execution. It builds an optimal path lookup table.
    ///
    /// # Example
    ///
    /// ```rust
    /// use confers::migration::MigrationRegistry;
    ///
    /// let mut registry = MigrationRegistry::new();
    /// registry.register(1, 2, |v| Ok(v));
    /// registry.register(2, 3, |v| Ok(v));
    ///
    /// registry.precompute_paths();
    ///
    /// let path = registry.get_migration_path(1, 3);
    /// assert!(path.is_some());
    /// ```
    pub fn precompute_paths(&mut self) {
        for (&from, targets) in &self.versions {
            for &to in targets {
                self.path_cache.insert((from, to), vec![(from, to)]);
            }
        }

        let all_versions: std::collections::HashSet<u32> = {
            let mut set = std::collections::HashSet::new();
            for (&from, targets) in &self.versions {
                set.insert(from);
                for &to in targets {
                    set.insert(to);
                }
            }
            set
        };
        let versions: Vec<u32> = all_versions.into_iter().collect();

        for _ in 0..versions.len() {
            let mut updated = false;
            for &mid in &versions {
                for &from in &versions {
                    for &to in &versions {
                        if from == to {
                            continue;
                        }

                        let path_from_mid = self.path_cache.get(&(from, mid)).cloned();
                        let path_mid_to = self.path_cache.get(&(mid, to)).cloned();

                        if let (Some(p1), Some(p2)) = (path_from_mid, path_mid_to) {
                            let new_path_len = p1.len() + p2.len();
                            let should_update = match self.path_cache.get(&(from, to)) {
                                Some(existing) => new_path_len < existing.len(),
                                None => true,
                            };

                            if should_update {
                                let mut new_path = p1;
                                new_path.extend(p2);
                                self.path_cache.insert((from, to), new_path);
                                updated = true;
                            }
                        }
                    }
                }
            }
            if !updated {
                break;
            }
        }
    }

    /// Get the migration path from one version to another.
    ///
    /// Returns `None` if no path exists.
    ///
    /// # Example
    ///
    /// ```rust
    /// use confers::migration::MigrationRegistry;
    ///
    /// let mut registry = MigrationRegistry::new();
    /// registry.register(1, 2, |v| Ok(v));
    /// registry.register(2, 3, |v| Ok(v));
    /// registry.precompute_paths();
    ///
    /// let path = registry.get_migration_path(1, 3);
    /// assert_eq!(path, Some(vec![1, 2, 3]));
    /// ```
    pub fn get_migration_path(&self, from: u32, to: u32) -> Option<Vec<u32>> {
        if from == to {
            return Some(vec![from]);
        }

        // Get edges from path cache
        let edges = self.path_cache.get(&(from, to))?;

        // Convert edges to version sequence
        let mut versions = vec![from];
        for &(_v_from, v_to) in edges {
            versions.push(v_to);
        }

        Some(versions)
    }

    /// Execute migration from one version to another.
    ///
    /// This method automatically finds and executes the optimal migration path.
    ///
    /// # Example
    ///
    /// ```rust
    /// use confers::migration::MigrationRegistry;
    /// use confers::types::{AnnotatedValue, ConfigValue, SourceId};
    ///
    /// let mut registry = MigrationRegistry::new();
    /// registry.register(1, 2, |mut v| { v.version = 2; Ok(v) });
    /// registry.precompute_paths();
    ///
    /// let value = AnnotatedValue::new(ConfigValue::null(), SourceId::new("test"), "test");
    /// let result = registry.migrate(value, 1, 2);
    /// assert!(result.is_ok());
    /// ```
    pub fn migrate(
        &mut self,
        mut value: AnnotatedValue,
        from: u32,
        to: u32,
    ) -> ConfigResult<AnnotatedValue> {
        if from == to {
            return Ok(value);
        }

        // Get migration path
        let path = self
            .get_migration_path(from, to)
            .ok_or_else(|| ConfigError::migration_not_found(from, to))?;

        // Execute migrations along the path
        for i in 0..path.len() - 1 {
            let current_version = path[i];
            let next_version = path[i + 1];

            // Get mutable reference to the migration function
            let migration = self
                .migrations
                .get_mut(&(current_version, next_version))
                .ok_or_else(|| ConfigError::migration_not_found(current_version, next_version))?;

            value = migration(value)?;
        }

        Ok(value)
    }
}

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

/// Builder for MigrationRegistry.
///
/// Provides a fluent API for registering migrations.
pub struct MigrationRegistryBuilder {
    migrations: HashMap<(u32, u32), MigrationFn>,
}

impl MigrationRegistryBuilder {
    /// Create a new builder.
    pub fn new() -> Self {
        Self {
            migrations: HashMap::new(),
        }
    }

    /// Register a migration.
    pub fn register<F>(mut self, from: u32, to: u32, f: F) -> Self
    where
        F: FnMut(AnnotatedValue) -> ConfigResult<AnnotatedValue> + Send + Sync + 'static,
    {
        let boxed: MigrationFn = Box::new(f);
        self.migrations.insert((from, to), boxed);
        self
    }

    /// Build the MigrationRegistry.
    pub fn build(self) -> MigrationRegistry {
        MigrationRegistry::with_migrations(self.migrations)
    }
}

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

/// Migration behavior on configuration reload.
///
/// Controls when migrations are applied during hot reload scenarios.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MigrationOnReload {
    /// Always migrate on every reload.
    ///
    /// This ensures the configuration is always at the latest version,
    /// but may be slower for frequent reloads.
    Always,

    /// Only migrate when version changes.
    ///
    /// This is the default behavior and provides a balance between
    /// correctness and performance.
    #[default]
    OnVersionChange,

    /// Disable migration on reload.
    ///
    /// Use this when you want to manually manage version migrations
    /// or when the configuration format is stable.
    Disabled,
}

// ============================================================================
// ConfigError extensions for migration
// ============================================================================

impl ConfigError {
    /// Create a migration not found error.
    pub fn migration_not_found(from: u32, to: u32) -> Self {
        ConfigError::MigrationFailed {
            from,
            to,
            reason: format!(
                "No migration path found from version {} to version {}",
                from, to
            ),
            source: None,
        }
    }

    /// Create a migration failed error.
    pub fn migration_failed(from: u32, to: u32, reason: impl Into<String>) -> Self {
        ConfigError::MigrationFailed {
            from,
            to,
            reason: reason.into(),
            source: None,
        }
    }
}