Skip to main content

tsh/compiler/
diagnostics.rs

1use miette::{Diagnostic, NamedSource, SourceSpan};
2use thiserror::Error;
3
4#[derive(Debug, Error, Diagnostic)]
5#[error("{message}")]
6pub struct CompileError {
7    pub message: String,
8    #[source_code]
9    pub source_code: NamedSource<String>,
10    #[label("{message}")]
11    pub span: SourceSpan,
12}
13impl CompileError {
14    pub fn new(
15        name: impl Into<String>,
16        source: &str,
17        start: usize,
18        len: usize,
19        message: impl Into<String>,
20    ) -> Self {
21        Self {
22            message: message.into(),
23            source_code: NamedSource::new(name.into(), source.to_owned()),
24            span: (
25                start.min(source.len()),
26                len.min(source.len().saturating_sub(start)),
27            )
28                .into(),
29        }
30    }
31    pub fn plain(message: impl Into<String>) -> Self {
32        Self::new("tsh", "", 0, 0, message)
33    }
34}