tin/
error.rs

1//! Common error types and utilities.
2use std::result;
3
4use crate::diagnostic;
5use crate::interpreter;
6use crate::ir;
7use crate::parser;
8
9/// An error that occurs while interacting with Tin.
10#[derive(Clone, Debug, Fail, PartialEq)]
11pub enum Error {
12    /// Interpreting the code failed.
13    ///
14    /// This can happen either during an interpreter run, or during compiler constant evaluation
15    /// which internally uses the interpreter.
16    #[fail(display = "interpreter error")]
17    Interpreter(interpreter::Error),
18    /// Semantically understanding the source code failed.
19    #[fail(display = "IR error")]
20    Ir(#[cause] ir::Error),
21    /// Parsing the source code failed.
22    #[fail(display = "parser error")]
23    Parser(#[cause] parser::Error),
24}
25
26/// A convenience result wrapper for the [`Error`] type.
27pub type Result<A> = result::Result<A, Error>;
28
29impl diagnostic::Diagnostic for Error {
30    fn to_diagnostics(&self, result: &mut Vec<codespan_reporting::Diagnostic>) {
31        match self {
32            Error::Parser(e) => e.to_diagnostics(result),
33            Error::Interpreter(_) => {}
34            Error::Ir(e) => e.to_diagnostics(result),
35        }
36    }
37}
38
39impl From<interpreter::Error> for Error {
40    fn from(err: interpreter::Error) -> Self {
41        Error::Interpreter(err)
42    }
43}
44
45impl From<ir::Error> for Error {
46    fn from(err: ir::Error) -> Self {
47        Error::Ir(err)
48    }
49}
50
51impl From<parser::Error> for Error {
52    fn from(err: parser::Error) -> Self {
53        Error::Parser(err)
54    }
55}