Skip to main content

svugy_parse/markdown/
diagnostic.rs

1//! Report what a Markdown diagram file holds that cannot be read, each error labelled at its source, and each description link left unlinked.
2
3use std::ops::Range;
4
5use miette::{Diagnostic, SourceSpan};
6use thiserror::Error;
7
8use super::ast::Span;
9use super::{MarkdownImport, MarkdownParse};
10use crate::diagnostic::{SourceError, SourceLanguage};
11
12/// Mark Markdown as a source language: its errors are Markdown errors, its warnings the description links it leaves unlinked.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct MarkdownLanguage;
15
16/// Report a Markdown error: a part of the diagram file that cannot be read, left out of the model.
17pub type MarkdownError = SourceError<MarkdownLanguage>;
18
19/// Report a description link whose `#` target names no component or edge, or several, so it stays as written and shows as plain text.
20#[derive(Debug, Clone, PartialEq, Eq, Error, Diagnostic)]
21#[error("{message}")]
22#[diagnostic(code(svugy::markdown::unresolved_target), severity(Warning))]
23pub struct UnresolvedTarget {
24    pub message: String,
25    #[label("linked here")]
26    pub span: SourceSpan,
27}
28
29/// Record the Markdown errors and warnings the layers raise, handed from one layer to the next.
30pub(crate) type Diagnostics = crate::diagnostic::Diagnostics<MarkdownLanguage>;
31
32impl SourceLanguage for MarkdownLanguage {
33    const ERROR_CODE: &'static str = "svugy::markdown";
34    type Warning = UnresolvedTarget;
35
36    fn import(source_text: &str) -> MarkdownImport {
37        let parsed = MarkdownParse::parse(source_text);
38        parsed.lower()
39    }
40}
41
42impl Diagnostics {
43    /// Record that the description link target at `span` stays unlinked, `message` saying why.
44    pub(crate) fn record_unresolved_target(&mut self, message: String, span: Span) {
45        let byte_range: Range<usize> = span.into();
46        let span = SourceSpan::from(byte_range);
47        let warning = UnresolvedTarget { message, span };
48        self.warnings.push(warning);
49    }
50}