use std::cell::RefCell;
use std::rc::Rc;
use ecow::EcoString;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagnosticSeverity {
Warning,
Info,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Diagnostic {
pub severity: DiagnosticSeverity,
pub message: EcoString,
}
impl Diagnostic {
pub fn warning<S: Into<EcoString>>(message: S) -> Self {
Self {
severity: DiagnosticSeverity::Warning,
message: message.into(),
}
}
pub fn info<S: Into<EcoString>>(message: S) -> Self {
Self {
severity: DiagnosticSeverity::Info,
message: message.into(),
}
}
}
pub trait DiagnosticSink {
fn emit(&mut self, diagnostic: Diagnostic);
}
#[derive(Debug, Default, Clone, Copy)]
pub struct NullSink;
impl DiagnosticSink for NullSink {
fn emit(&mut self, _: Diagnostic) {}
}
#[derive(Debug, Clone)]
pub struct SharedVecSink {
target: Rc<RefCell<Vec<Diagnostic>>>,
}
impl SharedVecSink {
pub fn new(target: Rc<RefCell<Vec<Diagnostic>>>) -> Self {
Self { target }
}
pub fn target(&self) -> Rc<RefCell<Vec<Diagnostic>>> {
Rc::clone(&self.target)
}
}
impl DiagnosticSink for SharedVecSink {
fn emit(&mut self, diagnostic: Diagnostic) {
self.target.borrow_mut().push(diagnostic);
}
}