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
use indexmap::IndexMap;
use self::Input::{SetValue, AtomicUpdater};
use teo_teon::value::Value;


pub enum Input {
    SetValue(Value),
    AtomicUpdater(Value),
}

impl Input {

    pub fn decode_field(updator: &Value) -> Input {
        if let Some(updator_map) = updator.as_dictionary() {
            let key = updator_map.keys().next().unwrap();
            let value = updator_map.values().next().unwrap();
            if key.as_str() == "set" {
                SetValue(value.clone())
            } else {
                AtomicUpdater(updator.clone())
            }
        } else {
            SetValue(updator.clone())
        }
    }

    pub fn key_value(value: &IndexMap<String, Value>) -> (&str, &Value) {
        (value.keys().next().unwrap().as_str(), value.values().next().unwrap())
    }

    pub fn has_i_mode(map: &IndexMap<String, Value>) -> bool {
        match map.get("mode") {
            Some(val) => {
                if let Some(str) = val.as_str() {
                    return str == "caseInsensitive"
                } else {
                    false
                }
            }
            None => {
                false
            }
        }
    }

    pub fn has_negative_take(json_value: &Value) -> bool {
        if json_value.is_dictionary() {
            let take = json_value.as_dictionary().unwrap().get("take");
            if take.is_some() {
                let take = take.unwrap();
                if take.is_any_int() {
                    let take = take.to_int64().unwrap();
                    return take < 0;
                }
            }
        }
        false
    }
}