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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use std::ops::{BitAnd, BitOr, Not};

/// Trait for the `check_that` function, that allows it to run a condition on a struct.
pub trait Checkable {
    /// Checks whether the object satisfies the condition passed as `c`.
    /// 
    /// Note that the `condition` can be chained using `.and` (&) and `.or` (|).
    fn check_that(&self, condition: Condition) -> bool;
}

/// Different possible conditions.
/// 
/// Many conditions are self explanatory, such as `IsInt` and `IsList`.
/// Complex conditions can be created easily using the `|` and `&` operators.
/// So, for example, if you want to check whether an enum is an integer, *or* a float,
/// you can do the following:
/// 
/// ```
/// # use cfgmap::{CfgValue::*, Condition::*, Checkable};
/// # let value = Int(5);
/// value.check_that(IsInt | IsFloat);
/// ```
/// 
/// If you'd rather use methods, the following is equivalent:
/// ```
/// # use cfgmap::{CfgValue::*, Condition::*, Checkable};
/// # let value = Int(5);
/// value.check_that(IsInt.or(IsFloat));
/// ```
/// 
/// Both of the above examples expand to the following:
/// ```
/// # use cfgmap::{CfgValue::*, Condition::*, Checkable};
/// # let value = Int(5);
/// value.check_that(Or(Box::new(IsInt), Box::new(IsFloat)));
/// ```
/// 
/// If you'd like to not only check the type of a `CfgValue`, but also the *value* its wrapped in,
/// you can use the `Exactly` conditions:
/// ```
/// # use cfgmap::{CfgValue::*, Condition::*, Checkable};
/// let value = Int(5);
/// assert!(value.check_that(IsExactlyInt(5)));
/// ```
/// 
/// These exist for all `CfgValue`s. There also exist other miscellaneous conditions, such as
/// `IsListWithLength(usize)` or `IsListWith(Box<Condition>)`, which serve other purposes.
#[derive(Clone)]
pub enum Condition {
    IsInt,
    IsFloat,
    IsStr,
    IsList,
    IsBool,

    IsMap,
    /// A combination of two conditions.
    /// 
    /// If both evaluate to `TRUE`, the result is `TRUE`, otherwise it is `FALSE`.
    And(Box<Condition>, Box<Condition>),

    /// A combination of two conditions.
    /// 
    /// If one evaluates to `TRUE`, the result is `TRUE`, otherwise it is `FALSE`.
    Or(Box<Condition>, Box<Condition>),

    /// Represents a negation.
    Not(Box<Condition>),

    /// Does an exact comparison with an integer.
    IsExactlyInt(super::_Int),

    /// Does an exact comparison with an float.
    IsExactlyFloat(super::_Float),

    /// Does an exact comparison with a string.
    IsExactlyStr(super::_Str),

    /// Does an exact comparison with a `Vec<CfgValue>`.
    IsExactlyList(Vec<super::CfgValue>),

    /// Does an exact comparison with a `CfgMap`
    IsExactlyMap(super::CfgMap),

    /// Verifies it to be a `Bool`, and checks whether it is true.
    IsTrue,

    /// Verifies it to be a `List` and applies the condition to each of its elements.
    IsListWith(Box<Condition>),

    /// Verifies it to be a `List`, while also having a specific length.
    IsListWithLength(usize),

    #[cfg(feature = "from_json")]
    /// Verifies the value to be `null`. Only availiable while using `from_json`.
    IsNull,

    #[cfg(feature = "from_toml")]
    /// Verifies the value to be a `Datetime`. Only available while using `from_toml`.
    IsDatetime,

    /// A result condition. When executed this will always return `true`.
    TRUE,

    /// A result condition. When executed this will always return `false`.
    FALSE
}

impl Condition {

    /// Helper function to generate an `AND` condition.
    pub fn and(self, other: Condition) -> Condition {
        Condition::And(Box::new(self), Box::new(other))
    }

    /// Helper function to generate an `OR` condition.
    pub fn or(self, other: Condition) -> Condition {
        Condition::Or(Box::new(self), Box::new(other))
    }

    /// Helper function to generate a `NOT` condition.
    pub fn not(self) -> Condition {
        Condition::Not(Box::new(self))
    }

