redactable 0.7.1

Automatic redaction of sensitive data in structs for safe logging and debugging
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
//! Text redaction strategies for string-like values.
//!
//! This module provides [`TextRedactionPolicy`] and its configuration types
//! for transforming sensitive strings. Policies are pure string transformations
//! that do not traverse structures or make runtime decisions about sensitivity.

use std::borrow::Cow;

/// Default placeholder used for full redaction.
pub const REDACTED_PLACEHOLDER: &str = "[REDACTED]";

/// Default character used to mask sensitive characters.
pub const MASK_CHAR: char = '*';

/// Configuration that keeps selected segments visible while masking the remainder.
///
/// The policy operates on Unicode scalar values. If the configuration keeps the
/// entire value visible, the output is unchanged.
///
/// Use the constructor methods [`KeepConfig::first`] and [`KeepConfig::last`]
/// to create instances.
#[derive(Clone, Copy, Debug)]
pub struct KeepConfig {
    /// Number of leading characters to keep visible.
    visible_prefix: usize,
    /// Number of trailing characters to keep visible.
    visible_suffix: usize,
    /// Symbol used to mask the middle.
    mask_char: char,
}

impl KeepConfig {
    /// Constructs a configuration that keeps only the first `visible_prefix` scalar values.
    #[must_use]
    pub fn first(visible_prefix: usize) -> Self {
        Self {
            visible_prefix,
            visible_suffix: 0,
            mask_char: MASK_CHAR,
        }
    }

    /// Constructs a configuration that keeps only the last `visible_suffix` scalar values.
    #[must_use]
    pub fn last(visible_suffix: usize) -> Self {
        Self {
            visible_prefix: 0,
            visible_suffix,
            mask_char: MASK_CHAR,
        }
    }

    /// Constructs a configuration that keeps both leading and trailing characters visible.
    ///
    /// If `visible_prefix + visible_suffix >= total_length`, the entire value
    /// is kept visible (no masking occurs).
    #[must_use]
    pub fn both(visible_prefix: usize, visible_suffix: usize) -> Self {
        Self {
            visible_prefix,
            visible_suffix,
            mask_char: MASK_CHAR,
        }
    }

    /// Uses a specific masking character.
    #[must_use]
    pub fn with_mask_char(mut self, mask_char: char) -> Self {
        self.mask_char = mask_char;
        self
    }

    /// Sets the masking character in place.
    pub(crate) fn set_mask_char(&mut self, mask_char: char) {
        self.mask_char = mask_char;
    }

    /// Applies the policy to a string value.
    ///
    /// Empty strings are fully redacted using [`REDACTED_PLACEHOLDER`].
    ///
    /// If `visible_prefix + visible_suffix >= total_length`, the entire value
    /// is kept visible (no masking occurs).
    pub(crate) fn apply_to(&self, value: &str) -> String {
        let mut chars: Vec<char> = value.chars().collect();
        let total = chars.len();
        if total == 0 {
            return REDACTED_PLACEHOLDER.to_string();
        }

        // If keep spans cover or exceed the total length, return unchanged
        if self.visible_prefix.saturating_add(self.visible_suffix) >= total {
            return chars.into_iter().collect();
        }

        // Mask the middle portion
        for ch in &mut chars[self.visible_prefix..(total - self.visible_suffix)] {
            *ch = self.mask_char;
        }
        chars.into_iter().collect()
    }
}

/// Configuration that masks selected segments while leaving the remainder unchanged.
///
/// Masking operates on Unicode scalar values and bounds the masked spans for
/// short inputs.
///
/// Use the constructor methods [`MaskConfig::first`] and [`MaskConfig::last`]
/// to create instances.
#[derive(Clone, Copy, Debug)]
#[allow(clippy::struct_field_names)] // Field names are descriptive for internal use
pub struct MaskConfig {
    /// Number of leading characters to mask.
    mask_prefix: usize,
    /// Number of trailing characters to mask.
    mask_suffix: usize,
    /// Symbol used to mask the selected segments.
    mask_char: char,
}

impl MaskConfig {
    /// Masks only the initial `mask_prefix` characters.
    #[must_use]
    pub fn first(mask_prefix: usize) -> Self {
        Self {
            mask_prefix,
            mask_suffix: 0,
            mask_char: MASK_CHAR,
        }
    }

    /// Masks only the final `mask_suffix` characters.
    #[must_use]
    pub fn last(mask_suffix: usize) -> Self {
        Self {
            mask_prefix: 0,
            mask_suffix,
            mask_char: MASK_CHAR,
        }
    }

    /// Masks both leading and trailing characters.
    ///
    /// If `mask_prefix + mask_suffix >= total_length`, the entire value
    /// is masked.
    #[must_use]
    pub fn both(mask_prefix: usize, mask_suffix: usize) -> Self {
        Self {
            mask_prefix,
            mask_suffix,
            mask_char: MASK_CHAR,
        }
    }

