use crate::{Error, ErrorKind, Result};
use std::{fmt, sync::Arc};
use zeroize::Zeroizing;
#[derive(Clone)]
pub struct Secret(Arc<Zeroizing<String>>);
impl Secret {
pub fn new(value: impl Into<String>) -> Result<Self> {
let value = Zeroizing::new(value.into());
if value.is_empty() || value.len() > 16 * 1024 {
return Err(Error::new(ErrorKind::Configuration));
}
Ok(Self(Arc::new(value)))
}
pub fn expose(&self) -> &str {
self.0.as_str()
}
}
impl fmt::Debug for Secret {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Secret([REDACTED])")
}
}
impl fmt::Display for Secret {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("[REDACTED]")
}
}