runic_diagnostics/collection/
diagnostic_set.rs1use super::*;
2use crate::owned::DiagnosticBundle;
3
4#[derive(Debug)]
6pub struct DiagnosticSet {
7 diagnostics: Box<[DiagnosticEntry]>,
8 counts: SeverityCounts,
9}
10
11impl Default for DiagnosticSet {
12 fn default() -> Self {
13 Self {
14 diagnostics: Box::default(),
15 counts: SeverityCounts::new(),
16 }
17 }
18}
19
20impl DiagnosticSet {
21 pub(super) const fn from_parts(
22 diagnostics: Box<[DiagnosticEntry]>,
23 counts: SeverityCounts,
24 ) -> Self {
25 Self {
26 diagnostics,
27 counts,
28 }
29 }
30
31 pub fn len(&self) -> usize {
33 self.diagnostics.len()
34 }
35
36 pub fn is_empty(&self) -> bool {
38 self.diagnostics.is_empty()
39 }
40
41 pub const fn counts(&self) -> SeverityCounts {
43 self.counts
44 }
45
46 pub const fn has_errors(&self) -> bool {
48 self.counts.errors() != 0
49 }
50
51 pub fn iter(&self) -> slice::Iter<'_, DiagnosticEntry> {
53 self.diagnostics.iter()
54 }
55
56 #[inline]
58 pub fn into_vec(self) -> Vec<DiagnosticEntry> {
59 self.diagnostics.into_vec()
60 }
61
62 pub fn attach_session_sources<P>(mut self, sources: Arc<P>) -> Self
67 where
68 P: SourceProvider + Send + Sync + 'static + ?Sized,
69 {
70 for entry in &mut self.diagnostics {
71 entry.diagnostic.set_session_sources(sources.clone());
72 }
73 self
74 }
75
76 pub fn assess<P>(&self, policy: &P) -> bool
79 where
80 P: FailurePolicy + ?Sized,
81 {
82 self.diagnostics.iter().any(|entry| policy.is_failure(entry.metadata()))
83 }
84
85 #[doc(hidden)]
86 pub fn into_report<P>(self, policy: &P) -> Report
87 where
88 P: FailurePolicy + ?Sized,
89 {
90 let mut entries = self.into_vec();
91 let primary_index = entries
92 .iter()
93 .position(|entry| policy.is_failure(entry.metadata()))
94 .or((!entries.is_empty()).then_some(0));
95 let Some(primary_index) = primary_index else {
96 return Report::msg("operation failed without producing a diagnostic");
97 };
98 let primary = entries.remove(primary_index).diagnostic;
99 if entries.is_empty() {
100 return Report::from_diagnostic(primary);
101 }
102
103 let related = entries.into_iter().map(|entry| entry.diagnostic).collect();
104 DiagnosticBundle::new(primary, related).into_report()
105 }
106}
107
108impl IntoIterator for DiagnosticSet {
109 type IntoIter = vec::IntoIter<DiagnosticEntry>;
110 type Item = DiagnosticEntry;
111
112 fn into_iter(self) -> Self::IntoIter {
113 self.diagnostics.into_vec().into_iter()
114 }
115}
116
117impl<'a> IntoIterator for &'a DiagnosticSet {
118 type IntoIter = slice::Iter<'a, DiagnosticEntry>;
119 type Item = &'a DiagnosticEntry;
120
121 fn into_iter(self) -> Self::IntoIter {
122 self.iter()
123 }
124}
125
126#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
128pub struct DiagnosticInstanceId(u64);
129
130impl DiagnosticInstanceId {
131 pub const fn get(self) -> u64 {
132 self.0
133 }
134
135 pub(super) const fn new(id: u64) -> Self {
136 Self(id)
137 }
138}
139
140#[derive(Debug)]
142pub struct DiagnosticEntry {
143 pub id: DiagnosticInstanceId,
144 pub insertion_order: u64,
145 pub effective_severity: Severity,
146 pub diagnostic: OwnedDiagnostic,
147 pub(super) metadata: DiagnosticMetadataSnapshot,
148}
149
150impl DiagnosticEntry {
151 pub fn metadata(&self) -> DiagnosticMetadata<'_> {
154 self.metadata.as_metadata(self.effective_severity)
155 }
156
157 #[inline]
158 pub(super) fn into_parts(self) -> (OwnedDiagnostic, Severity, DiagnosticMetadataSnapshot) {
159 (self.diagnostic, self.effective_severity, self.metadata)
160 }
161}
162
163#[derive(Debug)]
164pub(super) struct DiagnosticMetadataSnapshot {
165 descriptor: Option<&'static DiagnosticDescriptor>,
166 code: Option<StoredDiagnosticCode>,
167 tags: Box<[DiagnosticTag]>,
168}
169
170impl DiagnosticMetadataSnapshot {
171 pub fn capture(diagnostic: &OwnedDiagnostic) -> Self {
172 Self {
173 descriptor: diagnostic.descriptor(),
174 code: diagnostic.code().map(StoredDiagnosticCode::from),
175 tags: Vec::from(diagnostic.tags()).into_boxed_slice(),
176 }
177 }
178
179 pub fn as_metadata(&self, severity: Severity) -> DiagnosticMetadata<'_> {
180 DiagnosticMetadata {
181 descriptor: self.descriptor,
182 code: self.code.as_ref().map(StoredDiagnosticCode::as_ref),
183 severity,
184 tags: &self.tags,
185 }
186 }
187}
188
189#[derive(Debug)]
190struct StoredDiagnosticCode {
191 namespace: String,
192 code: String,
193}
194
195impl StoredDiagnosticCode {
196 fn as_ref(&self) -> DiagnosticCodeRef<'_> {
197 DiagnosticCodeRef {
198 namespace: &self.namespace,
199 code: &self.code,
200 }
201 }
202}
203
204impl From<DiagnosticCodeRef<'_>> for StoredDiagnosticCode {
205 fn from(code: DiagnosticCodeRef<'_>) -> Self {
206 Self {
207 namespace: String::from(code.namespace),
208 code: String::from(code.code),
209 }
210 }
211}
212
213#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
215pub struct SeverityCounts {
216 pub(super) errors: usize,
217 pub(super) warnings: usize,
218 pub(super) infos: usize,
219 pub(super) hints: usize,
220}
221
222impl SeverityCounts {
223 pub const fn new() -> Self {
224 Self {
225 errors: 0,
226 warnings: 0,
227 infos: 0,
228 hints: 0,
229 }
230 }
231
232 pub const fn errors(self) -> usize {
233 self.errors
234 }
235
236 pub const fn warnings(self) -> usize {
237 self.warnings
238 }
239
240 pub const fn infos(self) -> usize {
241 self.infos
242 }
243
244 pub const fn hints(self) -> usize {
245 self.hints
246 }
247
248 pub const fn total(self) -> usize {
249 self.errors + self.warnings + self.infos + self.hints
250 }
251
252 pub const fn get(self, severity: Severity) -> usize {
253 match severity {
254 Severity::Error => self.errors,
255 Severity::Warning => self.warnings,
256 Severity::Info => self.infos,
257 Severity::Hint => self.hints,
258 }
259 }
260
261 pub(super) fn increment(&mut self, severity: Severity) {
262 let count = match severity {
263 Severity::Error => &mut self.errors,
264 Severity::Warning => &mut self.warnings,
265 Severity::Info => &mut self.infos,
266 Severity::Hint => &mut self.hints,
267 };
268 *count = count.saturating_add(1);
269 }
270}