valkyrie_errors/errors/
mod.rs

1use std::ops::Range;
2
3use ariadne::{Report, ReportKind};
4
5use crate::{parsing::SyntaxError, DuplicateError, FileID, RuntimeError};
6
7pub mod display;
8
9pub type ValkyrieReport = Report<(FileID, Range<usize>)>;
10
11pub type ValkyrieResult<T = ()> = Result<T, ValkyrieError>;
12
13#[derive(Clone)]
14pub struct ValkyrieError {
15    pub kind: ValkyrieErrorKind,
16    pub level: ReportKind,
17}
18
19#[derive(Clone)]
20pub enum ValkyrieErrorKind {
21    Duplicate(Box<DuplicateError>),
22    Runtime(Box<RuntimeError>),
23    Parsing(Box<SyntaxError>),
24}
25
26impl ValkyrieError {
27    pub fn set_file(&mut self, file: FileID) {
28        match &mut self.kind {
29            ValkyrieErrorKind::Duplicate(_) => {}
30            ValkyrieErrorKind::Runtime(_) => {}
31            ValkyrieErrorKind::Parsing(s) => {
32                s.span.file = file;
33            }
34        }
35    }
36
37    pub fn as_report(&self) -> ValkyrieReport {
38        match &self.kind {
39            ValkyrieErrorKind::Duplicate(e) => e.as_report(self.level),
40            ValkyrieErrorKind::Runtime(e) => e.as_report(self.level),
41            ValkyrieErrorKind::Parsing(e) => e.as_report(self.level),
42        }
43    }
44}