nizaam-core 1.0.1

Domain-agnostic Rust infrastructure core for building secure, observable, reliable, and extensible Nizaam engines.
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
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
use std::fmt;
use std::sync::{
    Arc,
    atomic::{AtomicU64, Ordering},
};

use super::loader::LoadedConfiguration;
use super::parser::{ConfigurationParser, ParseErrors};
use super::resolution::{ConfigurationResolver, ResolutionErrors};
use super::snapshot::{ConfigurationSnapshot, ConfigurationSnapshotId};
use super::validation::{
    ConfigurationValidator, SemanticValidator, ValidatedConfiguration, ValidationErrors,
};

trait ErasedSemanticValidator: fmt::Debug + Send + Sync {
    fn validate(&self, configuration: &ValidatedConfiguration) -> Result<(), String>;
}

impl<V> ErasedSemanticValidator for V
where
    V: SemanticValidator + fmt::Debug + Send + Sync + 'static,
    V::Error: fmt::Display,
{
    fn validate(&self, configuration: &ValidatedConfiguration) -> Result<(), String> {
        SemanticValidator::validate(self, configuration).map_err(|error| error.to_string())
    }
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
struct ConfigurationUpdaterId(u64);

impl ConfigurationUpdaterId {
    fn new() -> Self {
        static NEXT_ID: AtomicU64 = AtomicU64::new(1);
        Self(NEXT_ID.fetch_add(1, Ordering::Relaxed))
    }
}

/// Orchestrates configuration proposals through parsing, validation,
/// resolution, snapshot creation, and atomic activation.
///
/// The updater never mutates an existing `ConfigurationSnapshot`. A successful
/// update publishes a new snapshot generation, while a failed update leaves
/// the currently active snapshot unchanged.
#[derive(Clone, Debug)]
pub struct ConfigurationUpdater {
    parser: ConfigurationParser,
    validator: ConfigurationValidator,
    resolver: ConfigurationResolver,
    semantic_validator: Option<Arc<dyn ErasedSemanticValidator>>,
    current: ConfigurationSnapshot,
    lineage: ConfigurationUpdaterId,
}

impl ConfigurationUpdater {
    /// Creates an updater with an already resolved initial snapshot.
    #[must_use]
    pub fn new(
        parser: ConfigurationParser,
        validator: ConfigurationValidator,
        resolver: ConfigurationResolver,
        initial_snapshot: ConfigurationSnapshot,
    ) -> Self {
        Self {
            parser,
            validator,
            resolver,
            semantic_validator: None,
            current: initial_snapshot,
            lineage: ConfigurationUpdaterId::new(),
        }
    }

    /// Attaches an engine-specific semantic validation boundary to updates.
    pub fn with_semantic_validator<V>(mut self, validator: V) -> Self
    where
        V: SemanticValidator + fmt::Debug + Send + Sync + 'static,
        V::Error: fmt::Display,
    {
        self.semantic_validator = Some(Arc::new(validator));
        self.lineage = ConfigurationUpdaterId::new();
        self
    }

    /// Returns the currently active immutable configuration snapshot.
    #[must_use]
    pub fn current(&self) -> &ConfigurationSnapshot {
        &self.current
    }

    /// Prepares a complete configuration proposal without activating it.
    ///
    /// Parsing, structural validation, semantic validation when configured, and
    /// resolution happen before any active state is changed. A successful
    /// preparation produces the next snapshot generation and can later be
    /// activated atomically.
    pub fn prepare(
        &self,
        proposal: &LoadedConfiguration,
    ) -> Result<PreparedConfigurationUpdate, ConfigurationUpdateError> {
        let parsed = self
            .parser
            .parse(proposal)
            .map_err(ConfigurationUpdateError::Parse)?;

        let validated = self
            .validator
            .validate(parsed)
            .map_err(ConfigurationUpdateError::Validation)?;

        if let Some(validator) = &self.semantic_validator {
            validator
                .validate(&validated)
                .map_err(|message| ConfigurationUpdateError::SemanticValidation { message })?;
        }

        let resolved = self
            .resolver
            .resolve(&validated)
            .map_err(ConfigurationUpdateError::Resolution)?;

        let next_id = next_snapshot_id(self.current.id())
            .ok_or(ConfigurationUpdateError::SnapshotIdExhausted)?;

        let snapshot = ConfigurationSnapshot::new(next_id, resolved);

        Ok(PreparedConfigurationUpdate {
            expected_current: self.current.id(),
            snapshot,
            lineage: self.lineage,
        })
    }

    /// Activates a previously prepared update only if it belongs to this updater
    /// and its expected current snapshot is still active.
    ///
    /// This provides optimistic conflict detection: a prepared proposal cannot
    /// overwrite a configuration snapshot that became active after it was
    /// prepared.
    pub fn activate(
        &mut self,
        prepared: PreparedConfigurationUpdate,
    ) -> Result<ConfigurationUpdateResult, ConfigurationUpdateError> {
        let actual = self.current.id();

        if self.lineage != prepared.lineage {
            return Err(ConfigurationUpdateError::LineageMismatch);
        }

        if actual != prepared.expected_current {
            return Err(ConfigurationUpdateError::Conflict {
                expected: prepared.expected_current,
                actual,
            });
        }

        let previous = self.current.id();
        let next = prepared.snapshot.id();

        self.current = prepared.snapshot;

        Ok(ConfigurationUpdateResult {
            previous,
            current: next,
        })
    }

    /// Prepares and activates a complete configuration proposal.
    ///
    /// The active snapshot is changed only after parsing, structural validation,
    /// configured semantic validation, resolution, and snapshot construction
    /// have all succeeded.
    pub fn update(
        &mut self,
        proposal: &LoadedConfiguration,
    ) -> Result<ConfigurationUpdateResult, ConfigurationUpdateError> {
        let prepared = self.prepare(proposal)?;
        self.activate(prepared)
    }
}

/// A configuration update that has completed parsing, validation, resolution,
/// and snapshot construction but has not yet been activated.
#[derive(Clone, Debug, PartialEq)]
pub struct PreparedConfigurationUpdate {
    expected_current: ConfigurationSnapshotId,
    snapshot: ConfigurationSnapshot,
    lineage: ConfigurationUpdaterId,
}

impl PreparedConfigurationUpdate {
    /// Returns the snapshot generation that was current during preparation.
    #[must_use]
    pub const fn expected_current(&self) -> ConfigurationSnapshotId {
        self.expected_current
    }

    /// Returns the candidate snapshot that will be activated.
    #[must_use]
    pub fn snapshot(&self) -> &ConfigurationSnapshot {
        &self.snapshot
    }
}

/// Result of a successfully activated configuration update.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ConfigurationUpdateResult {
    previous: ConfigurationSnapshotId,
    current: ConfigurationSnapshotId,
}

