cmark_writer/writer/runtime/diagnostics/
mod.rs1use std::cell::RefCell;
8use std::rc::Rc;
9
10use ecow::EcoString;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum DiagnosticSeverity {
15 Warning,
17 Info,
19}
20
21#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct Diagnostic {
24 pub severity: DiagnosticSeverity,
26 pub message: EcoString,
28}
29
30impl Diagnostic {
31 pub fn warning<S: Into<EcoString>>(message: S) -> Self {
33 Self {
34 severity: DiagnosticSeverity::Warning,
35 message: message.into(),
36 }
37 }
38
39 pub fn info<S: Into<EcoString>>(message: S) -> Self {
41 Self {
42 severity: DiagnosticSeverity::Info,
43 message: message.into(),
44 }
45 }
46}
47
48pub trait DiagnosticSink {
50 fn emit(&mut self, diagnostic: Diagnostic);
52}
53
54#[derive(Debug, Default, Clone, Copy)]
56pub struct NullSink;
57
58impl DiagnosticSink for NullSink {
59 fn emit(&mut self, _: Diagnostic) {}
60}
61
62#[derive(Debug, Clone)]
64pub struct SharedVecSink {
65 target: Rc<RefCell<Vec<Diagnostic>>>,
66}
67
68impl SharedVecSink {
69 pub fn new(target: Rc<RefCell<Vec<Diagnostic>>>) -> Self {
71 Self { target }
72 }
73
74 pub fn target(&self) -> Rc<RefCell<Vec<Diagnostic>>> {
76 Rc::clone(&self.target)
77 }
78}
79
80impl DiagnosticSink for SharedVecSink {
81 fn emit(&mut self, diagnostic: Diagnostic) {
82 self.target.borrow_mut().push(diagnostic);
83 }
84}