pub struct Secret(/* private fields */);Expand description
A configuration value that never appears in output.
Wraps a connection string, a password, a shared access key or an API
key. Both Debug and [Display] render <redacted>; the value comes
out only through Secret::expose, which is deliberately awkward to
type and easy to grep for.
§Why both Debug and Display
Deriving Debug on a configuration struct is the normal thing to do,
and the resulting line ends up in a log eventually. Masking Debug
alone is the common half-measure: it leaves format!("{secret}") and
every Display-based logging macro wide open. This type masks both.
§Examples
use env_reader::Secret;
#[derive(Debug)]
struct Config {
endpoint: String,
access_key: Secret,
}
let config = Config {
endpoint: "sb://example".into(),
access_key: Secret::new("hunter2"),
};
let printed = format!("{config:?}");
assert!(printed.contains("sb://example"));
assert!(!printed.contains("hunter2"));
assert_eq!(config.access_key.expose(), "hunter2");§PartialEq compares in ordinary time
It is derived, so a comparison is not constant-time. That is right for what this type is for — comparing two configurations, usually in a test — and wrong for checking a presented credential against a stored one. Do not use it for the latter.
Implementations§
Source§impl Secret
impl Secret
Sourcepub fn new(value: impl Into<String>) -> Self
pub fn new(value: impl Into<String>) -> Self
Wrap a value.
§Examples
use env_reader::Secret;
assert_eq!(Secret::new("hunter2").to_string(), "<redacted>");Sourcepub fn expose(&self) -> &str
pub fn expose(&self) -> &str
The value itself.
Named to be conspicuous at a call site and in a code review: every use of it is a place a secret leaves this type.
§Examples
use env_reader::Secret;
assert_eq!(Secret::new("hunter2").expose(), "hunter2");Sourcepub fn into_exposed(self) -> String
pub fn into_exposed(self) -> String
The value itself, consuming this.
For handing ownership to a client library that wants a String.