use std::fmt;
pub struct Redacted<T>(T);
impl<T> Redacted<T> {
#[inline]
pub fn new(value: T) -> Self {
Self(value)
}
#[inline]
pub fn expose(&self) -> &T {
&self.0
}
#[inline]
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> fmt::Debug for Redacted<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<redacted>")
}
}
impl<T> fmt::Display for Redacted<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<redacted>")
}
}
impl<T: Clone> Clone for Redacted<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T: PartialEq> PartialEq for Redacted<T> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<T: Eq> Eq for Redacted<T> {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn debug_and_display_are_redacted() {
let r = Redacted::new("secret".to_string());
assert_eq!(format!("{r:?}"), "<redacted>");
assert_eq!(format!("{r}"), "<redacted>");
}
#[test]
fn expose_returns_inner() {
let r = Redacted::new(42u32);
assert_eq!(*r.expose(), 42);
}
#[test]
fn clone_eq_and_into_inner() {
let a = Redacted::new("secret".to_string());
let b = a.clone();
assert_eq!(a, b);
assert_eq!(a.into_inner(), "secret");
}
}