impl ConfigurationUpdateResult {
    #[must_use]
    pub const fn previous(&self) -> ConfigurationSnapshotId {
        self.previous
    }

    #[must_use]
    pub const fn current(&self) -> ConfigurationSnapshotId {
        self.current
    }
}

/// Failure encountered while preparing or activating a configuration update.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ConfigurationUpdateError {
    Parse(ParseErrors),
    Validation(ValidationErrors),
    Resolution(ResolutionErrors),
    SemanticValidation {
        message: String,
    },
    LineageMismatch,
    Conflict {
        expected: ConfigurationSnapshotId,
        actual: ConfigurationSnapshotId,
    },
    SnapshotIdExhausted,
}

impl fmt::Display for ConfigurationUpdateError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Parse(errors) => write!(f, "configuration update parsing failed: {errors}"),
            Self::Validation(errors) => {
                write!(f, "configuration update validation failed: {errors}")
            }
            Self::Resolution(errors) => {
                write!(f, "configuration update resolution failed: {errors}")
            }
            Self::SemanticValidation { message } => {
                write!(
                    f,
                    "configuration update semantic validation failed: {message}"
                )
            }
            Self::LineageMismatch => {
                f.write_str("configuration update prepared by a different updater")
            }
            Self::Conflict { expected, actual } => write!(
                f,
                "configuration update conflict: expected snapshot {expected}, \
                 but current snapshot is {actual}"
            ),
            Self::SnapshotIdExhausted => {
                f.write_str("configuration snapshot identifier is exhausted")
            }
        }
    }
}

