1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use std::fmt::{Display, Formatter};

use ariadne::ReportKind;

use crate::{errors::ValkyrieReport, ValkyrieError, ValkyrieErrorKind};

#[derive(Debug)]
pub struct RuntimeError {
    message: String,
}

impl Display for RuntimeError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.message)
    }
}

impl RuntimeError {
    pub fn new(message: impl Display) -> Self {
        Self { message: message.to_string() }
    }
    pub fn as_report(&self, kind: ReportKind) -> ValkyrieReport {
        let mut report = ValkyrieReport::build(kind, 0usize, 0);
        report.set_message(self.to_string());
        report.finish()
    }
}

impl ValkyrieError {
    pub fn runtime_error(message: impl Into<String>) -> Self {
        let this = RuntimeError { message: message.into() };
        Self { kind: ValkyrieErrorKind::Runtime(Box::new(this)), level: ReportKind::Error }
    }
}