Skip to main content

maybe_fatal/
classified.rs

1use crate::{Diagnostic, DiagnosticSeverity, code};
2
3/// A [`Diagnostic`] with an explicit [severity](DiagnosticSeverity).
4///
5/// This is constructed through the [`Diagnostic::classify`] method.
6pub struct ClassifiedDiagnostic<S, D = code::DefaultDiscriminant> {
7    /// The diagnostic being classified.
8    pub(super) inner: Diagnostic<S, D>,
9
10    /// The severity of the diagnostic.
11    pub severity: DiagnosticSeverity,
12}
13
14impl<S, D> ClassifiedDiagnostic<S, D> {
15    /// Changes this diagnostic's severity to [`Error`](DiagnosticSeverity::Error).
16    pub const fn make_error(&mut self) -> &mut Self {
17        self.severity = DiagnosticSeverity::Error;
18        self
19    }
20
21    /// Changes this diagnostic's severity to [`Warning`](DiagnosticSeverity::Warning).
22    pub const fn make_warning(&mut self) -> &mut Self {
23        self.severity = DiagnosticSeverity::Warning;
24        self
25    }
26
27    /// Changes this diagnostic's severity to [`Advice`](DiagnosticSeverity::Advice).
28    pub const fn make_advice(&mut self) -> &mut Self {
29        self.severity = DiagnosticSeverity::Advice;
30        self
31    }
32
33    /// Reports this diagnostic using the given configuration.
34    ///
35    /// See the [`ariadne`] documentation for more details.
36    pub fn report<C>(self, config: ariadne::Config, cache: C) -> std::io::Result<()>
37    where
38        S: ariadne::Span,
39        D: code::Discriminant,
40        C: ariadne::Cache<S::SourceId>,
41    {
42        self.inner.report_with(self.severity, config, cache)
43    }
44}