summaryrefslogtreecommitdiffstats
path: root/src/value.rs
blob: bb88c3425c7eecf1ae828cafebb78bf8fb82e244 (plain)
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
use std::convert::From;
use std::collections::HashMap;
use std::borrow::Cow;

/// A configuration value.
///
/// Has an underlying or native type that comes from the configuration source
/// but will be coerced into the requested type.
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
    Nil,
    String(String),
    Integer(i64),
    Float(f64),
    Boolean(bool),
    Table(HashMap<String, Value>),
    Array(Vec<Value>),
}

impl Value {
    /// Gets the underlying value as a string, performing a conversion only if neccessary.
    #[allow(needless_lifetimes)]
    pub fn as_str<'a>(&'a self) -> Option<Cow<'a, str>> {
        match *self {
            Value::String(ref value) => Some(Cow::Borrowed(value)),
            Value::Integer(value) => Some(Cow::Owned(value.to_string())),
            Value::Float(value) => Some(Cow::Owned(value.to_string())),
            Value::Boolean(value) => Some(Cow::Owned(value.to_string())),

            _ => None,
        }
    }

    /// Gets the underlying type as a boolean, performing a conversion only if neccessary.
    pub fn as_bool(&self) -> Option<bool> {
        match *self {
            Value::Boolean(value) => Some(value),
            Value::Integer(value) => Some(value != 0),
            Value::Float(value) => Some(value != 0.0),

            Value::String(ref value) => {
                match value.to_lowercase().as_ref() {
                    "1" | "true" | "on" | "yes" => Some(true),
                    "0" | "false" | "off" | "no" => Some(false),
                    _ => None,
                }
            }

            _ => None,
        }
    }

    /// Gets the underlying type as an integer, performing a conversion only if neccessary.
    pub fn as_int(&self) -> Option<i64> {
        match *self {
            Value::Integer(value) => Some(value),
            Value::String(ref value) => value.parse().ok(),
            Value::Boolean(value) => Some(if value { 1 } else { 0 }),
            Value::Float(value) => Some(value.round() as i64),

            _ => None,
        }
    }

    /// Gets the underlying type as a floating-point, performing a conversion only if neccessary.
    pub fn as_float(&self) -> Option<f64> {
        match *self {
            Value::Float(value) => Some(value),
            Value::String(ref value) => value.parse().ok(),
            Value::Integer(value) => Some(value as f64),
            Value::Boolean(value) => Some(if value { 1.0 } else { 0.0 }),

            _ => None,
        }
    }

    /// Gets the underlying type as a map; only works if the type is actually a map.
    pub fn as_map(&self) -> Option<&HashMap<String, Value>> {
        match *self {
            Value::Table(ref value) => Some(value),
            _ => None,
        }
    }
    /// Gets the underlying type as a slice; only works if the type is actually a slice.
    pub fn as_slice(&self) -> Option<&[Value]> {
        match *self {
            Value::Array(ref value) => Some(value),
            _ => None,
        }
    }
}

// Generalized construction from type into variant is needed
// for setting configuration values

impl From<String> for Value {
    fn from(value: String) -> Value {
        Value::String(value.into())
    }
}

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

impl From<i64> for Value {
    fn from(value: i64) -> Value {
        Value::Integer(value)
    }
}

impl From<f64> for Value {
    fn from(value: f64) -> Value {
        Value::Float(value)
    }
}

impl From<bool> for Value {
    fn from(value: bool) -> Value {
        Value::Boolean(value)
    }
}

// impl From<HashMap<String, Value>> for Value {
//     fn from(value: HashMap<String, Value>) -> Value {
//         Value::Table(value)
//     }
// }

impl<T> From<HashMap<String, T>> for Value
    where T: Into<Value>
{
    fn from(values: HashMap<String, T>) -> Value {
        let mut r = HashMap::new();

        for (k, v) in values {
            r.insert(k.clone(), v.into());
        }

        Value::Table(r)
    }
}

impl<T> From<Vec<T>> for Value
    where T: Into<Value>
{
    fn from(values: Vec<T>) -> Value {
        let mut l = Vec::new();

        for v in values {
            l.push(v.into());
        }

        Value::Array(l)
    }
}