use std::sync::atomic::{AtomicBool, Ordering};
static CONFIG_DIR_IN_USE: AtomicBool = AtomicBool::new(true);
pub const NOT_PERSISTED_NOTE: &str = " (not saved: --no-config)";
pub fn disable_config_dir() {
CONFIG_DIR_IN_USE.store(false, Ordering::Relaxed);
}
pub fn config_writes_allowed() -> bool {
CONFIG_DIR_IN_USE.load(Ordering::Relaxed)
}
pub fn config_reads_allowed() -> bool {
CONFIG_DIR_IN_USE.load(Ordering::Relaxed)
}
pub fn unpersisted_suffix() -> &'static str {
if config_writes_allowed() {
""
} else {
NOT_PERSISTED_NOTE
}
}
#[cfg(test)]
pub(crate) struct SuppressGuard;
#[cfg(test)]
impl SuppressGuard {
pub(crate) fn new() -> Self {
CONFIG_DIR_IN_USE.store(false, Ordering::Relaxed);
Self
}
}
#[cfg(test)]
impl Drop for SuppressGuard {
fn drop(&mut self) {
CONFIG_DIR_IN_USE.store(true, Ordering::Relaxed);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_and_writes_are_allowed_by_default() {
let _lock = crate::test_env::env_lock();
assert!(config_writes_allowed());
assert!(config_reads_allowed());
assert_eq!(unpersisted_suffix(), "");
}
#[test]
fn the_guard_suppresses_and_restores_both_halves() {
let _lock = crate::test_env::env_lock();
{
let _g = SuppressGuard::new();
assert!(!config_writes_allowed());
assert!(!config_reads_allowed());
assert_eq!(unpersisted_suffix(), NOT_PERSISTED_NOTE);
}
assert!(config_writes_allowed());
assert!(config_reads_allowed());
}
}