nyar_error/errors/
mod.rs

1use diagnostic::{Diagnostic, ReportKind, SourceID};
2
3use crate::{parsing::SyntaxError, DuplicateError, MissingError, RuntimeError, SourceSpan};
4
5mod convert;
6pub mod display;
7
8pub type Validation<T> = validatus::Validation<T, NyarError>;
9pub type Result<T = ()> = core::result::Result<T, NyarError>;
10
11#[derive(Clone, Eq, PartialEq)]
12pub struct NyarError {
13    kind: Box<NyarErrorKind>,
14    level: ReportKind,
15}
16
17#[derive(Clone, Eq, PartialEq)]
18pub enum NyarErrorKind {
19    Missing(MissingError),
20    Duplicate(DuplicateError),
21    Runtime(RuntimeError),
22    Parsing(SyntaxError),
23    Custom(String),
24}
25
26impl NyarError {
27    pub fn set_file(&mut self, file: SourceID) {
28        match self.kind.as_mut() {
29            NyarErrorKind::Duplicate(_) => {}
30            NyarErrorKind::Runtime(_) => {}
31            NyarErrorKind::Parsing(s) => s.span.set_file(file),
32            NyarErrorKind::Custom(_) => {}
33            NyarErrorKind::Missing(s) => s.span.set_file(file),
34        }
35    }
36    pub fn with_file(mut self, file: SourceID) -> Self {
37        self.set_file(file);
38        self
39    }
40
41    pub fn set_span(&mut self, span: SourceSpan) {
42        match self.kind.as_mut() {
43            NyarErrorKind::Duplicate(_) => {}
44            NyarErrorKind::Runtime(_) => {}
45            NyarErrorKind::Parsing(s) => s.span = span,
46            NyarErrorKind::Custom(_) => {}
47            NyarErrorKind::Missing(s) => s.span = span,
48        }
49    }
50    pub fn with_span(mut self, file: SourceSpan) -> Self {
51        self.set_span(file);
52        self
53    }
54
55    pub fn as_report(&self) -> Diagnostic {
56        match self.kind.as_ref() {
57            NyarErrorKind::Missing(e) => e.as_report(self.level),
58            NyarErrorKind::Duplicate(e) => e.as_report(self.level),
59            NyarErrorKind::Runtime(e) => e.as_report(self.level),
60            NyarErrorKind::Parsing(e) => e.as_report(self.level),
61            NyarErrorKind::Custom(e) => Diagnostic::new(self.level).with_message(e).finish(),
62        }
63    }
64}
65
66#[allow(clippy::wrong_self_convention)]
67impl NyarErrorKind {
68    pub fn as_error(self, level: ReportKind) -> NyarError {
69        NyarError { kind: Box::new(self), level }
70    }
71}