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
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Four-level immutable masking configuration.
use std::borrow::Cow;
use super::{
MaskPolicy,
Sensitivity,
};
/// Mask policies assigned to all supported sensitivity levels.
#[must_use]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MaskingPolicy {
/// Policy for low-sensitivity values.
low: MaskPolicy,
/// Policy for medium-sensitivity values.
medium: MaskPolicy,
/// Policy for high-sensitivity values.
high: MaskPolicy,
/// Policy for secret values.
secret: MaskPolicy,
}
impl MaskingPolicy {
/// Creates a four-level masking configuration from the supplied policies.
///
/// # Parameters
///
/// * `low` - Policy for low-sensitivity values.
/// * `medium` - Policy for medium-sensitivity values.
/// * `high` - Policy for high-sensitivity values.
/// * `secret` - Policy for secret values.
///
/// # Returns
///
/// A masking configuration containing the supplied policies.
#[inline(always)]
pub const fn new(
low: MaskPolicy,
medium: MaskPolicy,
high: MaskPolicy,
secret: MaskPolicy,
) -> Self {
Self {
low,
medium,
high,
secret,
}
}
/// Returns a copy with the policy for `level` replaced by `policy`.
///
/// # Parameters
///
/// * `level` - Sensitivity level to update.
/// * `policy` - Replacement mask policy.
///
/// # Returns
///
/// The updated immutable masking configuration.
#[inline]
pub fn with_policy(
mut self,
level: Sensitivity,
policy: MaskPolicy,
) -> Self {
match level {
Sensitivity::Low => self.low = policy,
Sensitivity::Medium => self.medium = policy,
Sensitivity::High => self.high = policy,
Sensitivity::Secret => self.secret = policy,
}
self
}
/// Masks `value` with the policy configured for `level`.
///
/// Empty values remain empty; otherwise the selected policy determines
/// whether the result borrows or owns its contents.
///
/// # Type Parameters
///
/// * `'a` - Lifetime of the input and any borrowed result.
///
/// # Parameters
///
/// * `level` - Sensitivity level selecting the mask policy.
/// * `value` - Value to mask.
///
/// # Returns
///
/// The borrowed empty input or an owned masked value.
#[must_use = "use the returned masked value instead of the original value"]
#[inline(always)]
pub fn mask<'a>(&self, level: Sensitivity, value: &'a str) -> Cow<'a, str> {
self.for_level(level).mask(value)
}
/// Returns the configured complete replacement for an opaque value.
///
/// This never reads the original value. Edge-preserving policies return
/// only their replacement text because no prefix or suffix is safe to
/// retain when the value is opaque.
///
/// # Parameters
///
/// * `level` - Sensitivity level selecting the mask policy.
///
/// # Returns
///
/// The complete replacement configured for `level`.
#[must_use = "use the opaque replacement instead of formatting the original value"]
#[inline(always)]
pub fn mask_opaque(&self, level: Sensitivity) -> &str {
self.for_level(level).opaque_mask()
}
/// Masks a value without allocating beyond a byte limit.
///
/// # Type Parameters
///
/// * `'a` - Lifetime of the input and any borrowed result.
///
/// # Parameters
///
/// * `level` - Sensitivity level selecting the mask policy.
/// * `value` - Value to mask.
/// * `max_bytes` - Maximum bytes allocated for the masked result.
///
/// # Returns
///
/// The borrowed empty input or an owned mask bounded by `max_bytes`.
#[must_use = "use the returned bounded mask instead of the original value"]
#[inline(always)]
pub(crate) fn mask_bounded<'a>(
&self,
level: Sensitivity,
value: &'a str,
max_bytes: usize,
) -> Cow<'a, str> {
self.for_level(level).mask_bounded(value, max_bytes)
}
/// Returns an opaque replacement constrained to `max_bytes`.
///
/// # Parameters
///
/// * `level` - Sensitivity level selecting the mask policy.
/// * `max_bytes` - Maximum bytes retained from the replacement.
///
/// # Returns
///
/// An owned bounded prefix of the configured opaque replacement.
#[must_use = "use the bounded opaque replacement instead of the original value"]
#[inline(always)]
pub(crate) fn mask_opaque_bounded(
&self,
level: Sensitivity,
max_bytes: usize,
) -> String {
self.for_level(level).opaque_mask_bounded(max_bytes)
}
/// Returns the mask policy configured for `level`.
///
/// # Parameters
///
/// * `level` - Sensitivity level to resolve.
///
/// # Returns
///
/// The mask policy assigned to `level`.
#[must_use = "use the mask policy selected for this sensitivity level"]
#[inline(always)]
pub const fn for_level(&self, level: Sensitivity) -> &MaskPolicy {
match level {
Sensitivity::Low => &self.low,
Sensitivity::Medium => &self.medium,
Sensitivity::High => &self.high,
Sensitivity::Secret => &self.secret,
}
}
}
impl Default for MaskingPolicy {
/// Creates the built-in conservative four-level masking configuration.
///
/// # Returns
///
/// The built-in masking configuration.
fn default() -> Self {
Self::new(
MaskPolicy::preserve_edges(2, 2, "****", 4),
MaskPolicy::preserve_suffix(1, "*******", 1),
MaskPolicy::fixed("****"),
MaskPolicy::fixed("<redacted>"),
)
}
}