Skip to main content

oxc_markdown_parser/
diagnostic.rs

1//! Parse diagnostics.
2//!
3//! CommonMark has no syntax errors, so markdown-mode parses always return an empty diagnostics list;
4//! the list exists for MDX mode (unclosed JSX tags, stray `<`/`{`, malformed expressions, …)
5//! and for input-level limits.
6//! Diagnostics never abort the parse, an AST is always produced.
7
8use crate::pos::Span;
9use std::fmt::Display;
10
11#[derive(Clone, Debug)]
12pub struct Diagnostic {
13    pub kind: DiagnosticKind,
14    pub span: Span,
15}
16
17impl Diagnostic {
18    pub(crate) fn new(kind: DiagnosticKind, span: Span) -> Self {
19        Self { kind, span }
20    }
21}
22
23// MDX mode will add variants (unclosed JSX, malformed expressions, …);
24// non_exhaustive keeps those additions from being breaking changes.
25#[derive(Clone, Debug, PartialEq, Eq)]
26#[non_exhaustive]
27pub enum DiagnosticKind {
28    /// The source exceeds the maximum supported size (4 GiB).
29    SourceTooLong,
30}
31
32impl Display for Diagnostic {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        let message = match &self.kind {
35            DiagnosticKind::SourceTooLong => "source exceeds the maximum supported size (4 GiB)",
36        };
37        write!(f, "{message} at {}..{}", self.span.start, self.span.end)
38    }
39}