use std::collections::BTreeMap;
use std::sync::{Arc, Mutex};
use super::diagnostic::{Diagnostic, DiagnosticKey};
#[derive(Debug, Default, Clone)]
pub struct DiagnosticSink {
inner: Arc<Mutex<BTreeMap<DiagnosticKey, Diagnostic>>>,
}
impl DiagnosticSink {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn push(&self, mut diagnostic: Diagnostic) {
diagnostic.canonicalise();
if let Ok(mut guard) = self.inner.lock() {
guard.entry(diagnostic.key()).or_insert(diagnostic);
}
}
pub fn for_each<F: FnMut(&Diagnostic)>(&self, mut f: F) {
if let Ok(guard) = self.inner.lock() {
for diagnostic in guard.values() {
f(diagnostic);
}
}
}
#[must_use]
pub fn snapshot(&self) -> Vec<Diagnostic> {
self.inner
.lock()
.map(|g| g.values().cloned().collect())
.unwrap_or_default()
}
#[must_use]
pub fn len(&self) -> usize {
self.inner.lock().map(|g| g.len()).unwrap_or(0)
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}