use std::sync::Mutex;
#[derive(Debug, Clone)]
pub struct Warning {
pub category: String,
pub message: String,
}
static WARNINGS: Mutex<Vec<Warning>> = Mutex::new(Vec::new());
pub fn warn(category: &str, message: &str) {
let w = Warning {
category: category.to_string(),
message: message.to_string(),
};
eprintln!(
"\x1b[33m[warn:{}]\x1b[0m {}",
w.category, w.message
);
if let Ok(mut warnings) = WARNINGS.lock() {
warnings.push(w);
}
}
pub fn take_warnings() -> Vec<Warning> {
if let Ok(mut warnings) = WARNINGS.lock() {
std::mem::take(&mut *warnings)
} else {
Vec::new()
}
}
pub fn warning_count() -> usize {
if let Ok(warnings) = WARNINGS.lock() {
warnings.len()
} else {
0
}
}
#[cfg(test)]
pub fn clear() {
if let Ok(mut warnings) = WARNINGS.lock() {
warnings.clear();
}
}
#[macro_export]
macro_rules! scan_warn {
($cat:expr, $($arg:tt)*) => {
$crate::diagnostics::warn($cat, &format!($($arg)*))
};
}