use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use crate::error::printable;
const DEFAULT_QUOTA: u32 = 10;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum WarningKind {
#[default]
Warning,
Note,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Warning {
pub kind: WarningKind,
pub name: Option<&'static str>,
pub message: String,
pub file: Option<String>,
pub line: Option<u32>,
}
impl Warning {
#[must_use]
pub fn new(name: Option<&'static str>, message: impl Into<String>) -> Warning {
Warning {
kind: WarningKind::Warning,
name,
message: message.into(),
file: None,
line: None,
}
}
#[must_use]
pub fn note(message: impl Into<String>) -> Warning {
Warning {
kind: WarningKind::Note,
name: None,
message: message.into(),
file: None,
line: None,
}
}
#[must_use]
pub fn in_file(mut self, file: impl Into<String>) -> Warning {
self.file = Some(file.into());
self
}
#[must_use]
pub fn at_line(mut self, line: u32) -> Warning {
self.line = Some(line);
self
}
#[must_use]
pub fn location(&self) -> String {
match (self.file.as_deref().map(printable), self.line) {
(Some(file), Some(line)) => format!("In file {file}:{line}: "),
(Some(file), None) => format!("In file {file}: "),
(None, Some(line)) => format!("In line {line}: "),
(None, None) => String::new(),
}
}
}
impl fmt::Display for Warning {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.kind == WarningKind::Note {
return f.write_str(&self.message);
}
write!(f, "{}warning: {}", self.location(), self.message)?;
if let Some(name) = self.name {
write!(f, " [{name}]")?;
}
Ok(())
}
}
pub trait WarningSink {
fn warn(&mut self, warning: Warning);
fn is_suppressed(&self, _name: Option<&str>) -> bool {
false
}
}
#[derive(Debug, Clone)]
pub struct Warnings {
muted: BTreeSet<String>,
quota: u32,
raised: BTreeMap<String, u32>,
kept: Vec<Warning>,
}
impl Default for Warnings {
fn default() -> Self {
Warnings::new()
}
}
impl Warnings {
#[must_use]
pub fn new() -> Warnings {
Warnings {
muted: BTreeSet::new(),
quota: DEFAULT_QUOTA,
raised: BTreeMap::new(),
kept: Vec::new(),
}
}
pub fn mute(&mut self, name: impl Into<String>) {
self.muted.insert(name.into());
}
pub fn unlimited(&mut self) {
self.quota = u32::MAX;
}
pub fn record(&mut self, warning: Warning) -> Option<&Warning> {
if warning.kind == WarningKind::Note {
self.kept.push(warning);
return self.kept.last();
}
if let Some(name) = warning.name {
if self.muted.contains(name) {
return None;
}
let raised = self.raised.entry(name.to_string()).or_insert(0);
*raised = raised.saturating_add(1);
if *raised > self.quota {
return None;
}
}
self.kept.push(warning);
self.kept.last()
}
#[must_use]
pub fn warnings(&self) -> &[Warning] {
&self.kept
}
#[must_use]
pub fn into_warnings(self) -> Vec<Warning> {
self.kept
}
#[must_use]
pub fn suppressed(&self) -> Vec<(String, u32)> {
self.raised
.iter()
.filter(|(name, raised)| !self.muted.contains(*name) && **raised > self.quota)
.map(|(name, raised)| (name.clone(), *raised - self.quota))
.collect()
}
}
impl WarningSink for Warnings {
fn warn(&mut self, warning: Warning) {
self.record(warning);
}
fn is_suppressed(&self, name: Option<&str>) -> bool {
let Some(name) = name else { return false };
self.muted.contains(name) || self.raised.get(name).is_some_and(|r| *r >= self.quota)
}
}