#![cfg_attr(docsrs, doc(cfg(feature = "toggle")))]
use once_cell::sync::OnceCell;
#[derive(Debug, Copy, Clone)]
#[cfg_attr(docsrs, doc(cfg(feature = "toggle")))]
pub enum RedactionBehavior {
Redact,
Plaintext,
}
impl RedactionBehavior {
pub fn is_redact(&self) -> bool {
matches!(self, &RedactionBehavior::Redact)
}
pub fn is_plaintext(&self) -> bool {
matches!(self, &RedactionBehavior::Plaintext)
}
}
static DEBUG_FORMAT: OnceCell<RedactionBehavior> = OnceCell::new();
#[cfg_attr(docsrs, doc(cfg(feature = "toggle")))]
pub fn disable() -> Result<(), RedactionBehavior> {
DEBUG_FORMAT.set(RedactionBehavior::Plaintext)
}
pub(crate) fn get_redaction_behavior() -> RedactionBehavior {
if let "true" | "1" | "on" = std::env::var("VEIL_DISABLE_REDACTION")
.unwrap_or_default()
.to_ascii_lowercase()
.as_str()
{
*DEBUG_FORMAT.get_or_init(|| RedactionBehavior::Plaintext)
} else {
*DEBUG_FORMAT.get_or_init(|| RedactionBehavior::Redact)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn redaction_cant_be_set_after_reading() {
assert!(get_redaction_behavior().is_redact());
disable().unwrap_err();
assert!(get_redaction_behavior().is_redact());
}
}