Skip to main content

lisette_diagnostics/
sink.rs

1use std::cell::RefCell;
2
3use syntax::ParseError;
4
5use crate::LisetteDiagnostic;
6
7#[derive(Debug, Default)]
8pub struct LocalSink {
9    diagnostics: RefCell<Vec<LisetteDiagnostic>>,
10}
11
12impl LocalSink {
13    pub fn new() -> Self {
14        Self::default()
15    }
16
17    pub fn push(&self, diagnostic: LisetteDiagnostic) {
18        self.diagnostics.borrow_mut().push(diagnostic);
19    }
20
21    pub fn has_errors(&self) -> bool {
22        self.diagnostics.borrow().iter().any(|d| d.is_error())
23    }
24
25    pub fn len(&self) -> usize {
26        self.diagnostics.borrow().len()
27    }
28
29    pub fn is_empty(&self) -> bool {
30        self.diagnostics.borrow().is_empty()
31    }
32
33    pub fn to_vec(&self) -> Vec<LisetteDiagnostic> {
34        self.diagnostics.borrow().clone()
35    }
36
37    pub fn take(&self) -> Vec<LisetteDiagnostic> {
38        self.diagnostics.take()
39    }
40
41    pub fn truncate(&self, len: usize) {
42        self.diagnostics.borrow_mut().truncate(len);
43    }
44
45    pub fn extend(&self, diagnostics: impl IntoIterator<Item = LisetteDiagnostic>) {
46        self.diagnostics.borrow_mut().extend(diagnostics);
47    }
48
49    pub fn extend_parse_errors(&self, errors: Vec<ParseError>) {
50        let diagnostics = errors.into_iter().map(LisetteDiagnostic::from);
51        self.diagnostics.borrow_mut().extend(diagnostics);
52    }
53
54    pub fn merge(sinks: Vec<LocalSink>) -> Vec<LisetteDiagnostic> {
55        let mut all: Vec<LisetteDiagnostic> = sinks.into_iter().flat_map(|s| s.take()).collect();
56        all.sort_by_key(|d| d.primary_offset());
57        all
58    }
59}