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
// List of traits needed for wrapping regular functions to FunctionContainer.

use crate::expr::*;
use serde_json::Value;

impl From<Operand> for bool {
    fn from(op: Operand) -> Self {
        if let Operand::Value(val) = op {
            if val.is_boolean() {
                return val.as_bool().unwrap();
            }
        }
        false
    }
}

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

impl From<Operand> for i64 {
    fn from(op: Operand) -> Self {
        if let Operand::Value(val) = op {
            if val.is_i64() {
                return val.as_i64().unwrap();
            }
        }
        0
    }
}

impl From<i64> for Operand {
    fn from(n: i64) -> Self {
        Operand::Value(Value::from(n))
    }
}

impl From<Operand> for f64 {
    fn from(op: Operand) -> Self {
        if let Operand::Value(val) = op {
            if val.is_f64() {
                return val.as_f64().unwrap();
            }
        }
        0.
    }
}

impl From<f64> for Operand {
    fn from(n: f64) -> Self {
        Operand::Value(Value::from(n))
    }
}

impl From<Operand> for String {
    fn from(op: Operand) -> Self {
        if let Operand::Value(val) = op {
            if val.is_string() {
                return val.as_str().unwrap().to_string();
            }
        }
        "".to_string()
    }
}

impl From<String> for Operand {
    fn from(s: String) -> Self {
        Operand::Value(Value::from(s))
    }
}

impl From<Operand> for Value {
    fn from(op: Operand) -> Self {
        if let Operand::Value(val) = op {
            val
        } else {
            Value::Bool(false)
        }
    }
}

impl From<Value> for Operand {
    fn from(val: Value) -> Self {
        Operand::Value(val)
    }
}