1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// file: src/lib.rs
// authors: Brandon H. Gomes

//! An Expression Library

#![feature(generic_associated_types)]
#![allow(incomplete_features)]

pub use exprz_core::*;

/// Vector Expressions
pub mod vec {
    use {
        super::{
            iter::{IntoIteratorGen, IteratorGen},
            ExprRef, Expression,
        },
        core::{iter::FromIterator, slice},
        std::vec,
    };

    /// Vector Expression Type over `String`s
    pub type ExprString = Expr<String>;

    /// Vector Expression Type
    #[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
    pub enum Expr<A> {
        /// Atomic expression
        Atom(A),

        /// Grouped expression
        Group(Vec<Self>),
    }

    impl<A> Expression for Expr<A> {
        type Atom = A;

        type Group = ExprGroup<A>;

        fn cases(&self) -> ExprRef<Self> {
            match self {
                Self::Atom(atom) => ExprRef::Atom(atom),
                Self::Group(group) => ExprRef::Group(group),
            }
        }

        fn from_atom(atom: <Self as Expression>::Atom) -> Self {
            Self::Atom(atom)
        }

        fn from_group(group: <Self as Expression>::Group) -> Self {
            Self::Group(group.group)
        }
    }

    impl<A> Default for Expr<A> {
        #[inline]
        fn default() -> Self {
            <Self as Expression>::default()
        }
    }

    /// Vector Expression Group Wrapper Type
    pub struct ExprGroup<A> {
        /// Inner group
        pub group: Vec<Expr<A>>,
    }

    impl<A> IntoIterator for ExprGroup<A> {
        type Item = Expr<A>;

        type IntoIter = vec::IntoIter<Expr<A>>;

        fn into_iter(self) -> Self::IntoIter {
            self.group.into_iter()
        }
    }

    impl<A> FromIterator<Expr<A>> for ExprGroup<A> {
        fn from_iter<I>(iter: I) -> Self
        where
            I: IntoIterator<Item = Expr<A>>,
        {
            ExprGroup {
                group: iter.into_iter().collect(),
            }
        }
    }

    impl<A> IteratorGen<Expr<A>> for &Vec<Expr<A>> {
        type Item<'t>
        where
            A: 't,
        = &'t Expr<A>;

        type Iter<'t>
        where
            A: 't,
        = slice::Iter<'t, Expr<A>>;

        fn iter(&self) -> Self::Iter<'_> {
            (self[..]).iter()
        }
    }

    impl<A> IntoIteratorGen<Expr<A>> for ExprGroup<A> {
        type IterGen<'t>
        where
            A: 't,
        = &'t Vec<Expr<A>>;

        fn gen(&self) -> Self::IterGen<'_> {
            &self.group
        }
    }
}