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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use var::Var;
use parse::IR;
use eval::Evaluator;
use def::Def;

use std::collections::HashMap;

/// Expect Types for Composites
#[derive(Debug,PartialEq, Clone, Copy)]
pub enum Expect {
    All,
    Any,
    None,
}

impl Expect {
    pub fn parse(s: String) -> Expect {
        match &s[..] {
            "all" => Expect::All,
            "any" => Expect::Any,
            "none" => Expect::None,
            _ => Expect::None,
        }
    }
}


/// Logic Variants
///
/// These are each to be delimited by a new line
/// Always should resolve to boolean
#[derive(Debug,PartialEq)]
pub enum Logic {
    /// Greater Than, eg: weight > 1
    GT(Var,Var),
    /// Lesser Than
    LT(Var,Var),

    /// Boolean: True
    Is(String),
    /// Boolean: False
    IsNot(String),

    /// A composite logic type to group logic statements together
    Composite(Expect, Vec<String>),
}

pub type Logics = HashMap<String,LogicFn>;
pub struct LogicFn(Box<dyn Fn(&Def,&Logics) -> Option<bool> + Send>);
impl LogicFn {
    pub fn run(&self, def: &Def, logic: &Logics) -> Option<bool> {
        self.0(def, logic)
    }
}

// NOTE: we don't actually impl this, but satisfy checker
impl PartialEq for LogicFn {
    fn eq(&self, _other: &LogicFn) -> bool {
        false
    }
}

use std::fmt;
// NOTE: we don't actually impl this, but satisfy checker
impl fmt::Debug for LogicFn {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f,"")
    }
}

impl Logic {
    pub fn parse_comp(mut keys: Vec<&str>,
                      mut exp: Vec<IR>) -> Result<Logic,&'static str> {
        // NOTE: we may want to inspect what happened if the kind was not found
        let kind = Expect::parse(keys.pop().unwrap().to_owned());

        let exp = exp.drain(..).map(|n| n.into()).collect();
        Ok(Logic::Composite(kind,
                            exp))
    }
    
    pub fn parse(mut exp: Vec<IR>) -> Result<Logic,&'static str> {
        let len = exp.len();
        
        if len == 1 {
            let mut exp: String = exp.pop().unwrap().into();
            let inv = exp.remove(0);
            if inv == '!' {
                Ok(Logic::IsNot(exp))
            }
            else {
                exp.insert(0,inv);
                Ok(Logic::Is(exp))
            }
        }
        else if len == 3 {
            let var = exp.pop().unwrap();
            let var = Var::parse(var)?;

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

    /// Evaluate Logic into Functions
    pub fn eval (&self) -> LogicFn {
        match self {
            &Logic::Is(ref lookup) => {
                let lookup = lookup.clone();
                let lfn = Box::new(move |data: &Def, logic: &Logics| {
                    if let Some(r) = Evaluator::resolve(&lookup, logic, data) {
                        match r {
                            Var::Bool(v) => {
                                 Some(v)
                            },
                            _ => { //if exists?
                                Some(true)
                            },
                        }
                    }
                    else { Some(false) }
                });

                LogicFn(lfn)
            },
            &Logic::IsNot(ref lookup) => { //inverse state
                let lookup = lookup.clone();
                let lfn = Box::new(move |data: &Def, logic: &Logics| {
                    if let Some(r) = Evaluator::resolve(&lookup, logic, data) {
                        match r {
                            Var::Bool(v) => {
                                Some(!v)
                            },
                            _ => {
                                Some(false)
                            },
                        }
                    }
                    else {  Some(true) } // missing identity turns into true on inv bool
                });

                LogicFn(lfn)
            },

            &Logic::GT(ref left, ref right) => {
                let left = left.clone();
                let right = right.clone();
                let lfn = Box::new(move |data: &Def, _logic: &Logics| {
                    let right = Var::get_num(&right,data);
                    let left = Var::get_num(&left,data);
                
                    if left.is_ok() && right.is_ok() {
                        Some(left.unwrap() > right.unwrap())
                    }
                    else { None }
                });

                LogicFn(lfn)
            },
            &Logic::LT(ref left, ref right) => {
                let left = left.clone();
                let right = right.clone();
                let lfn = Box::new(move |data: &Def, _logic: &Logics| {
                    let right = Var::get_num(&right,data);
                    let left = Var::get_num(&left,data);
                    
                    if left.is_ok() && right.is_ok() {
                        Some(left.unwrap() < right.unwrap())
                    }
                    else { None }
                });

                LogicFn(lfn)
            },
            &Logic::Composite(x, ref lookups) => {
                let lookups = lookups.clone();
                let lfn = Box::new(move |data: &Def, logic: &Logics| {
                    // track if any lookups are false or true
                    let mut comp_true = false;
                    let mut comp_false = false;
                    
                    for lookup in lookups.iter() {
                        if let Some(val) = Evaluator::resolve(lookup, logic, data) {
                            match val {
                                Var::Bool(v) => {
                                    if v { comp_true = true; }
                                    else { comp_false = true; }
                                },
                                _ => { comp_true = lookup != &val.to_string(); }
                            }
                        }
                        else { comp_false = true }
                    }
                    
                    match x {
                        Expect::All => { // all must pass as true
                            Some(comp_true && !comp_false)
                        },
                        Expect::Any => { // first truth passes for set
                            Some(comp_true)
                        },
                        Expect::None => { // inverse of any, none must be true
                            Some(!comp_true && comp_false)
                        },
                    }
                });
                
                LogicFn(lfn)
            }
        }
                 
        
    }
}