impl std::error::Error for ConfigurationUpdateError {}

fn next_snapshot_id(current: ConfigurationSnapshotId) -> Option<ConfigurationSnapshotId> {
    current
        .value()
        .checked_add(1)
        .map(ConfigurationSnapshotId::new)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::validation::{ConfigurationValue, ParsedConfiguration};

    fn loaded(values: &[(&str, &str)]) -> LoadedConfiguration {
        LoadedConfiguration::from_test_values(
            values
                .iter()
                .map(|(key, value)| ((*key).to_owned(), (*value).to_owned())),
        )
    }

    fn updater(initial: &[(&str, ConfigurationValue)]) -> ConfigurationUpdater {
        let mut parsed = ParsedConfiguration::empty();

        for (key, value) in initial {
            parsed.insert((*key).to_owned(), value.clone());
        }

        let validated = ConfigurationValidator::new()
            .validate(parsed)
            .expect("test configuration should validate");

        let resolved = ConfigurationResolver::new()
            .resolve(&validated)
            .expect("test configuration should resolve");

        let snapshot = ConfigurationSnapshot::new(ConfigurationSnapshotId::new(0), resolved);

        let parser = ConfigurationParser::new()
            .with_type("PORT", crate::config::parser::ConfigurationType::Integer)
            .expect("test parser type declaration should succeed");

        ConfigurationUpdater::new(
            parser,
            ConfigurationValidator::new(),
            ConfigurationResolver::new(),
            snapshot,
        )
    }

    #[test]
    fn updater_exposes_initial_snapshot() {
        let updater = updater(&[("PORT", ConfigurationValue::Integer(8080))]);

        assert_eq!(updater.current().id().value(), 0);
        assert_eq!(
            updater
                .current()
                .get("PORT")
                .and_then(ConfigurationValue::as_integer),
            Some(8080)
        );
    }

    #[test]
    fn successful_update_creates_next_snapshot() {
        let mut updater = updater(&[]);

        let result = updater
            .update(&loaded(&[("PORT", "9090")]))
            .expect("update should succeed");

        assert_eq!(result.previous().value(), 0);
        assert_eq!(result.current().value(), 1);
        assert_eq!(
            updater
                .current()
                .get("PORT")
                .and_then(ConfigurationValue::as_integer),
            Some(9090)
        );
    }

    #[test]
    fn successful_update_does_not_mutate_old_snapshot() {
        let mut updater = updater(&[("PORT", ConfigurationValue::Integer(8080))]);

        let old = updater.current().clone();

        updater
            .update(&loaded(&[("PORT", "9090")]))
            .expect("update should succeed");

        assert_eq!(
            old.get("PORT").and_then(ConfigurationValue::as_integer),
            Some(8080)
        );
        assert_eq!(old.id().value(), 0);
    }

    #[test]
    fn parse_failure_preserves_current_snapshot() {
        let mut updater = updater(&[("PORT", ConfigurationValue::Integer(8080))]);

        let parser = ConfigurationParser::new()
            .with_type("PORT", crate::config::parser::ConfigurationType::Integer)
            .expect("type declaration should succeed");

        updater.parser = parser;

        let error = updater
            .update(&loaded(&[("PORT", "not-an-integer")]))
            .expect_err("update should fail");

        assert!(matches!(error, ConfigurationUpdateError::Parse(_)));
        assert_eq!(updater.current().id().value(), 0);
        assert_eq!(
            updater
                .current()
                .get("PORT")
                .and_then(ConfigurationValue::as_integer),
            Some(8080)
        );
    }

    #[test]
    fn validation_failure_preserves_current_snapshot() {
        let mut updater = updater(&[]);
        updater.validator = ConfigurationValidator::new().require_key("REQUIRED");

        let error = updater
            .update(&loaded(&[("OTHER", "value")]))
            .expect_err("validation should fail");

        assert!(matches!(error, ConfigurationUpdateError::Validation(_)));
        assert_eq!(updater.current().id().value(), 0);
    }

    #[test]
    fn resolution_failure_preserves_current_snapshot() {
        let mut updater = updater(&[]);

        let error = updater
            .update(&loaded(&[("VALUE", "${MISSING}")]))
            .expect_err("resolution should fail");

        assert!(matches!(error, ConfigurationUpdateError::Resolution(_)));
        assert_eq!(updater.current().id().value(), 0);
    }

    #[test]
    fn prepare_does_not_activate_configuration() {
        let updater = updater(&[]);

        let prepared = updater
            .prepare(&loaded(&[("PORT", "9090")]))
            .expect("preparation should succeed");

        assert_eq!(updater.current().id().value(), 0);
        assert_eq!(prepared.expected_current().value(), 0);
        assert_eq!(prepared.snapshot().id().value(), 1);
        assert_eq!(
            prepared
                .snapshot()
                .get("PORT")
                .and_then(ConfigurationValue::as_integer),
            Some(9090)
        );
    }

    #[test]
    fn prepared_update_can_be_activated() {
        let mut updater = updater(&[]);

        let prepared = updater
            .prepare(&loaded(&[("PORT", "9090")]))
            .expect("preparation should succeed");

        let result = updater
            .activate(prepared)
            .expect("activation should succeed");

        assert_eq!(result.previous().value(), 0);
        assert_eq!(result.current().value(), 1);
    }

    #[test]
    fn stale_prepared_update_is_rejected() {
        let mut updater = updater(&[]);

        let first = updater
            .prepare(&loaded(&[("PORT", "8080")]))
            .expect("first preparation should succeed");

        let second = updater
            .prepare(&loaded(&[("PORT", "9090")]))
            .expect("second preparation should succeed");

        updater
            .activate(first)
            .expect("first activation should succeed");

        let error = updater
            .activate(second)
            .expect_err("stale activation should fail");

        assert_eq!(
            error,
            ConfigurationUpdateError::Conflict {
                expected: ConfigurationSnapshotId::new(0),
                actual: ConfigurationSnapshotId::new(1),
            }
        );

        assert_eq!(
            updater
                .current()
                .get("PORT")
                .and_then(ConfigurationValue::as_integer),
            Some(8080)
        );
    }

    #[test]
    fn failed_activation_does_not_change_current_snapshot() {
        let mut updater = updater(&[]);

        let first = updater
            .prepare(&loaded(&[("PORT", "8080")]))
            .expect("preparation should succeed");

        let second = updater
            .prepare(&loaded(&[("PORT", "9090")]))
            .expect("preparation should succeed");

        updater
            .activate(first)
            .expect("first activation should succeed");

        let _ = updater.activate(second);

        assert_eq!(updater.current().id().value(), 1);
        assert_eq!(
            updater
                .current()
                .get("PORT")
                .and_then(ConfigurationValue::as_integer),
            Some(8080)
        );
    }

    #[test]
    fn snapshot_ids_progress_monotonically() {
        let mut updater = updater(&[]);

        for expected in 1..=3 {
            let result = updater
                .update(&loaded(&[("PORT", "8080")]))
                .expect("update should succeed");

            assert_eq!(result.current().value(), expected);
        }
    }

    #[derive(Debug)]
    struct PortRangeValidator;

    #[derive(Debug)]
    struct PortRangeError;

    impl fmt::Display for PortRangeError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.write_str("PORT must be between 1 and 65535")
        }
    }

    impl SemanticValidator for PortRangeValidator {
        type Error = PortRangeError;

        fn validate(&self, configuration: &ValidatedConfiguration) -> Result<(), Self::Error> {
            match configuration.get("PORT") {
                Some(ConfigurationValue::Integer(port)) if (1..=65_535).contains(port) => Ok(()),
                _ => Err(PortRangeError),
            }
        }
    }

    #[test]
    fn semantic_validation_runs_before_candidate_activation() {
        let mut updater = updater(&[]).with_semantic_validator(PortRangeValidator);

        let error = updater
            .update(&loaded(&[("PORT", "70000")]))
            .expect_err("semantic validation must reject an invalid port");

        assert_eq!(
            error,
            ConfigurationUpdateError::SemanticValidation {
                message: "PORT must be between 1 and 65535".to_owned(),
            }
        );
        assert_eq!(updater.current().id().value(), 0);
    }

    #[test]
    fn changing_semantic_validation_rejects_previous_prepared_update() {
        let original = updater(&[]);
        let prepared = original
            .prepare(&loaded(&[("PORT", "9090")]))
            .expect("preparation should succeed");

        let mut changed = original.with_semantic_validator(PortRangeValidator);

        assert_eq!(
            changed
                .activate(prepared)
                .expect_err("changing the semantic policy must reject the old candidate"),
            ConfigurationUpdateError::LineageMismatch
        );
        assert_eq!(changed.current().id().value(), 0);
    }

    #[test]
    fn prepared_update_cannot_be_activated_by_another_updater() {
        let first = updater(&[]);
        let mut second = updater(&[]);

        let prepared = first
            .prepare(&loaded(&[("PORT", "9090")]))
            .expect("preparation should succeed");

        assert_eq!(
            second
                .activate(prepared)
                .expect_err("cross-updater activation must fail"),
            ConfigurationUpdateError::LineageMismatch
        );
        assert_eq!(second.current().id().value(), 0);
    }

    #[test]
    fn snapshot_id_overflow_is_detected() {
        assert_eq!(
            next_snapshot_id(ConfigurationSnapshotId::new(u64::MAX)),
            None
        );
    }

    #[test]
    fn prepared_snapshot_remains_immutable_after_activation() {
        let mut updater = updater(&[]);

        let prepared = updater
            .prepare(&loaded(&[("PORT", "9090")]))
            .expect("preparation should succeed");

        let prepared_snapshot = prepared.snapshot().clone();

        updater
            .activate(prepared)
            .expect("activation should succeed");

        assert_eq!(
            prepared_snapshot
                .get("PORT")
                .and_then(ConfigurationValue::as_integer),
            Some(9090)
        );
        assert_eq!(prepared_snapshot.id().value(), 1);
    }

    #[test]
    fn update_errors_are_safe_to_render() {
        let error = ConfigurationUpdateError::Conflict {
            expected: ConfigurationSnapshotId::new(1),
            actual: ConfigurationSnapshotId::new(2),
        };

        assert_eq!(
            error.to_string(),
            "configuration update conflict: expected snapshot 1, \
             but current snapshot is 2"
        );
    }

    #[test]
    fn current_snapshot_is_not_replaced_during_failed_preparation() {
        let mut updater = updater(&[("PORT", ConfigurationValue::Integer(8080))]);

        updater.validator = ConfigurationValidator::new().require_key("REQUIRED");

        let _ = updater.update(&loaded(&[("PORT", "9090")]));

        assert_eq!(updater.current().id().value(), 0);
        assert_eq!(
            updater
                .current()
                .get("PORT")
                .and_then(ConfigurationValue::as_integer),
            Some(8080)
        );
    }
}