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
use std::ops::Deref;

/// Selects a value at the given TOML path from a TOML value.
///
/// Works similarly to an xpath query to select a value inside a complex XML document. This function
/// is useful to select a value from a TOML document without deserializing it into specific type
/// which can sometimes be a complex endeavour.
///
/// Example:
/// ```
/// use libherokubuildpack::toml::toml_select_value;
/// use toml::toml;
///
/// let toml = toml! {
///     [config]
///     [config.net]
///     port = 12345
///     host = "localhost"
/// };
///
/// assert_eq!(
///     toml_select_value(vec!["config", "net", "port"], &toml.into()),
///     Some(&toml::Value::from(12345))
/// );
/// ```
pub fn toml_select_value<S: AsRef<str>, K: Deref<Target = [S]>>(
    keys: K,
    value: &toml::Value,
) -> Option<&toml::Value> {
    if keys.is_empty() {
        Some(value)
    } else {
        match &value {
            toml::Value::Table(table) => keys.split_first().and_then(|(head, tail)| {
                table
                    .get(head.as_ref())
                    .and_then(|next_value| toml_select_value(tail, next_value))
            }),
            _ => None,
        }
    }
}

#[cfg(test)]
mod test {
    use crate::toml::toml_select_value;
    use std::collections::HashMap;
    use toml::toml;

    #[test]
    fn test_common_case() {
        let toml = toml! {
            [bogus]
            value = "Will it trip it up?"
            [now]
            this = "is podracing!"
        };

        assert_eq!(
            toml_select_value(vec!["now", "this"], &toml.into()),
            Some(&toml::Value::from("is podracing!"))
        );
    }

    #[test]
    fn test_value_from_dotted_keys() {
        let toml = toml! {
            now.this.is = "podracing"
            [bogus]
            value = "Will it trip it up?"
        };

        assert_eq!(
            toml_select_value(vec!["now", "this", "is"], &toml.into()),
            Some(&toml::Value::from("podracing"))
        );
    }

    #[test]
    fn test_value_from_table() {
        let toml = toml! {
            [bogus]
            value = "Will it trip it up?"

            [now.this]
            is = "podracing"
        };

        assert_eq!(
            toml_select_value(vec!["now", "this", "is"], &toml.into()),
            Some(&toml::Value::from("podracing"))
        );
    }

    #[test]
    fn test_partial_match() {
        let toml = toml! {
            [bogus]
            value = "Will it trip it up?"

            [now.this]
            is = "podracing"
        };

        assert_eq!(
            toml_select_value(vec!["now", "this", "was"], &toml.into()),
            None
        );
    }

    #[test]
    fn test_does_not_modify_value_types() {
        let toml = toml! {
            [translations]
            leet = 1337
        };

        assert_eq!(
            toml_select_value(vec!["translations", "leet"], &toml.into()),
            Some(&toml::Value::from(1337))
        );
    }

    #[test]
    fn test_works_without_keys() {
        let toml = toml! {
            foo = "bar"
        };

        let mut hash_map = HashMap::new();
        hash_map.insert(String::from("foo"), String::from("bar"));

        assert_eq!(
            toml_select_value::<&str, Vec<&str>>(Vec::new(), &toml.into()),
            Some(&toml::Value::from(hash_map))
        );
    }
}