Skip to main content

litcheck_core/diagnostics/
mod.rs

1mod filename;
2mod location;
3mod selection;
4mod source_file;
5mod source_manager;
6mod span;
7
8pub use serde_spanned;
9
10pub use self::source_manager::SourceManagerExt;
11pub use self::{
12    filename::FileName,
13    location::{FileLineCol, Location},
14    selection::{Position, Selection},
15    source_file::{
16        ByteIndex, ByteOffset, ColumnIndex, ColumnNumber, LineIndex, LineNumber, SourceContent,
17        SourceContentUpdateError, SourceFile, SourceFileRef, SourceLanguage,
18    },
19    source_manager::{
20        DefaultSourceManager, SourceId, SourceManager, SourceManagerError, SourceManagerSync,
21    },
22    span::{SourceSpan, Span, Spanned},
23};
24
25pub use miette::{
26    Diagnostic, IntoDiagnostic, LabeledSpan, Report, Severity, SourceCode, SourceOffset, WrapErr,
27    bail, diagnostic,
28};
29
30#[cfg(feature = "fancy-diagnostics")]
31pub use miette::set_panic_hook;
32
33pub type Diag = miette::MietteDiagnostic;
34pub type DiagResult<T> = miette::Result<T>;
35
36use std::{borrow::Cow, hash::Hash};
37
38use crate::StaticCow;
39
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct Label {
42    span: SourceSpan,
43    label: Option<StaticCow<str>>,
44}
45
46impl Label {
47    pub fn at(span: SourceSpan) -> Self {
48        Self { span, label: None }
49    }
50
51    pub fn point<L>(source_id: SourceId, offset: impl Into<ByteIndex>, label: L) -> Self
52    where
53        StaticCow<str>: From<L>,
54    {
55        Self {
56            span: SourceSpan::at(source_id, offset),
57            label: Some(Cow::from(label)),
58        }
59    }
60
61    pub fn new<L>(span: SourceSpan, label: L) -> Self
62    where
63        StaticCow<str>: From<L>,
64    {
65        Self {
66            span,
67            label: Some(Cow::from(label)),
68        }
69    }
70
71    pub fn label(&self) -> Option<&str> {
72        self.label.as_deref()
73    }
74
75    #[inline(always)]
76    pub const fn span(&self) -> SourceSpan {
77        self.span
78    }
79}
80
81impl From<Label> for SourceSpan {
82    #[inline(always)]
83    fn from(label: Label) -> SourceSpan {
84        label.span
85    }
86}
87
88impl From<Label> for LabeledSpan {
89    #[inline]
90    fn from(label: Label) -> LabeledSpan {
91        if let Some(message) = label.label {
92            LabeledSpan::at(label.span, message)
93        } else {
94            LabeledSpan::underline(label.span)
95        }
96    }
97}