rune/core/
error.rs

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::fmt::{Display, Formatter};

#[derive(Debug, PartialEq)]
pub(crate) enum Type {
    Int,
    Char,
    Cons,
    Vec,
    Record,
    HashTable,
    Sequence,
    BufferOrName,
    String,
    Symbol,
    Float,
    Func,
    Number,
    List,
    Buffer,
}

/// Error provided if object was the wrong type
#[derive(Debug, PartialEq)]
pub(crate) struct TypeError {
    expect: Type,
    actual: Type,
    print: String,
}

impl std::error::Error for TypeError {}

impl Display for TypeError {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        let Self { expect, actual, print } = self;
        write!(f, "expected {expect:?}, found {actual:?}: {print}")
    }
}

impl TypeError {
    /// Get a type error from an object.
    pub(crate) fn new<'ob, T>(expect: Type, obj: T) -> Self
    where
        T: Into<super::object::ObjectType<'ob>>,
    {
        let obj = obj.into();
        Self { expect, actual: obj.get_type(), print: obj.to_string() }
    }
}