    /// Uses a specific masking character.
    #[must_use]
    pub fn with_mask_char(mut self, mask_char: char) -> Self {
        self.mask_char = mask_char;
        self
    }

    /// Sets the masking character in place.
    pub(crate) fn set_mask_char(&mut self, mask_char: char) {
        self.mask_char = mask_char;
    }

    /// Applies the policy to a string value.
    ///
    /// Empty strings are fully redacted using [`REDACTED_PLACEHOLDER`].
    ///
    /// If `mask_prefix + mask_suffix >= total_length`, the entire value
    /// is masked.
    pub(crate) fn apply_to(&self, value: &str) -> String {
        let mut chars: Vec<char> = value.chars().collect();
        let total = chars.len();
        if total == 0 {
            return REDACTED_PLACEHOLDER.to_string();
        }

        // If mask spans cover or exceed total length, mask everything
        if self.mask_prefix.saturating_add(self.mask_suffix) >= total {
            chars.fill(self.mask_char);
            return chars.into_iter().collect();
        }

        // Mask the prefix portion
        for ch in &mut chars[..self.mask_prefix] {
            *ch = self.mask_char;
        }

        // Mask the suffix portion
        if self.mask_suffix > 0 {
            let start = total - self.mask_suffix;
            for ch in &mut chars[start..] {
                *ch = self.mask_char;
            }
        }

        chars.into_iter().collect()
    }
}

/// Configuration for email address redaction.
///
/// Masks the local part (before `@`) while preserving the domain. If no `@` is
/// present, the input is masked like a prefix-keep policy.
#[derive(Clone, Copy, Debug)]
pub struct EmailConfig {
    /// Number of leading characters of the local part to keep visible.
    visible_prefix: usize,
    /// Symbol used to mask the local part.
    mask_char: char,
}

impl EmailConfig {
    /// Creates a new email config that keeps the first `visible_prefix` chars of the local part.
    #[must_use]
    pub fn new(visible_prefix: usize) -> Self {
        Self {
            visible_prefix,
            mask_char: MASK_CHAR,
        }
    }

    /// Uses a specific masking character.
    #[must_use]
    pub fn with_mask_char(mut self, mask_char: char) -> Self {
        self.mask_char = mask_char;
        self
    }

    /// Sets the masking character in place.
    pub(crate) fn set_mask_char(&mut self, mask_char: char) {
        self.mask_char = mask_char;
    }

    /// Applies the policy to an email address.
    ///
    /// If there's no `@`, the value is masked like a prefix-keep policy.
    pub(crate) fn apply_to(&self, value: &str) -> String {
        let chars: Vec<char> = value.chars().collect();
        let total = chars.len();
        if total == 0 {
            return REDACTED_PLACEHOLDER.to_string();
        }

        if let Some(at_pos) = value.find('@') {
            let local = &value[..at_pos];
            let domain = &value[at_pos..]; // includes the @

            let local_chars: Vec<char> = local.chars().collect();
            let local_len = local_chars.len();

            if self.visible_prefix >= local_len {
                // Keep entire local part
                return value.to_string();
            }

            let visible: String = local_chars[..self.visible_prefix].iter().collect();
            let masked_count = local_len - self.visible_prefix;
            let masked: String = std::iter::repeat_n(self.mask_char, masked_count).collect();

            format!("{visible}{masked}{domain}")
        } else {
            if self.visible_prefix >= total {
                return value.to_string();
            }

            let mut result = chars;
            for ch in &mut result[self.visible_prefix..] {
                *ch = self.mask_char;
            }
            result.into_iter().collect()
        }
    }
}

/// A redaction strategy for string-like values.
///
/// All strategies operate on Unicode scalar values and return an owned `String`.
// Use `Cow` so callers can provide borrowed or owned placeholders.
#[derive(Clone, Debug)]
pub enum TextRedactionPolicy {
    /// Replace the entire value with a fixed placeholder.
    Full {
        /// The placeholder text to use.
        placeholder: Cow<'static, str>,
    },
    /// Keep configured segments visible while masking everything else.
    Keep(KeepConfig),
    /// Mask configured segments while leaving the remainder untouched.
    Mask(MaskConfig),
    /// Email-specific: mask local part while preserving domain.
    Email(EmailConfig),
}

impl TextRedactionPolicy {
    /// Constructs [`TextRedactionPolicy::Full`] using [`REDACTED_PLACEHOLDER`].
    #[must_use]
    pub fn default_full() -> Self {
        Self::Full {
            placeholder: Cow::Borrowed(REDACTED_PLACEHOLDER),
        }
    }

