valkyrie_errors/runtime/
mod.rs1use std::{
2 fmt::{Display, Formatter},
3 panic::Location,
4};
5
6use ariadne::ReportKind;
7
8use crate::{errors::ValkyrieReport, FileSpan, SyntaxError, ValkyrieError, ValkyrieErrorKind};
9
10mod for_serde;
11
12#[derive(Clone, Debug)]
13pub struct RuntimeError {
14 message: String,
15}
16
17impl From<RuntimeError> for ValkyrieError {
18 fn from(value: RuntimeError) -> Self {
19 ValkyrieError { kind: ValkyrieErrorKind::Runtime(Box::new(value)), level: ReportKind::Error }
20 }
21}
22
23impl From<()> for ValkyrieError {
24 #[track_caller]
25 fn from(_: ()) -> Self {
26 let caller = Location::caller();
27 RuntimeError { message: caller.to_string() }.into()
28 }
29}
30
31impl From<std::io::Error> for ValkyrieError {
32 fn from(value: std::io::Error) -> Self {
33 RuntimeError { message: value.to_string() }.into()
34 }
35}
36
37impl Display for RuntimeError {
38 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39 f.write_str(&self.message)
40 }
41}
42
43impl RuntimeError {
44 pub fn new(message: impl Display) -> Self {
45 Self { message: message.to_string() }
46 }
47 pub fn as_report(&self, kind: ReportKind) -> ValkyrieReport {
48 let mut report = ValkyrieReport::build(kind, 0usize, 0);
49 report.set_message(self.to_string());
50 report.finish()
51 }
52}
53
54impl ValkyrieError {
55 pub fn syntax_error(message: impl Into<String>, position: FileSpan) -> Self {
56 let this = SyntaxError { info: message.into(), span: position };
57 Self { kind: ValkyrieErrorKind::Parsing(Box::new(this)), level: ReportKind::Error }
58 }
59
60 pub fn runtime_error(message: impl Into<String>) -> Self {
61 let this = RuntimeError { message: message.into() };
62 Self { kind: ValkyrieErrorKind::Runtime(Box::new(this)), level: ReportKind::Error }
63 }
64}