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    ChannelSender,
25    ChannelReceiver,
26}
27
28/// Error provided if object was the wrong type
29#[derive(Debug, PartialEq)]
30pub(crate) struct TypeError {
31    expect: Type,
32    actual: Type,
33    print: String,
34}
35
36impl std::error::Error for TypeError {}
37
38impl Display for TypeError {
39    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
40        let Self { expect, actual, print } = self;
41        write!(f, "expected {expect:?}, found {actual:?}: {print}")
42    }
43}
44
45impl TypeError {
46    /// Get a type error from an object.
47    pub(crate) fn new<'ob, T>(expect: Type, obj: T) -> Self
48    where
49        T: Into<super::object::ObjectType<'ob>>,
50    {
51        let obj = obj.into();
52        Self { expect, actual: obj.get_type(), print: obj.to_string() }
53    }
54}