use std::fmt;
use crate::inference::credentials::redact_secret;
#[derive(Clone, PartialEq, Eq)]
pub struct SecretString(String);
impl SecretString {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
pub fn expose(&self) -> &str {
&self.0
}
}
impl fmt::Debug for SecretString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "SecretString({})", redact_secret(&self.0))
}
}
impl fmt::Display for SecretString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&redact_secret(&self.0))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn secret_debug_is_redacted() {
let s = SecretString::new("sk-or-verysecretvalue1234"); let dumped = format!("{s:?}");
assert!(!dumped.contains("verysecretvalue"), "leaked: {dumped}");
assert!(dumped.contains("chars"), "not redacted shape: {dumped}");
}
#[test]
fn secret_display_is_redacted() {
let s = SecretString::new("sk-or-verysecretvalue1234"); let shown = format!("{s}");
assert!(!shown.contains("verysecretvalue"), "leaked: {shown}");
}
#[test]
fn secret_expose_returns_raw() {
let s = SecretString::new("raw-key"); assert_eq!(s.expose(), "raw-key");
}
}