    /// Executes the condition. For all conditions, this function
    /// will return one of the result conditions - `TRUE` or `FALSE`.
    /// All conditions are executed on the input that is passed - including 
    /// conditions within `AND` and `OR` combinations.
    /// 
    /// ## Examples
    /// 
    /// ```
    /// use cfgmap::{Condition::*, CfgValue::*};
    /// assert!(IsInt.execute(&Int(5)).to_bool()); 
    /// assert!(!IsInt.execute(&Float(1.0)).to_bool());
    /// assert!((IsInt | IsFloat).execute(&Float(1.0)).to_bool());
    /// ```
    pub fn execute(&self, input: &super::CfgValue) -> Condition {
        use Condition::*;

        match self {
            // Basic conditions.
            IsInt => input.is_int().into(),
            IsFloat => input.is_float().into(),
            IsStr => input.is_str().into(),
            IsList => input.is_list().into(),
            IsMap => input.is_map().into(),
            IsBool => input.is_bool().into(),
            TRUE => TRUE,
            FALSE => FALSE,

            // Combined conditions.
            And(x,y) => {
                let res1 = x.execute(input);
                let res2 = y.execute(input);

                if res1.to_bool() && res2.to_bool() {
                    TRUE
                } else {FALSE}
            },
            Or(x,y) => {
                let res1 = x.execute(input);
                let res2 = y.execute(input);

                if res1.to_bool() || res2.to_bool() {
                    TRUE
                } else {FALSE}
            },
            Not(x) => {
                let res = x.execute(input);

                if res.to_bool() { FALSE } else { TRUE }
            },

            // Exact condition.
            IsExactlyInt(s) => input.as_int().map_or(false, |i| *i == *s).into(),
            IsExactlyFloat(s) => input.as_float().map_or(false, |f| *f == *s).into(),
            IsExactlyStr(s) => input.as_str().map_or(false, |st| *st == *s).into(),
            IsExactlyList(s) => input.as_list().map_or(false, |l| *l == *s).into(),
            IsExactlyMap(s) => input.as_map().map_or(false, |l| *l == *s).into(),
            IsTrue => input.as_bool().map_or(false, |b| *b).into(),

            // Miscellaneous.
            IsListWith(s) => {
                input.as_list().map(|list| {
                    for elem in list.iter() {
                        if !elem.check_that((**s).clone()) {
                            return FALSE;
                        }
                    }
                    TRUE
                }).map_or(FALSE, |o| o.into())
            },

            IsListWithLength(l) => input.as_list().map_or(false, |li| *l == li.len()).into(),

            // Feature-dependent.

            #[cfg(feature = "from_json")]
            IsNull => input.is_null().into(),

            #[cfg(feature = "from_toml")]
            IsDatetime => input.is_datetime().into(),
        }
    }

    /// Converts a bool into one of the result conditions.
    fn from_bool(b: bool) -> Condition {
        if b {Condition::TRUE} else {Condition::FALSE}
    }

    /// Converts from result condition to boolean.
    /// 
    /// All non-TRUE values are interpreted as FALSE.
    /// Reasoning behind this is that all other values are either incomplete conditions,
    /// or FALSE.
    pub fn to_bool(&self) -> bool {
        if let Condition::TRUE = self { true } else { false }
    }
}

/// Syntactical sugar for `a.and(b)`.
impl BitAnd for Condition {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        self.and(rhs)
    }
}

/// Syntactical sugar for `a.or(b)`.
impl BitOr for Condition {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        self.or(rhs)
    }
}

/// Syntactical sugar for `a.not()`
impl Not for Condition {
    type Output = Self;

    fn not(self) -> Self::Output {
        self.not()
    }
}

impl From<bool> for Condition {
    fn from(b: bool) -> Self {
        Condition::from_bool(b)
    }
}

impl From<Condition> for bool {
    fn from(c: Condition) -> Self {
        c.to_bool()
    }
}

#[cfg(test)]
mod test {
    use crate::{CfgMap, CfgValue::*, Condition::*, Checkable};

    #[test]
    fn basic_and_exact() {
        let i = Int(5);
        let f = Float(2.0);
        let s = Str(String::from("hello"));
        let b = Bool(true);
        let l = List(vec![Int(2), Float(8.0)]);
        let m = Map(CfgMap::new());

        // Verifies int
        assert!(i.check_that(IsInt));
        assert!(i.check_that(IsExactlyInt(5)));
        assert!(!i.check_that(IsExactlyInt(6)));

        // Verifies float
        assert!(f.check_that(IsFloat));
        assert!(f.check_that(IsExactlyFloat(2.0)));
        assert!(!f.check_that(IsExactlyFloat(3.0)));

        // Verifies string
        assert!(s.check_that(IsStr));
        assert!(s.check_that(IsExactlyStr(String::from("hello"))));
        assert!(!s.check_that(IsExactlyStr(String::from("hella"))));

        // Verifies bool
        assert!(b.check_that(IsBool));
        assert!(b.check_that(IsTrue));

        // Verifies list
        assert!(l.check_that(IsList));
        assert!(l.check_that(IsExactlyList(vec![Int(2), Float(8.0)])));
        assert!(!l.check_that(IsExactlyList(vec![Int(2), Float(8.1)])));

        // Verifies map
        assert!(m.check_that(IsMap));
        assert!(m.check_that(IsExactlyMap(CfgMap::new())));
        let mut map = CfgMap::new();
        map.default = "default".into();
        assert!(!m.check_that(IsExactlyMap(map)));
    }

    #[test]
    fn combinations() {
        vec![Int(5), Float(9.0), Str(String::from("foobar"))]
            .iter()
            .for_each(|e| assert!(e.check_that(IsInt | IsFloat | IsStr)));

        vec![Int(5), Float(9.0), Str(String::from("foobar"))]
            .iter()
            .for_each(|e| assert!(!e.check_that(IsList | IsMap)));

        vec![Int(5), Float(9.0), Str(String::from("foobar"))]
            .iter()
            .for_each(|e| assert!(!e.check_that(IsInt & IsFloat)));
    }

    #[test]
    fn misc() {
        let listexample = List(vec![Int(5), Float(9.0)]);

        assert!(listexample.check_that(IsListWith(Box::new(IsInt | IsFloat))));
        assert!(listexample.check_that(IsListWithLength(2)));
        assert!(!listexample.check_that(IsListWithLength(3)));
    }

}