rain_lang/value/
error.rs

1/*!
2Rain graph errors and error handling
3*/
4
5use std::convert::Infallible;
6use smallvec::SmallVec;
7use crate::graph::region::WeakRegion;
8use super::ValId;
9
10/// A `rain` value error
11#[derive(Debug, Clone)]
12pub enum ValueError {
13    /// A non-function value was applied in an S-expression
14    NotAFunction(NotAFunction),
15    /// Incomparable regions
16    IncomparableRegions(IncomparableRegions),
17    /// A region has already been fused
18    RegionAlreadyFused(RegionAlreadyFused),
19    /// Not implemented
20    NotImplementedError
21}
22
23/// A `rain` region error
24#[derive(Debug, Clone)]
25pub enum RegionError {
26    /// Incomparable regions
27    IncomparableRegions(IncomparableRegions),
28    /// A region has already been fused
29    RegionAlreadyFused(RegionAlreadyFused)
30}
31
32impl From<RegionError> for ValueError {
33    fn from(err: RegionError) -> ValueError {
34        match err {
35            RegionError::IncomparableRegions(i) => ValueError::IncomparableRegions(i),
36            RegionError::RegionAlreadyFused(r) => ValueError::RegionAlreadyFused(r)
37        }
38    }
39}
40
41/// The size of a small set of incomparable regions
42const SMALL_INCOMPARABLE_REGIONS: usize = 2;
43
44/// Region already fused error
45#[derive(Debug, Clone)]
46pub struct RegionAlreadyFused;
47
48impl From<Infallible> for RegionAlreadyFused {
49    fn from(err: Infallible) -> RegionAlreadyFused { match err {} }
50}
51impl From<RegionAlreadyFused> for ValueError {
52    fn from(err: RegionAlreadyFused) -> ValueError { ValueError::RegionAlreadyFused(err) }
53}
54impl From<RegionAlreadyFused> for RegionError {
55    fn from(err: RegionAlreadyFused) -> RegionError { RegionError::RegionAlreadyFused(err) }
56}
57
58/// Incomparable region error
59#[derive(Debug, Clone)]
60pub struct IncomparableRegions(pub SmallVec<[WeakRegion; SMALL_INCOMPARABLE_REGIONS]>);
61
62impl From<Infallible> for IncomparableRegions {
63    fn from(err: Infallible) -> IncomparableRegions { match err {} }
64}
65
66/// A non-function value was applied in an S-expression
67#[derive(Debug, Clone)]
68pub struct NotAFunction {
69    /// The value applied
70    pub applied: Option<ValId>,
71    /// The argument
72    pub argument: Option<ValId>
73}
74
75impl From<Infallible> for ValueError { fn from(i: Infallible) -> ValueError { match i {} } }
76impl From<NotAFunction> for ValueError {
77    fn from(n: NotAFunction) -> ValueError { ValueError::NotAFunction(n) }
78}
79impl From<IncomparableRegions> for ValueError {
80    fn from(i: IncomparableRegions) -> ValueError { ValueError::IncomparableRegions(i) }
81}
82impl From<IncomparableRegions> for RegionError {
83    fn from(i: IncomparableRegions) -> RegionError { RegionError::IncomparableRegions(i) }
84}