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
pub mod parse;
pub mod eval;
pub mod source;
pub mod var;

use var::Var;

#[derive(Debug,PartialEq)]
pub enum Expect {
    All,
    Any,
    None,
    
    Ref(String) // references env variable set from logic
}
impl Expect {
    pub fn parse(s: String) -> Expect {
        match &s[..] {
            "all" => Expect::All,
            "any" => Expect::Any,
            "none" => Expect::None,
            _ => Expect::Ref(s),
        }
    }
}


/// delimited by new line
/// should resolve to boolean
#[derive(Debug,PartialEq)]
pub enum Logic {
    GT(Var,Var), // weight > 1
    LT(Var,Var),

    //boolean checks
    Is(String),
    IsNot(String),
}

impl Logic {
    pub fn parse(mut exp: Vec<String>) -> Logic {
        let len = exp.len();
        
        if len == 1 {
            let mut exp = exp.pop().unwrap();
            let inv = exp.remove(0);
            if inv == '!' {
                Logic::IsNot(exp)
            }
            else {
                exp.insert(0,inv);
                Logic::Is(exp)
            }
        }
        else if len == 3 {
            let var = exp.pop().unwrap();
            let var = Var::parse(var);

            let sym = exp.pop().unwrap();
            let key = exp.pop().unwrap();
            let key = Var::parse(key);
            
            if sym == ">" {
                Logic::GT(key,var)
            }
            else if sym == "<" {
                Logic::LT(key,var)
            }
            else { panic!("ERROR: Invalid Logic Syntax") }
        }
        else { panic!("ERROR: Unbalanced Logic Syntax ({:?})",exp) }
    }
}