    /// Constructs [`TextRedactionPolicy::Full`] using a custom placeholder.
    #[must_use]
    pub fn full_with<P>(placeholder: P) -> Self
    where
        P: Into<Cow<'static, str>>,
    {
        Self::Full {
            placeholder: placeholder.into(),
        }
    }

    /// Constructs [`TextRedactionPolicy::Keep`] from an explicit configuration.
    #[must_use]
    pub fn keep_with(config: KeepConfig) -> Self {
        Self::Keep(config)
    }

    /// Keeps only the first `visible_prefix` scalar values in clear text.
    #[must_use]
    pub fn keep_first(visible_prefix: usize) -> Self {
        Self::keep_with(KeepConfig::first(visible_prefix))
    }

    /// Keeps only the last `visible_suffix` scalar values in clear text.
    #[must_use]
    pub fn keep_last(visible_suffix: usize) -> Self {
        Self::keep_with(KeepConfig::last(visible_suffix))
    }

    /// Masks segments using the provided configuration.
    #[must_use]
    pub fn mask_with(config: MaskConfig) -> Self {
        Self::Mask(config)
    }

    /// Masks the first `mask_prefix` scalar values.
    #[must_use]
    pub fn mask_first(mask_prefix: usize) -> Self {
        Self::mask_with(MaskConfig::first(mask_prefix))
    }

    /// Masks the last `mask_suffix` scalar values.
    #[must_use]
    pub fn mask_last(mask_suffix: usize) -> Self {
        Self::mask_with(MaskConfig::last(mask_suffix))
    }

    /// Email-specific policy: keeps first `visible_prefix` chars of local part, preserves domain.
    ///
    /// # Example
    /// ```
    /// use redactable::TextRedactionPolicy;
    ///
    /// let policy = TextRedactionPolicy::email_local(2);
    /// assert_eq!(policy.apply_to("alice@example.com"), "al***@example.com");
    /// assert_eq!(policy.apply_to("bob@company.io"), "bo*@company.io");
    /// ```
    #[must_use]
    pub fn email_local(visible_prefix: usize) -> Self {
        Self::Email(EmailConfig::new(visible_prefix))
    }

    /// Overrides the masking character used by keep/mask/email policies.
    ///
    /// This method has no effect on [`TextRedactionPolicy::Full`] because full
    /// redaction replaces the entire value with a placeholder string rather
    /// than masking individual characters.
    #[must_use]
    pub fn with_mask_char(mut self, mask_char: char) -> Self {
        match &mut self {
            TextRedactionPolicy::Full { .. } => {}
            TextRedactionPolicy::Keep(config) => {
                config.set_mask_char(mask_char);
            }
            TextRedactionPolicy::Mask(config) => {
                config.set_mask_char(mask_char);
            }
            TextRedactionPolicy::Email(config) => {
                config.set_mask_char(mask_char);
            }
        }
        self
    }

    /// Applies the policy to `value`.
    ///
    /// This method is total (it does not return errors).
    #[must_use]
    pub fn apply_to(&self, value: &str) -> String {
        match self {
            TextRedactionPolicy::Full { placeholder } => placeholder.clone().into_owned(),
            TextRedactionPolicy::Keep(config) => config.apply_to(value),
            TextRedactionPolicy::Mask(config) => config.apply_to(value),
            TextRedactionPolicy::Email(config) => config.apply_to(value),
        }
    }
}

impl std::default::Default for TextRedactionPolicy {
    fn default() -> Self {
        Self::default_full()
    }
}

#[cfg(test)]
mod tests {
    use super::{KeepConfig, MaskConfig, REDACTED_PLACEHOLDER, TextRedactionPolicy};

    #[test]
    fn keep_policy_allows_full_visibility() {
        let policy = TextRedactionPolicy::keep_with(KeepConfig::first(3));
        assert_eq!(policy.apply_to("ab"), "ab");
    }

    #[test]
    fn keep_policy_respects_mask_char() {
        let policy = TextRedactionPolicy::keep_first(2).with_mask_char('#');
        assert_eq!(policy.apply_to("abcdef"), "ab####");
    }

    #[test]
    fn full_policy_uses_default_placeholder() {
        let policy = TextRedactionPolicy::default_full();
        assert_eq!(policy.apply_to("secret"), REDACTED_PLACEHOLDER);
    }

    #[test]
    fn full_policy_uses_custom_placeholder() {
        let policy = TextRedactionPolicy::full_with("<redacted>");
        assert_eq!(policy.apply_to("secret"), "<redacted>");
    }

    #[test]
    fn mask_policy_masks_first_and_last_segments() {
        let policy = TextRedactionPolicy::mask_first(2);
        assert_eq!(policy.apply_to("abcdef"), "**cdef");

        let policy = TextRedactionPolicy::mask_last(3);
        assert_eq!(policy.apply_to("abcdef"), "abc***");
    }

