use std::collections::HashSet;
use std::fmt;
use std::sync::atomic::AtomicBool;
use std::sync::{LazyLock, Mutex};
use anstream::eprintln;
use owo_colors::OwoColorize;
pub static ENABLED: AtomicBool = AtomicBool::new(false);
pub static WARNINGS: LazyLock<Mutex<HashSet<String>>> = LazyLock::new(Mutex::default);
pub fn enable() {
ENABLED.store(true, std::sync::atomic::Ordering::SeqCst);
}
pub fn disable() {
ENABLED.store(false, std::sync::atomic::Ordering::SeqCst);
}
pub fn is_enabled() -> bool {
ENABLED.load(std::sync::atomic::Ordering::SeqCst)
}
pub fn warn(message: fmt::Arguments<'_>) {
if is_enabled() {
emit(message.to_string());
}
}
pub fn warn_once(message: fmt::Arguments<'_>) {
if !is_enabled() {
return;
}
let message = message.to_string();
let should_emit = match WARNINGS.lock() {
Ok(mut states) => states.insert(message.clone()),
Err(_) => false,
};
if should_emit {
emit(message);
}
}
fn emit(message: String) {
crate::cli::reporter::suspend(move || {
eprintln!(
"{}{} {}",
"warning".yellow().bold(),
":".bold(),
message.bold()
);
});
}
#[macro_export]
macro_rules! warn_user {
($($arg:tt)*) => {
if $crate::warnings::is_enabled() {
$crate::warnings::warn(format_args!($($arg)*));
}
};
}
#[macro_export]
macro_rules! warn_user_once {
($($arg:tt)*) => {
if $crate::warnings::is_enabled() {
$crate::warnings::warn_once(format_args!($($arg)*));
}
};
}