runic_diagnostics/collection/
collector.rs1use super::*;
2
3#[derive(Debug)]
5pub struct DiagnosticCollector {
6 diagnostics: Vec<DiagnosticEntry>,
7 counts: SeverityCounts,
8 limits: DiagnosticLimits,
9 accepted_user_diagnostics: usize,
10 accepted_user_errors: usize,
11 next_instance_id: u64,
12 next_insertion_order: u64,
13 has_limit_sentinel: bool,
14 closed: bool,
15}
16
17impl DiagnosticCollector {
18 pub const fn new() -> Self {
19 Self::with_limits(DiagnosticLimits::UNLIMITED)
20 }
21
22 pub const fn with_limits(limits: DiagnosticLimits) -> Self {
23 Self {
24 diagnostics: Vec::new(),
25 counts: SeverityCounts::new(),
26 limits,
27 accepted_user_diagnostics: 0,
28 accepted_user_errors: 0,
29 next_instance_id: 0,
30 next_insertion_order: 0,
31 has_limit_sentinel: false,
32 closed: false,
33 }
34 }
35
36 pub fn add<T>(&mut self, diagnostic: T) -> PushResult
38 where
39 T: Diagnostic + Send + Sync + 'static,
40 {
41 self.add_owned(OwnedDiagnostic::new(diagnostic))
42 }
43
44 pub fn add_owned(&mut self, diagnostic: OwnedDiagnostic) -> PushResult {
46 let severity = diagnostic.severity();
47 self.push_with_severity(diagnostic, severity)
48 }
49
50 pub fn add_report(&mut self, report: Report) -> PushResult {
52 self.push_with_severity(report.into_diagnostic(), Severity::Error)
53 }
54
55 pub fn capture<T>(&mut self, result: crate::Result<T>) -> Option<T> {
60 match result {
61 Ok(value) => Some(value),
62 Err(report) => {
63 let _ = self.add_report(report);
64 None
65 }
66 }
67 }
68
69 pub fn merge(&mut self, diagnostics: DiagnosticSet) -> MergeSummary {
72 let mut summary = MergeSummary::default();
73 for entry in diagnostics {
74 match self.merge_entry(entry) {
75 MergeDisposition::Accepted => {
76 summary.accepted = summary.accepted.saturating_add(1);
77 }
78 MergeDisposition::DroppedByLimit => {
79 summary.dropped_by_limit = summary.dropped_by_limit.saturating_add(1);
80 }
81 MergeDisposition::ClosedAfterLimit => {
82 summary.closed_after_limit = summary.closed_after_limit.saturating_add(1);
83 }
84 MergeDisposition::CoalescedLimitSentinel => {
85 summary.coalesced_limit_sentinels =
86 summary.coalesced_limit_sentinels.saturating_add(1);
87 }
88 }
89 }
90 summary
91 }
92
93 pub fn counts(&self) -> SeverityCounts {
94 self.counts
95 }
96
97 pub fn len(&self) -> usize {
98 self.diagnostics.len()
99 }
100
101 pub fn is_empty(&self) -> bool {
102 self.diagnostics.is_empty()
103 }
104
105 pub const fn is_closed(&self) -> bool {
106 self.closed
107 }
108
109 pub fn finish(self) -> DiagnosticSet {
110 DiagnosticSet::from_parts(self.diagnostics.into_boxed_slice(), self.counts)
111 }
112
113 fn push_with_severity(
114 &mut self,
115 diagnostic: OwnedDiagnostic,
116 effective_severity: Severity,
117 ) -> PushResult {
118 if let Some(result) = self.check_user_capacity(effective_severity) {
119 return result;
120 }
121
122 let metadata = DiagnosticMetadataSnapshot::capture(&diagnostic);
123 self.accept_user(diagnostic, effective_severity, metadata)
124 }
125
126 fn merge_entry(&mut self, entry: DiagnosticEntry) -> MergeDisposition {
127 if self.closed {
128 return MergeDisposition::ClosedAfterLimit;
129 }
130 if entry.diagnostic.is::<LimitReachedDiagnostic>() {
131 if self.has_limit_sentinel {
132 return MergeDisposition::CoalescedLimitSentinel;
133 }
134 self.has_limit_sentinel = true;
135 let (diagnostic, effective_severity, metadata) = entry.into_parts();
136 self.push_unchecked(diagnostic, effective_severity, metadata);
137 return MergeDisposition::Accepted;
138 }
139
140 let (diagnostic, effective_severity, metadata) = entry.into_parts();
141 match self.push_with_snapshot(diagnostic, effective_severity, metadata) {
142 PushResult::Accepted => MergeDisposition::Accepted,
143 PushResult::DroppedByLimit => MergeDisposition::DroppedByLimit,
144 PushResult::ClosedAfterLimit => MergeDisposition::ClosedAfterLimit,
145 }
146 }
147
148 fn push_with_snapshot(
149 &mut self,
150 diagnostic: OwnedDiagnostic,
151 effective_severity: Severity,
152 metadata: DiagnosticMetadataSnapshot,
153 ) -> PushResult {
154 if let Some(result) = self.check_user_capacity(effective_severity) {
155 return result;
156 }
157
158 self.accept_user(diagnostic, effective_severity, metadata)
159 }
160
161 fn check_user_capacity(&mut self, effective_severity: Severity) -> Option<PushResult> {
162 if self.closed {
163 return Some(PushResult::ClosedAfterLimit);
164 }
165 if self.would_exceed_limit(effective_severity) {
166 self.closed = true;
167 self.append_limit_sentinel();
168 return Some(PushResult::DroppedByLimit);
169 }
170 None
171 }
172
173 fn accept_user(
174 &mut self,
175 diagnostic: OwnedDiagnostic,
176 effective_severity: Severity,
177 metadata: DiagnosticMetadataSnapshot,
178 ) -> PushResult {
179 self.push_unchecked(diagnostic, effective_severity, metadata);
180 self.accepted_user_diagnostics = self.accepted_user_diagnostics.saturating_add(1);
181 if effective_severity == Severity::Error {
182 self.accepted_user_errors = self.accepted_user_errors.saturating_add(1);
183 }
184 PushResult::Accepted
185 }
186
187 fn would_exceed_limit(&self, severity: Severity) -> bool {
188 self.limits
189 .max_diagnostics
190 .is_some_and(|limit| self.accepted_user_diagnostics >= limit)
191 || (severity == Severity::Error
192 && self.limits.max_errors.is_some_and(|limit| self.accepted_user_errors >= limit))
193 }
194
195 fn append_limit_sentinel(&mut self) {
196 if self.has_limit_sentinel {
197 return;
198 }
199 let diagnostic = OwnedDiagnostic::new(LimitReachedDiagnostic);
200 let metadata = DiagnosticMetadataSnapshot::capture(&diagnostic);
201 self.has_limit_sentinel = true;
202 self.push_unchecked(diagnostic, Severity::Error, metadata);
203 }
204
205 fn push_unchecked(
206 &mut self,
207 diagnostic: OwnedDiagnostic,
208 effective_severity: Severity,
209 metadata: DiagnosticMetadataSnapshot,
210 ) {
211 let id = DiagnosticInstanceId::new(self.next_instance_id);
212 let insertion_order = self.next_insertion_order;
213 self.next_instance_id = self
214 .next_instance_id
215 .checked_add(1)
216 .expect("an allocated diagnostic entry cannot exhaust u64 IDs");
217 self.next_insertion_order = self
218 .next_insertion_order
219 .checked_add(1)
220 .expect("an allocated diagnostic entry cannot exhaust u64 order values");
221 self.counts.increment(effective_severity);
222 self.diagnostics.push(DiagnosticEntry {
223 id,
224 insertion_order,
225 effective_severity,
226 diagnostic,
227 metadata,
228 });
229 }
230}
231
232enum MergeDisposition {
233 Accepted,
234 DroppedByLimit,
235 ClosedAfterLimit,
236 CoalescedLimitSentinel,
237}
238
239impl Default for DiagnosticCollector {
240 fn default() -> Self {
241 Self::new()
242 }
243}
244
245impl DiagnosticSink for DiagnosticCollector {
246 fn push(&mut self, diagnostic: OwnedDiagnostic) -> PushResult {
247 self.add_owned(diagnostic)
248 }
249}
250
251#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
253pub struct MergeSummary {
254 pub accepted: usize,
257 pub dropped_by_limit: usize,
259 pub closed_after_limit: usize,
261 pub coalesced_limit_sentinels: usize,
263}