mib-rs 0.10.0

SNMP MIB parser and resolver
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//! Diagnostic reporting and configuration.
//!
//! [`Diagnostic`] represents a single issue found during parsing or resolution.
//! [`DiagnosticConfig`] controls diagnostic collection, presentation, severity
//! overrides, and load failure thresholds, with preset configurations for
//! common use cases.

use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;

use crate::source::{
    BytePosition, Position, PositionEncoding, PositionError, SourceDocument, SourceId,
    SourceOrigin, SourceRange, SourceRangeError, SourceSet,
};

use super::{DiagCode, ReportingLevel, Severity};

/// An issue found during parsing or resolution.
///
/// Its [`severity`](Self::severity) is the effective severity after applying
/// [`DiagnosticConfig::overrides`]. Source locations remain checked,
/// source-qualified byte ranges; line and column values are derived by a
/// report-owned [`DiagnosticEntry`] handles when needed for presentation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Diagnostic {
    /// Effective severity after applying diagnostic configuration overrides.
    pub severity: Severity,
    /// Diagnostic code identifying the issue category.
    pub code: DiagCode,
    /// Human-readable description of the issue.
    pub message: String,
    /// Module name where the issue was found, if applicable.
    pub module: Option<String>,
    /// Exact half-open source range, or `None` for a generated/source-less issue.
    pub range: Option<SourceRange>,
}

impl fmt::Display for Diagnostic {
    /// Formats without a source position; use [`DiagnosticEntry::render`] to
    /// include a checked, derived location.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{}]", self.severity)?;
        if let Some(module) = &self.module {
            write!(f, " {module}:")?;
        }
        write!(f, " {}", self.message)
    }
}

/// An ordered diagnostic collection that retains all referenced source documents.
///
/// Cloning a report shares immutable source documents; diagnostic values are
/// cloned because they are small presentation records.
/// Reports are returned by [`Mib::diagnostic_report`](crate::Mib::diagnostic_report),
/// the lossless CST entry points [`cst::parse`](crate::cst::parse) and
/// [`cst::parse_with_config`](crate::cst::parse_with_config), and
/// [`LoadError::DiagnosticThreshold`](crate::LoadError::DiagnosticThreshold).
/// Each report keeps every diagnostic tied to the exact source arena that
/// allocated its IDs.
///
/// ```compile_fail
/// use std::sync::Arc;
/// use mib_rs::{DiagnosticReport, SourceSet};
///
/// // Arbitrary diagnostic/source association is intentionally unavailable.
/// let report = DiagnosticReport::new(Vec::new(), Arc::new(SourceSet::new()));
/// ```
///
/// Checked operations belong to report-owned entries rather than accepting a
/// free [`Diagnostic`] reference:
///
/// ```compile_fail
/// # fn reports() -> (mib_rs::DiagnosticReport, mib_rs::DiagnosticReport) { todo!() }
/// let (first, second) = reports();
/// let foreign = &first.diagnostics()[0];
/// let location = second.range(foreign);
/// ```
#[derive(Debug, Clone)]
pub struct DiagnosticReport {
    diagnostics: Vec<Diagnostic>,
    sources: Arc<SourceSet>,
}

impl DiagnosticReport {
    pub(crate) fn new(mut diagnostics: Vec<Diagnostic>, sources: Arc<SourceSet>) -> Self {
        sort_diagnostics(&mut diagnostics, &sources);
        Self {
            diagnostics,
            sources,
        }
    }

    /// Return the number of diagnostics in this report.
    pub fn len(&self) -> usize {
        self.diagnostics.len()
    }

    /// Return whether this report contains no diagnostics.
    pub fn is_empty(&self) -> bool {
        self.diagnostics.is_empty()
    }

