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
use object::Object;

#[derive(Debug, PartialEq)]
pub enum Value {
    Str(String),
    Num(i64),
    Bool(bool),
    // Array(Vec<Box<Value>>),
    Object(Box<Object>)
}

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

impl<'a> From<&'a str> for Value {
    fn from(string: &'a str) -> Self {
        Value::Str(string.into())
    }
}

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

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

impl From<Object> for Value {
    fn from(obj: Object) -> Self {
        Value::Object(Box::new(obj))
    }
}

impl<'a> Into<Option<String>> for &'a Value {
    fn into(self) -> Option<String> {
        match self {
            &Value::Str(ref s) => Some(s.clone()),
            _ => None
        }
    }
}

impl<'a> Into<Option<i64>> for &'a Value {
    fn into(self) -> Option<i64> {
        match self {
            &Value::Num(n) => Some(n),
            _ => None
        }
    }
}

impl<'a> Into<Option<bool>> for &'a Value {
    fn into(self) -> Option<bool> {
        match self {
            &Value::Bool(b) => Some(b),
            _ => None
        }
    }
}