kamo/types/
error.rs

1#![allow(clippy::module_name_repetitions)]
2
3use std::{error::Error, fmt};
4
5use super::{Type, ARRAY_MAX};
6
7/// An error that occurs during type checking.
8#[derive(Clone, PartialEq, Eq)]
9pub enum TypeError {
10    /// The size of a fixed array exceeds the maximum size. Takes the actual
11    /// size.
12    ArraySizeTooLarge(usize),
13    /// Array element type must either be a filled type or an option type.
14    ArrayElemNotFilled,
15    /// Nested option types are not supported.
16    NestedOption,
17    /// Option type must be a filled. It can than be either `nil` or the filled
18    /// type.
19    OptionNotFilled,
20    /// Parameter type must either be a filled type or an option type. Takes the
21    /// parameter index starting from 1.
22    ParamNotFilled(usize),
23    /// Variadic type must either be a filled type or an option type.
24    VariadicNotFilled,
25    /// Pair car type must either be a filled type or an option type.
26    PairCarNotFilled,
27    /// Pair cdr type must either be a filled type or an option type.
28    PairCdrNotFilled,
29    /// Union type must contain only specific types.
30    UnionNotSpecific(Type),
31    /// Union type must contain at least two members.
32    UnionTooFewMembers,
33}
34
35impl Error for TypeError {}
36
37impl fmt::Display for TypeError {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self {
40            Self::ArraySizeTooLarge(size) => {
41                write!(f, "array size exceeds maximum of {ARRAY_MAX}: {size}")
42            }
43            Self::ArrayElemNotFilled => write!(f, "array element type is not filled or an option"),
44            Self::NestedOption => write!(f, "nested option types are not supported"),
45            Self::OptionNotFilled => write!(f, "option type is not filled"),
46            Self::ParamNotFilled(idx) => {
47                write!(f, "parameter {idx} type is not filled or an option")
48            }
49            Self::VariadicNotFilled => write!(f, "variadic type is not filled or an option"),
50            Self::PairCarNotFilled => write!(f, "pair car type is not filled or an option"),
51            Self::PairCdrNotFilled => write!(f, "pair cdr type is not filled or an option"),
52            Self::UnionNotSpecific(ty) => {
53                write!(f, "union type must contain only specific types, got: {ty}")
54            }
55            Self::UnionTooFewMembers => write!(f, "union type must contain at least two members"),
56        }
57    }
58}
59
60impl fmt::Debug for TypeError {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        fmt::Display::fmt(self, f)
63    }
64}