Skip to main content

qubit_redact/domain/
redact_map_value_mut.rs

1// =============================================================================
2//    Copyright (c) 2025 - 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Logical in-place redaction contract for text-valued map-like containers.
9
10use crate::{
11    RedactValueMut,
12    RedactionPolicy,
13};
14
15/// Redacts map values in place after classifying each value by its runtime key.
16///
17/// This provides logical replacement only; see [`RedactValueMut`] for its
18/// memory-erasure boundary.
19///
20/// # Type Parameters
21///
22/// * `K` - Runtime map-key type used for field classification.
23/// * `V` - Mutable map-value type redacted in place.
24pub trait RedactMapValueMut<K: ?Sized, V: ?Sized> {
25    /// Replaces sensitive values according to `policy`.
26    ///
27    /// # Parameters
28    ///
29    /// * `policy` - Complete policy used to classify every runtime key.
30    fn redact_map_in_place(&mut self, policy: &RedactionPolicy);
31}
32
33impl<M: ?Sized, K: ?Sized, V: ?Sized> RedactMapValueMut<K, V> for M
34where
35    for<'a> &'a mut M: IntoIterator<Item = (&'a K, &'a mut V)>,
36    K: AsRef<str>,
37    V: RedactValueMut,
38{
39    /// Replaces sensitive entry values according to their runtime keys.
40    ///
41    /// # Parameters
42    ///
43    /// * `policy` - Complete policy used to classify every runtime key.
44    #[inline]
45    fn redact_map_in_place(&mut self, policy: &RedactionPolicy) {
46        for (key, value) in self {
47            if let Some(level) = policy.sensitivity_for(key.as_ref()) {
48                value.redact_value_in_place(level, policy.masking());
49            }
50        }
51    }
52}