    /// Return diagnostics in canonical deterministic order.
    ///
    /// This slice exposes metadata only. Use [`Self::iter`] or [`Self::get`] to
    /// obtain a report-owned [`DiagnosticEntry`] for checked source operations.
    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }

    /// Iterate over report-owned diagnostic entries in canonical order.
    pub fn iter(&self) -> impl ExactSizeIterator<Item = DiagnosticEntry<'_>> + DoubleEndedIterator {
        (0..self.diagnostics.len()).map(|index| DiagnosticEntry {
            report: self,
            index,
        })
    }

    /// Return a report-owned diagnostic entry by canonical-order index.
    pub fn get(&self, index: usize) -> Option<DiagnosticEntry<'_>> {
        (index < self.diagnostics.len()).then_some(DiagnosticEntry {
            report: self,
            index,
        })
    }

    #[cfg(test)]
    pub(crate) fn shared_sources(&self) -> &Arc<SourceSet> {
        &self.sources
    }
}

/// A diagnostic tied to the report that owns its source arena.
///
/// Entries are created only by [`DiagnosticReport::iter`] and
/// [`DiagnosticReport::get`]. Checked range and position methods therefore
/// cannot accidentally resolve a diagnostic through another report whose
/// compilation-local source IDs happen to have the same numeric value.
#[derive(Clone, Copy, Debug)]
pub struct DiagnosticEntry<'report> {
    report: &'report DiagnosticReport,
    index: usize,
}

impl<'report> DiagnosticEntry<'report> {
    /// Return the diagnostic metadata owned by this entry's report.
    pub fn diagnostic(&self) -> &'report Diagnostic {
        &self.report.diagnostics[self.index]
    }

    /// Resolve and validate a diagnostic's optional source range.
    pub fn range(
        &self,
    ) -> Result<Option<(&'report SourceDocument, SourceRange)>, DiagnosticReportError> {
        let diagnostic = self.diagnostic();
        let Some(range) = diagnostic.range else {
            return Ok(None);
        };
        let source = self
            .report
            .sources
            .get(range.source())
            .ok_or(DiagnosticReportError::SourceNotRetained(range.source()))?;
        source.slice(range)?;
        Ok(Some((source, range)))
    }

    /// Return the checked bytes covered by a diagnostic's range.
    pub fn slice(&self) -> Result<Option<&'report [u8]>, DiagnosticReportError> {
        self.range()?
            .map(|(source, range)| source.slice(range).map_err(Into::into))
            .transpose()
    }

    /// Derive zero-based byte positions for a diagnostic's half-open range.
    pub fn byte_positions(
        &self,
    ) -> Result<Option<(BytePosition, BytePosition)>, DiagnosticReportError> {
        self.range()?
            .map(|(source, range)| {
                Ok((
                    source.byte_position(range.start())?,
                    source.byte_position(range.end())?,
                ))
            })
            .transpose()
    }

    /// Derive zero-based editor positions in an explicit encoding.
    pub fn positions(
        &self,
        encoding: PositionEncoding,
    ) -> Result<Option<(Position, Position)>, DiagnosticReportError> {
        self.range()?
            .map(|(source, range)| {
                Ok((
                    source.position(range.start(), encoding)?,
                    source.position(range.end(), encoding)?,
                ))
            })
            .transpose()
    }

    /// Render a diagnostic with its source label and checked one-based byte range.
    ///
    /// The displayed range is half-open. Source-less diagnostics omit the
    /// location, as does [`Diagnostic`]'s standalone display implementation.
    pub fn render(&self) -> Result<String, DiagnosticReportError> {
        let diagnostic = self.diagnostic();
        let mut rendered = format!("[{}]", diagnostic.severity);
        if let Some((source, _)) = self.range()? {
            let (start, end) = self
                .byte_positions()?
                .expect("a checked source range has byte positions");
            use std::fmt::Write;
            write!(
                rendered,
                " {}:{}:{}-{}:{}",
                source.label(),
                u64::from(start.line()) + 1,
                u64::from(start.column()) + 1,
                u64::from(end.line()) + 1,
                u64::from(end.column()) + 1
            )
            .expect("writing to String cannot fail");
        }
        if let Some(module) = &diagnostic.module {
            rendered.push(' ');
            rendered.push_str(module);
        }
        if diagnostic.module.is_some() || diagnostic.range.is_some() {
            rendered.push(':');
        }
        rendered.push(' ');
        rendered.push_str(&diagnostic.message);
        Ok(rendered)
    }
}

