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