Skip to main content

mads_core/configuration/
secret.rs

1//! Explicit secret exposure and redacted ordinary formatting.
2
3use std::fmt;
4
5/// A value whose ordinary formatting is always redacted.
6///
7/// Access requires an explicit call to [`Self::expose`] or
8/// [`Self::into_exposed`]. This wrapper does not promise memory zeroization.
9#[derive(Clone, PartialEq, Eq)]
10pub struct Secret<T>(T);
11
12impl<T> Secret<T> {
13    /// Wraps a value without formatting it.
14    pub fn new(value: T) -> Self {
15        Self(value)
16    }
17
18    /// Explicitly borrows the secret value.
19    pub fn expose(&self) -> &T {
20        &self.0
21    }
22
23    /// Explicitly consumes the wrapper and returns the secret value.
24    pub fn into_exposed(self) -> T {
25        self.0
26    }
27}
28
29impl<T> fmt::Debug for Secret<T> {
30    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
31        formatter.write_str("[REDACTED]")
32    }
33}
34
35impl<T> fmt::Display for Secret<T> {
36    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
37        formatter.write_str("[REDACTED]")
38    }
39}