rune/core/
error.rs

1use std::fmt::{Display, Formatter};
2
3#[derive(Debug, PartialEq)]
4pub(crate) enum Type {
5    Int,
6    Char,
7    Cons,
8    Vec,
9    Record,
10    HashTable,
11    Sequence,
12    BufferOrName,
13    BufferOrString,
14    String,
15    StringOrChar,
16    Symbol,
17    Float,
18    Func,
19    Number,
20    List,
21    Buffer,
22    CharTable,
23    BigInt,
24}
25
26/// Error provided if object was the wrong type
27#[derive(Debug, PartialEq)]
28pub(crate) struct TypeError {
29    expect: Type,
30    actual: Type,
31    print: String,
32}
33
34impl std::error::Error for TypeError {}
35
36impl Display for TypeError {
37    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
38        let Self { expect, actual, print } = self;
39        write!(f, "expected {expect:?}, found {actual:?}: {print}")
40    }
41}
42
43impl TypeError {
44    /// Get a type error from an object.
45    pub(crate) fn new<'ob, T>(expect: Type, obj: T) -> Self
46    where
47        T: Into<super::object::ObjectType<'ob>>,
48    {
49        let obj = obj.into();
50        Self { expect, actual: obj.get_type(), print: obj.to_string() }
51    }
52}