    #[test]
    fn mask_policy_respects_custom_mask_char() {
        let policy = TextRedactionPolicy::mask_with(MaskConfig::last(2)).with_mask_char('#');
        assert_eq!(policy.apply_to("abcd"), "ab##");
    }

    #[test]
    fn email_policy_preserves_domain() {
        let policy = TextRedactionPolicy::email_local(2);
        assert_eq!(policy.apply_to("alice@example.com"), "al***@example.com");
        assert_eq!(policy.apply_to("bob@company.io"), "bo*@company.io");
        assert_eq!(policy.apply_to("x@a.com"), "x@a.com"); // single char local, prefix=2 keeps all
    }

    #[test]
    fn email_policy_masks_non_email_inputs() {
        let policy = TextRedactionPolicy::email_local(2);

        // No @ symbol - mask like prefix keep
        assert_eq!(policy.apply_to("noatsymbol"), "no********");

        // Empty string
        assert_eq!(policy.apply_to(""), REDACTED_PLACEHOLDER);

        // Short local part
        assert_eq!(policy.apply_to("ab@x.com"), "ab@x.com"); // exactly 2 chars, no masking

        // Very short email
        assert_eq!(policy.apply_to("a@b.c"), "a@b.c"); // 1 char local, prefix=2 keeps all
    }

    #[test]
    fn email_policy_respects_mask_char() {
        let policy = TextRedactionPolicy::email_local(2).with_mask_char('#');
        assert_eq!(policy.apply_to("alice@example.com"), "al###@example.com");
    }

    #[test]
    fn empty_string_returns_placeholder_for_policies() {
        // Empty strings are fully redacted for keep/mask/email policies.
        let keep_policy = TextRedactionPolicy::keep_first(4);
        assert_eq!(keep_policy.apply_to(""), REDACTED_PLACEHOLDER);

        let mask_policy = TextRedactionPolicy::mask_first(4);
        assert_eq!(mask_policy.apply_to(""), REDACTED_PLACEHOLDER);

        let email_policy = TextRedactionPolicy::email_local(2);
        assert_eq!(email_policy.apply_to(""), REDACTED_PLACEHOLDER);

        let full_policy = TextRedactionPolicy::default_full();
        assert_eq!(full_policy.apply_to(""), REDACTED_PLACEHOLDER);
    }

    #[test]
    fn keep_both_overlap_keeps_entire_value() {
        // When prefix + suffix >= total, keep everything visible
        let policy = TextRedactionPolicy::keep_with(KeepConfig::both(2, 2));
        assert_eq!(policy.apply_to("abc"), "abc"); // 2 + 2 = 4 >= 3

        let policy = TextRedactionPolicy::keep_with(KeepConfig::both(3, 3));
        assert_eq!(policy.apply_to("abcd"), "abcd"); // 3 + 3 = 6 >= 4

        // Edge case: exactly equals total
        let policy = TextRedactionPolicy::keep_with(KeepConfig::both(2, 2));
        assert_eq!(policy.apply_to("abcd"), "abcd"); // 2 + 2 = 4 >= 4

        // Overflow-safe: large values still keep entire value
        let policy = TextRedactionPolicy::keep_with(KeepConfig::both(usize::MAX, usize::MAX));
        assert_eq!(policy.apply_to("abcd"), "abcd");
    }

    #[test]
    fn mask_both_overlap_masks_entire_value() {
        // When prefix + suffix >= total, mask everything
        let policy = TextRedactionPolicy::mask_with(MaskConfig::both(2, 2));
        assert_eq!(policy.apply_to("abc"), "***"); // 2 + 2 = 4 >= 3

        let policy = TextRedactionPolicy::mask_with(MaskConfig::both(3, 3));
        assert_eq!(policy.apply_to("abcd"), "****"); // 3 + 3 = 6 >= 4

        // Edge case: exactly equals total
        let policy = TextRedactionPolicy::mask_with(MaskConfig::both(2, 2));
        assert_eq!(policy.apply_to("abcd"), "****"); // 2 + 2 = 4 >= 4

        // Overflow-safe: large values still mask entire value
        let policy = TextRedactionPolicy::mask_with(MaskConfig::both(usize::MAX, usize::MAX));
        assert_eq!(policy.apply_to("abcd"), "****");
    }

    #[test]
    fn keep_both_no_overlap() {
        // Normal case: prefix + suffix < total
        let policy = TextRedactionPolicy::keep_with(KeepConfig::both(2, 2));
        assert_eq!(policy.apply_to("abcdef"), "ab**ef"); // keep first 2 and last 2
    }

    #[test]
    fn mask_both_no_overlap() {
        // Normal case: prefix + suffix < total
        let policy = TextRedactionPolicy::mask_with(MaskConfig::both(2, 2));
        assert_eq!(policy.apply_to("abcdef"), "**cd**"); // mask first 2 and last 2
    }
}