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
//! Provides an extension of the `HashMap` type called `ExtendedMap`.

use crate::config::tree::ConfigNode;

use std::collections::HashMap;
use std::str::FromStr;

/// An extension of the standard HashMap that provides some syntactic sugar for working with
///   optional and compulsory items, as well as errors.
pub trait ExtendedMap<K, V> {
    /// Gets an item from the map by value.
    fn get_owned(&self, key: K) -> Option<V>;

    /// Gets an item from the map or returns a given default.
    fn get_optional(&self, key: K, default: V) -> V;

    /// Gets an item from the map or returns the given error.
    fn get_compulsory(&self, key: K, error: &'static str) -> Result<V, &'static str>;

    /// Gets an item from the map or a given default and parses it, or returns the given error.
    fn get_optional_parsed<T>(
        &self,
        key: K,
        default: T,
        error: &'static str,
    ) -> Result<T, &'static str>
    where
        T: FromStr;

    /// Gets an item from the map and parses it, or returns the given error.
    fn get_compulsory_parsed<T>(&self, key: K, error: &'static str) -> Result<T, &'static str>
    where
        T: FromStr;
}

impl ExtendedMap<&'static str, String> for HashMap<String, String> {
    fn get_owned(&self, key: &str) -> Option<String> {
        self.get(key).map(|s| s.to_string())
    }

    fn get_optional(&self, key: &str, default: String) -> String {
        self.get(key).unwrap_or(&default).to_string()
    }

    fn get_compulsory(&self, key: &str, error: &'static str) -> Result<String, &'static str> {
        self.get(key).map_or(Err(error), |s| Ok(s.clone()))
    }

    fn get_optional_parsed<T>(
        &self,
        key: &str,
        default: T,
        error: &'static str,
    ) -> Result<T, &'static str>
    where
        T: FromStr,
    {
        self.get(key)
            .map_or(Ok(default), |s| s.parse::<T>().map_err(|_| error))
    }

    fn get_compulsory_parsed<T>(&self, key: &str, error: &'static str) -> Result<T, &'static str>
    where
        T: FromStr,
    {
        self.get(key)
            .map_or(Err(error), |s| s.parse::<T>().map_err(|_| error))
    }
}

impl ExtendedMap<&'static str, String> for HashMap<String, ConfigNode> {
    fn get_owned(&self, key: &'static str) -> Option<String> {
        self.get(key).and_then(|n| n.get_string())
    }

    fn get_optional(&self, key: &'static str, default: String) -> String {
        self.get(key)
            .and_then(|n| n.get_string())
            .unwrap_or(default)
    }

    fn get_compulsory(
        &self,
        key: &'static str,
        error: &'static str,
    ) -> Result<String, &'static str> {
        self.get(key)
            .and_then(|n| n.get_string())
            .map_or(Err(error), Ok)
    }

    fn get_optional_parsed<T>(
        &self,
        key: &'static str,
        default: T,
        error: &'static str,
    ) -> Result<T, &'static str>
    where
        T: FromStr,
    {
        self.get(key)
            .map(|n| match n {
                ConfigNode::String(_, s) => s.parse().map_err(|_| ()),
                ConfigNode::Boolean(_, b) => b.parse().map_err(|_| ()),
                ConfigNode::Number(_, n) => n.parse().map_err(|_| ()),
                ConfigNode::Section(_, _) => Err(()),
                ConfigNode::Route(_, _) => Err(()),
                ConfigNode::Host(_, _) => Err(()),
            })
            .unwrap_or(Ok(default))
            .map_err(|_| error)
    }

    fn get_compulsory_parsed<T>(
        &self,
        key: &'static str,
        error: &'static str,
    ) -> Result<T, &'static str>
    where
        T: FromStr,
    {
        self.get(key)
            .map(|n| match n {
                ConfigNode::String(_, s) => s.parse().map_err(|_| ()),
                ConfigNode::Boolean(_, b) => b.parse().map_err(|_| ()),
                ConfigNode::Number(_, n) => n.parse().map_err(|_| ()),
                ConfigNode::Section(_, _) => Err(()),
                ConfigNode::Route(_, _) => Err(()),
                ConfigNode::Host(_, _) => Err(()),
            })
            .unwrap_or(Err(()))
            .map_err(|_| error)
    }
}