pub const DO_NOT_TRACK_ENV: &str = "DO_NOT_TRACK";
pub const TELEMETRY_DISABLED_ENV: &str = "FALLOW_TELEMETRY_DISABLED";
#[must_use]
pub fn is_opt_out_value(value: &str) -> bool {
let value = value.trim().to_ascii_lowercase();
!matches!(value.as_str(), "" | "0" | "false" | "no" | "off")
}
#[must_use]
pub fn telemetry_opted_out() -> bool {
opted_out_with(|name| std::env::var(name).ok())
}
#[must_use]
pub fn opted_out_with(lookup: impl Fn(&str) -> Option<String>) -> bool {
[DO_NOT_TRACK_ENV, TELEMETRY_DISABLED_ENV]
.into_iter()
.any(|name| lookup(name).is_some_and(|value| is_opt_out_value(&value)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn any_set_value_except_an_off_value_opts_out() {
for value in [
"1", "true", "yes", "on", "enabled", "y", "TRUE", " 1 ", "anything",
] {
assert!(is_opt_out_value(value), "{value:?} must opt out");
}
}
#[test]
fn empty_and_off_values_keep_telemetry_alone() {
for value in ["", " ", "0", "false", "FALSE", "no", "off", " Off "] {
assert!(!is_opt_out_value(value), "{value:?} must not opt out");
}
}
#[test]
fn either_switch_opts_out() {
let dnt = |name: &str| (name == DO_NOT_TRACK_ENV).then(|| "enabled".to_owned());
let admin = |name: &str| (name == TELEMETRY_DISABLED_ENV).then(|| "y".to_owned());
let off = |_: &str| Some("0".to_owned());
assert!(opted_out_with(dnt));
assert!(opted_out_with(admin));
assert!(!opted_out_with(off));
assert!(!opted_out_with(|_| None));
}
}