aplang_lib/interpreter/
errors.rs

1use miette::{Diagnostic, LabeledSpan, NamedSource, Report, SourceCode, SourceSpan};
2use std::fmt::{Debug, Display};
3use std::sync::Arc;
4use thiserror::Error;
5
6#[derive(Error, Debug)]
7#[error("error{} occurred", if reports.len() > 1 {"s"} else {""})]
8pub struct Reports {
9    reports: Vec<Report>,
10}
11
12impl Diagnostic for Reports {
13    fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
14        Some(Box::new("failure"))
15    }
16
17    fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
18        Some(Box::new(format!(
19            "See individual error{} for more details",
20            if self.reports.len() > 1 { "s" } else { "" }
21        )))
22    }
23
24    fn source_code(&self) -> Option<&dyn SourceCode> {
25        None
26    }
27
28    fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
29        None
30    }
31
32    fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
33        Some(Box::new(self.reports.iter().map(|report| report.as_ref())))
34    }
35}
36
37impl From<Vec<Report>> for Reports {
38    fn from(value: Vec<Report>) -> Self {
39        Self { reports: value }
40    }
41}
42
43#[derive(Debug, Error, Diagnostic)]
44#[error("{message}")]
45#[diagnostic(code(aplang::runtime))]
46pub struct RuntimeError {
47    pub named_source: NamedSource<Arc<str>>,
48    #[label("{label}")]
49    pub span: SourceSpan,
50    pub message: String,
51    #[help("{help}")]
52    pub help: String,
53    pub label: String,
54}