rain-lang 0.0.1

An implementation of an RVSDG in Rust with a concept of lifetimes
Documentation
/*!
Rain graph errors and error handling
*/

use std::convert::Infallible;
use smallvec::SmallVec;
use crate::graph::region::WeakRegion;
use super::ValId;

/// A `rain` value error
#[derive(Debug, Clone)]
pub enum ValueError {
    /// A non-function value was applied in an S-expression
    NotAFunction(NotAFunction),
    /// Incomparable regions
    IncomparableRegions(IncomparableRegions),
    /// A region has already been fused
    RegionAlreadyFused(RegionAlreadyFused),
    /// Not implemented
    NotImplementedError
}

/// A `rain` region error
#[derive(Debug, Clone)]
pub enum RegionError {
    /// Incomparable regions
    IncomparableRegions(IncomparableRegions),
    /// A region has already been fused
    RegionAlreadyFused(RegionAlreadyFused)
}

impl From<RegionError> for ValueError {
    fn from(err: RegionError) -> ValueError {
        match err {
            RegionError::IncomparableRegions(i) => ValueError::IncomparableRegions(i),
            RegionError::RegionAlreadyFused(r) => ValueError::RegionAlreadyFused(r)
        }
    }
}

/// The size of a small set of incomparable regions
const SMALL_INCOMPARABLE_REGIONS: usize = 2;

/// Region already fused error
#[derive(Debug, Clone)]
pub struct RegionAlreadyFused;

impl From<Infallible> for RegionAlreadyFused {
    fn from(err: Infallible) -> RegionAlreadyFused { match err {} }
}
impl From<RegionAlreadyFused> for ValueError {
    fn from(err: RegionAlreadyFused) -> ValueError { ValueError::RegionAlreadyFused(err) }
}
impl From<RegionAlreadyFused> for RegionError {
    fn from(err: RegionAlreadyFused) -> RegionError { RegionError::RegionAlreadyFused(err) }
}

/// Incomparable region error
#[derive(Debug, Clone)]
pub struct IncomparableRegions(pub SmallVec<[WeakRegion; SMALL_INCOMPARABLE_REGIONS]>);

impl From<Infallible> for IncomparableRegions {
    fn from(err: Infallible) -> IncomparableRegions { match err {} }
}

/// A non-function value was applied in an S-expression
#[derive(Debug, Clone)]
pub struct NotAFunction {
    /// The value applied
    pub applied: Option<ValId>,
    /// The argument
    pub argument: Option<ValId>
}

impl From<Infallible> for ValueError { fn from(i: Infallible) -> ValueError { match i {} } }
impl From<NotAFunction> for ValueError {
    fn from(n: NotAFunction) -> ValueError { ValueError::NotAFunction(n) }
}
impl From<IncomparableRegions> for ValueError {
    fn from(i: IncomparableRegions) -> ValueError { ValueError::IncomparableRegions(i) }
}
impl From<IncomparableRegions> for RegionError {
    fn from(i: IncomparableRegions) -> RegionError { RegionError::IncomparableRegions(i) }
}