fn sort_diagnostics(diagnostics: &mut [Diagnostic], sources: &SourceSet) {
    diagnostics.sort_by(|left, right| {
        left.code
            .phase()
            .cmp(right.code.phase())
            .then_with(|| left.code.as_code().cmp(right.code.as_code()))
            .then(left.severity.cmp(&right.severity))
            .then(left.module.cmp(&right.module))
            .then_with(|| compare_ranges(left.range, right.range, sources))
            .then(left.message.cmp(&right.message))
    });
}

fn compare_ranges(
    left: Option<SourceRange>,
    right: Option<SourceRange>,
    sources: &SourceSet,
) -> Ordering {
    match (left, right) {
        (None, None) => Ordering::Equal,
        (None, Some(_)) => Ordering::Less,
        (Some(_), None) => Ordering::Greater,
        (Some(left), Some(right)) => {
            let left_source = sources.get(left.source());
            let right_source = sources.get(right.source());
            match (left_source, right_source) {
                (Some(left_source), Some(right_source)) => {
                    compare_origins(left_source.origin(), right_source.origin())
                        .then_with(|| left_source.label().cmp(right_source.label()))
                        .then(left.start().cmp(&right.start()))
                        .then(left.end().cmp(&right.end()))
                }
                (Some(_), None) => Ordering::Less,
                (None, Some(_)) => Ordering::Greater,
                (None, None) => left
                    .start()
                    .cmp(&right.start())
                    .then(left.end().cmp(&right.end())),
            }
        }
    }
}

fn compare_origins(left: &SourceOrigin, right: &SourceOrigin) -> Ordering {
    fn rank(origin: &SourceOrigin) -> u8 {
        match origin {
            SourceOrigin::File { .. } => 0,
            SourceOrigin::Embedded { .. } => 1,
            SourceOrigin::Memory { .. } => 2,
            SourceOrigin::Custom { .. } => 3,
        }
    }

    rank(left)
        .cmp(&rank(right))
        .then_with(|| match (left, right) {
            (SourceOrigin::File { path: left }, SourceOrigin::File { path: right }) => {
                left.cmp(right)
            }
            (
                SourceOrigin::Embedded { identity: left },
                SourceOrigin::Embedded { identity: right },
            )
            | (
                SourceOrigin::Memory { identity: left },
                SourceOrigin::Memory { identity: right },
            ) => left.cmp(right),
            (
                SourceOrigin::Custom {
                    provider: left_provider,
                    identity: left_identity,
                },
                SourceOrigin::Custom {
                    provider: right_provider,
                    identity: right_identity,
                },
            ) => left_provider
                .cmp(right_provider)
                .then(left_identity.cmp(right_identity)),
            _ => Ordering::Equal,
        })
}

