use std::fmt;
#[derive(Clone, PartialEq, Eq)]
pub struct Secret(String);
impl Secret {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
pub fn expose(&self) -> &str {
&self.0
}
pub fn into_inner(self) -> String {
self.0
}
}
impl fmt::Debug for Secret {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Secret(***)")
}
}
impl fmt::Display for Secret {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("***")
}
}
impl From<&str> for Secret {
fn from(value: &str) -> Self {
Self::new(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn debug_output_never_contains_the_value() {
let secret = Secret::new("ghp_supersecret");
assert_eq!(format!("{secret:?}"), "Secret(***)");
assert_eq!(format!("{secret}"), "***");
assert_eq!(secret.expose(), "ghp_supersecret");
}
}