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
use std::collections::{HashMap, BTreeMap};
use std::hash::Hash;
use std::str::FromStr;

use config::Value;

pub fn parse_simple_toml_value(string: &str) -> Value {
    if let Ok(int) = i64::from_str(string) {
        return Value::Integer(int)
    }

    if let Ok(boolean) = bool::from_str(string) {
        return Value::Boolean(boolean)
    }

    if let Ok(float) = f64::from_str(string) {
        return Value::Float(float)
    }

    Value::String(string.to_string())
}

/// Conversion trait from standard types into TOML `Value`s.
pub trait IntoValue {
    /// Converts `self` into a TOML `Value`.
    fn into_value(self) -> Value;
}

impl<'a> IntoValue for &'a str {
    fn into_value(self) -> Value {
        Value::String(self.to_string())
    }
}

impl IntoValue for Value {
    fn into_value(self) -> Value {
        self
    }
}

impl<V: IntoValue> IntoValue for Vec<V> {
    fn into_value(self) -> Value {
        Value::Array(self.into_iter().map(|v| v.into_value()).collect())
    }
}

impl<S: Into<String>, V: IntoValue> IntoValue for BTreeMap<S, V> {
    fn into_value(self) -> Value {
        let table = self.into_iter()
            .map(|(s, v)| (s.into(), v.into_value()))
            .collect();

        Value::Table(table)
    }
}

impl<S: Into<String> + Hash + Eq, V: IntoValue> IntoValue for HashMap<S, V> {
    fn into_value(self) -> Value {
        let table = self.into_iter()
            .map(|(s, v)| (s.into(), v.into_value()))
            .collect();

        Value::Table(table)
    }
}

macro_rules! impl_into_value {
    ($variant:ident : $t:ty) => ( impl_into_value!($variant: $t,); );

    ($variant:ident : $t:ty, $($extra:tt)*) => (
        impl IntoValue for $t {
            fn into_value(self) -> Value {
                Value::$variant(self $($extra)*)
            }
        }
    )
}

impl_into_value!(String: String);
impl_into_value!(Integer: i64);
impl_into_value!(Integer: isize, as i64);
impl_into_value!(Integer: i32, as i64);
impl_into_value!(Integer: i8, as i64);
impl_into_value!(Integer: u8, as i64);
impl_into_value!(Integer: u32, as i64);
impl_into_value!(Boolean: bool);
impl_into_value!(Float: f64);
impl_into_value!(Float: f32, as f64);