/// Failure to resolve or convert a source location retained by a report.
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
pub enum DiagnosticReportError {
    /// The diagnostic names a source outside the report's retained source set.
    #[error("source {0} is not retained by this diagnostic report")]
    SourceNotRetained(SourceId),
    /// The retained source rejected the diagnostic's range.
    #[error(transparent)]
    Range(#[from] SourceRangeError),
    /// The retained source rejected a requested position conversion.
    #[error(transparent)]
    Position(#[from] PositionError),
}

/// Controls diagnostic collection, presentation, and failure policy.
///
/// This does NOT control resolver behavior.
/// Resolver fallback behavior is controlled by [`ResolverStrictness`](crate::types::ResolverStrictness).
#[derive(Debug, Clone)]
pub struct DiagnosticConfig {
    /// Which severity levels are reported. See [`ReportingLevel`].
    pub reporting: ReportingLevel,
    /// Diagnostics at this severity or above cause loading to fail.
    pub fail_at: Severity,
    /// Per-code severity overrides (e.g. promote a warning to error).
    ///
    /// Overrides change the severity stored on emitted diagnostics and used by
    /// failure checks. Demoting a diagnostic does not by itself suppress it;
    /// use [`ignore`](Self::ignore) for suppression.
    pub overrides: HashMap<DiagCode, Severity>,
    /// Glob patterns for [`DiagCode`] strings to suppress (supports `*` and `?`).
    pub ignore: Vec<String>,
}

impl Default for DiagnosticConfig {
    fn default() -> Self {
        DiagnosticConfig {
            reporting: ReportingLevel::Default,
            fail_at: Severity::Severe,
            overrides: HashMap::new(),
            ignore: Vec::new(),
        }
    }
}

impl DiagnosticConfig {
    /// Returns a preset configuration for the given [`ReportingLevel`].
    pub fn for_reporting(level: ReportingLevel) -> Self {
        match level {
            ReportingLevel::Verbose => Self::verbose(),
            ReportingLevel::Default => Self::default(),
            ReportingLevel::Quiet => Self::quiet(),
            ReportingLevel::Silent => Self::silent(),
        }
    }

    /// Verbose preset: report all diagnostics including style and info.
    pub fn verbose() -> Self {
        DiagnosticConfig {
            reporting: ReportingLevel::Verbose,
            fail_at: Severity::Severe,
            overrides: HashMap::new(),
            ignore: Vec::new(),
        }
    }

    /// Quiet preset: report errors and above only.
    pub fn quiet() -> Self {
        DiagnosticConfig {
            reporting: ReportingLevel::Quiet,
            fail_at: Severity::Severe,
            overrides: HashMap::new(),
            ignore: Vec::new(),
        }
    }

    /// Silent preset: suppress all diagnostics. Only fatal errors cause failure.
    pub fn silent() -> Self {
        DiagnosticConfig {
            reporting: ReportingLevel::Silent,
            fail_at: Severity::Fatal,
            overrides: HashMap::new(),
            ignore: Vec::new(),
        }
    }

    /// Returns the configured severity for a diagnostic code.
    ///
    /// This is the severity stored on emitted diagnostics and evaluated by
    /// [`should_fail`](Self::should_fail).
    pub fn effective_severity(&self, code: DiagCode) -> Severity {
        self.overrides
            .get(&code)
            .copied()
            .unwrap_or_else(|| code.severity())
    }

    /// Returns `true` if the diagnostic code matches an [`ignore`](Self::ignore) pattern.
    pub fn is_ignored(&self, code: DiagCode) -> bool {
        let code_str = code.as_code();
        self.ignore
            .iter()
            .any(|pattern| match_glob(pattern, code_str))
    }

    /// Returns `true` if the reporting level collects the given severity.
    ///
    /// Fatal diagnostics are always collected, including in silent mode.
    /// Ignore patterns are a separate policy evaluated by
    /// [`should_collect`](Self::should_collect).
    pub fn should_report(&self, severity: Severity) -> bool {
        severity == Severity::Fatal
            || self
                .max_reported_severity()
                .is_some_and(|max| severity <= max)
    }

    /// Returns `true` if a diagnostic with the given code should be collected.
    ///
    /// Promotions can bring a diagnostic into the configured reporting level,
    /// while demotions do not discard a diagnostic that its default severity
    /// would collect. Effective fatal diagnostics are always collected,
    /// including when ignored or reporting is silent.
    pub fn should_collect(&self, code: DiagCode) -> bool {
        let effective_severity = self.effective_severity(code);

        if effective_severity == Severity::Fatal {
            return true;
        }

        if self.is_ignored(code) {
            return false;
        }

        self.should_report(code.severity()) || self.should_report(effective_severity)
    }

    /// Returns `true` if the given effective severity meets or exceeds the
    /// [`fail_at`](Self::fail_at) threshold.
    pub fn should_fail(&self, severity: Severity) -> bool {
        severity <= self.fail_at
    }

    /// Returns the maximum severity number (least severe) that should be
    /// reported at the current reporting level.
    ///
    /// - Verbose: report all diagnostics (sev 0-6)
    /// - Default: report Minor and above (sev 0-3)
    /// - Quiet: report Error and above (sev 0-2)
    /// - Silent: report nothing (except fatal, handled by caller)
    fn max_reported_severity(&self) -> Option<Severity> {
        match self.reporting {
            ReportingLevel::Verbose => Some(Severity::Info),
            ReportingLevel::Default => Some(Severity::Minor),
            ReportingLevel::Quiet => Some(Severity::Error),
            ReportingLevel::Silent => None,
        }
    }
}

/// Glob matching on diagnostic codes. Supports * and ? wildcards.
/// Diagnostic codes contain no slashes, so * matches any sequence of characters.
fn match_glob(pattern: &str, s: &str) -> bool {
    glob_match(pattern.as_bytes(), s.as_bytes())
}

fn glob_match(pattern: &[u8], s: &[u8]) -> bool {
    let mut pi = 0;
    let mut si = 0;
    let mut star_pi: Option<usize> = None;
    let mut star_si = 0;

    while si < s.len() {
        if pi < pattern.len() && (pattern[pi] == b'?' || pattern[pi] == s[si]) {
            pi += 1;
            si += 1;
        } else if pi < pattern.len() && pattern[pi] == b'*' {
            star_pi = Some(pi);
            star_si = si;
            pi += 1;
        } else if let Some(sp) = star_pi {
            pi = sp + 1;
            star_si += 1;
            si = star_si;
        } else {
            return false;
        }
    }

    while pi < pattern.len() && pattern[pi] == b'*' {
        pi += 1;
    }

    pi == pattern.len()
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use crate::source::SourceOrigin;

    use super::*;

    #[test]
    fn diagnostic_display() {
        let d = Diagnostic {
            severity: Severity::Error,
            code: DiagCode::ImportNotFound,
            message: "symbol foo not found".to_string(),
            module: Some("IF-MIB".to_string()),
            range: None,
        };
        assert_eq!(d.to_string(), "[error] IF-MIB: symbol foo not found");
    }

    #[test]
    fn diagnostic_display_no_location() {
        let d = Diagnostic {
            severity: Severity::Warning,
            code: DiagCode::ImportUnused,
            message: "unused import".to_string(),
            module: None,
            range: None,
        };
        assert_eq!(d.to_string(), "[warning] unused import");
    }

    #[test]
    fn report_retains_and_renders_checked_full_range() {
        let mut sources = SourceSet::new();
        let source_id = sources
            .insert(
                SourceOrigin::memory("diagnostic-report"),
                "diagnostic-report",
                Arc::from(&b"first\nsecond"[..]),
            )
            .unwrap();
        let range = sources.get(source_id).unwrap().range(8..10).unwrap();
        let diagnostic = Diagnostic {
            severity: Severity::Error,
            code: DiagCode::ParseError,
            message: "precise range".to_string(),
            module: Some("TEST-MIB".to_string()),
            range: Some(range),
        };
        let sources = Arc::new(sources);
        let report = DiagnosticReport::new(vec![diagnostic], Arc::clone(&sources));
        drop(sources);

        let entry = report.get(0).unwrap();
        assert_eq!(entry.slice().unwrap(), Some(&b"co"[..]));
        assert_eq!(
            entry.byte_positions().unwrap(),
            Some((BytePosition::new(1, 2), BytePosition::new(1, 4)))
        );
        assert_eq!(
            entry.render().unwrap(),
            "[error] diagnostic-report:2:3-2:5 TEST-MIB: precise range"
        );
    }

    #[test]
    fn report_rejects_a_range_whose_source_is_not_retained() {
        let mut retained = SourceSet::new();
        retained
            .insert(
                SourceOrigin::memory("retained"),
                "retained",
                Arc::from(&b"retained"[..]),
            )
            .unwrap();
        let mut foreign = SourceSet::new();
        foreign
            .insert(
                SourceOrigin::memory("foreign-first"),
                "foreign-first",
                Arc::from(&b"first"[..]),
            )
            .unwrap();
        let foreign_id = foreign
            .insert(
                SourceOrigin::memory("foreign-second"),
                "foreign-second",
                Arc::from(&b"second"[..]),
            )
            .unwrap();
        let range = foreign.get(foreign_id).unwrap().range(0..1).unwrap();
        let diagnostic = Diagnostic {
            severity: Severity::Error,
            code: DiagCode::ParseError,
            message: "foreign".to_string(),
            module: None,
            range: Some(range),
        };
        let report = DiagnosticReport::new(vec![diagnostic], Arc::new(retained));

        let entry = report.get(0).unwrap();
        assert!(matches!(
            entry.range(),
            Err(DiagnosticReportError::SourceNotRetained(id)) if id == foreign_id
        ));
        assert!(matches!(
            entry.render(),
            Err(DiagnosticReportError::SourceNotRetained(id)) if id == foreign_id
        ));
    }

    #[test]
    fn report_entries_cannot_cross_resolve_aliased_source_ids() {
        fn report(identity: &str, bytes: &'static [u8]) -> DiagnosticReport {
            let mut sources = SourceSet::new();
            let source_id = sources
                .insert(SourceOrigin::memory(identity), identity, Arc::from(bytes))
                .unwrap();
            let range = sources
                .get(source_id)
                .unwrap()
                .range(0..bytes.len())
                .unwrap();
            DiagnosticReport::new(
                vec![Diagnostic {
                    severity: Severity::Error,
                    code: DiagCode::ParseError,
                    message: identity.to_string(),
                    module: None,
                    range: Some(range),
                }],
                Arc::new(sources),
            )
        }

        let first = report("first", b"alpha");
        let second = report("second", b"bravo!");
        let first_entry = first.get(0).unwrap();
        let second_entry = second.get(0).unwrap();

        assert_eq!(
            first_entry.diagnostic().range.unwrap().source(),
            second_entry.diagnostic().range.unwrap().source()
        );
        assert_eq!(first_entry.slice().unwrap(), Some(&b"alpha"[..]));
        assert_eq!(second_entry.slice().unwrap(), Some(&b"bravo!"[..]));
        assert_eq!(first_entry.range().unwrap().unwrap().0.label(), "first");
        assert_eq!(second_entry.range().unwrap().unwrap().0.label(), "second");
    }

    #[test]
    fn canonical_order_uses_stable_source_identity_not_source_id_allocation() {
        fn report(order: [&str; 2]) -> DiagnosticReport {
            let mut sources = SourceSet::new();
            for identity in order {
                sources
                    .insert(
                        SourceOrigin::memory(identity),
                        identity,
                        Arc::from(&b"x"[..]),
                    )
                    .unwrap();
            }
            let diagnostics = sources
                .iter()
                .map(|source| Diagnostic {
                    severity: Severity::Error,
                    code: DiagCode::ParseError,
                    message: source.label().to_string(),
                    module: Some("TEST-MIB".to_string()),
                    range: Some(source.range(0..1).unwrap()),
                })
                .collect();
            DiagnosticReport::new(diagnostics, Arc::new(sources))
        }

        let forward = report(["a-source", "b-source"]);
        let reverse = report(["b-source", "a-source"]);
        let labels = |report: &DiagnosticReport| {
            report
                .iter()
                .map(|entry| entry.range().unwrap().unwrap().0.label().to_string())
                .collect::<Vec<_>>()
        };
        assert_eq!(labels(&forward), vec!["a-source", "b-source"]);
        assert_eq!(labels(&reverse), labels(&forward));
    }

    #[test]
    fn canonical_order_is_deterministic_for_source_less_diagnostics() {
        let diagnostic = |message: &str| Diagnostic {
            severity: Severity::Error,
            code: DiagCode::ParseError,
            message: message.to_string(),
            module: None,
            range: None,
        };
        let report = DiagnosticReport::new(
            vec![diagnostic("second"), diagnostic("first")],
            Arc::new(SourceSet::new()),
        );
        assert_eq!(
            report
                .diagnostics()
                .iter()
                .map(|diagnostic| diagnostic.message.as_str())
                .collect::<Vec<_>>(),
            vec!["first", "second"]
        );
    }

    #[test]
    fn glob_matching() {
        assert!(match_glob("identifier-*", "identifier-underscore"));
        assert!(match_glob("identifier-*", "identifier-length-32"));
        assert!(!match_glob("identifier-*", "import-not-found"));
        assert!(match_glob("*", "anything"));
        assert!(match_glob("exact-match", "exact-match"));
        assert!(!match_glob("exact-match", "exact-mismatch"));
    }

    #[test]
    fn should_report_respects_level() {
        let config = DiagnosticConfig::default();
        // Default reports Minor and above (sev 0-3)
        assert!(config.should_report(Severity::Error));
        assert!(config.should_report(Severity::Minor));
        assert!(!config.should_report(Severity::Style));
    }

    #[test]
    fn should_report_silent() {
        let config = DiagnosticConfig::silent();
        // Silent reports nothing except fatal
        assert!(config.should_report(Severity::Fatal));
        assert!(!config.should_report(Severity::Error));
        assert!(!config.should_report(Severity::Style));
    }

    #[test]
    fn should_report_verbose() {
        let config = DiagnosticConfig::verbose();
        // Verbose reports everything
        assert!(config.should_report(Severity::Error));
        assert!(config.should_report(Severity::Style));
    }

    #[test]
    fn effective_severity_applies_override() {
        let mut config = DiagnosticConfig::default();
        config
            .overrides
            .insert(DiagCode::MacroNotImported, Severity::Severe);

        assert_eq!(
            config.effective_severity(DiagCode::MacroNotImported),
            Severity::Severe
        );
        assert_eq!(
            config.effective_severity(DiagCode::ParseError),
            Severity::Error
        );
    }

    #[test]
    fn promotion_affects_collection() {
        let mut config = DiagnosticConfig::default();
        config
            .overrides
            .insert(DiagCode::IdentifierUnderscore, Severity::Minor);

        assert!(config.should_collect(DiagCode::IdentifierUnderscore));
    }

    #[test]
    fn demotion_does_not_discard_collected_diagnostic() {
        let mut config = DiagnosticConfig::quiet();
        config
            .overrides
            .insert(DiagCode::ParseError, Severity::Info);

        assert!(config.should_collect(DiagCode::ParseError));
    }

    #[test]
    fn ignore_suppresses_nonfatal_diagnostic() {
        let mut config = DiagnosticConfig::verbose();
        config.ignore.push("parse-*".to_string());

        assert!(config.is_ignored(DiagCode::ParseError));
        assert!(!config.should_collect(DiagCode::ParseError));
    }

    #[test]
    fn effective_fatal_is_always_collected() {
        let mut config = DiagnosticConfig::silent();
        config
            .overrides
            .insert(DiagCode::IdentifierUnderscore, Severity::Fatal);
        config.ignore.push("identifier-*".to_string());

        assert!(config.is_ignored(DiagCode::IdentifierUnderscore));
        assert!(config.should_collect(DiagCode::IdentifierUnderscore));
    }

    #[test]
    fn should_fail_threshold() {
        let config = DiagnosticConfig::default();
        assert!(config.should_fail(Severity::Fatal));
        assert!(config.should_fail(Severity::Severe));
        assert!(!config.should_fail(Severity::Error));
    }

    #[test]
    fn for_reporting_presets() {
        let verbose = DiagnosticConfig::for_reporting(ReportingLevel::Verbose);
        assert!(matches!(verbose.reporting, ReportingLevel::Verbose));

        let silent = DiagnosticConfig::for_reporting(ReportingLevel::Silent);
        assert!(matches!(silent.reporting, ReportingLevel::Silent));
        assert!(matches!(silent.fail_at, Severity::Fatal));
    }
}