Skip to main content

vault_client_rs/types/
redaction.rs

1use std::sync::atomic::{AtomicU8, Ordering};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4#[repr(u8)]
5pub enum RedactionLevel {
6    Full = 0,    // "[REDACTED]" (default, current behavior)
7    Partial = 1, // first 4 chars + "..."
8    None = 2,    // show the value (for local debugging only)
9}
10
11static LEVEL: AtomicU8 = AtomicU8::new(0);
12
13pub fn set_redaction_level(level: RedactionLevel) {
14    LEVEL.store(level as u8, Ordering::Relaxed);
15}
16
17pub fn redaction_level() -> RedactionLevel {
18    match LEVEL.load(Ordering::Relaxed) {
19        1 => RedactionLevel::Partial,
20        2 => RedactionLevel::None,
21        _ => RedactionLevel::Full,
22    }
23}
24
25pub fn redact(value: &str) -> String {
26    match redaction_level() {
27        RedactionLevel::Full => "[REDACTED]".into(),
28        RedactionLevel::Partial => {
29            let mut chars = value.chars();
30            let prefix: String = chars.by_ref().take(4).collect();
31            if prefix.chars().count() < 4 || chars.next().is_none() {
32                "[REDACTED]".into()
33            } else {
34                format!("{prefix}...")
35            }
36        }
37        RedactionLevel::None => value.into(),
38    }
39}