mithril_client_cli/
configuration.rs

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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use serde::Deserialize;
use std::collections::HashMap;
use thiserror::Error;

/// Configuration error
#[derive(Debug, Error)]
pub enum ConfigError {
    /// Error raised when a required parameter is not present.
    #[error("Parameter '{0}' is mandatory.")]
    Required(String),

    /// Error raised when a parameter cannot be converted to string.
    #[error("Parameter '{0}' cannot be converted to string.")]
    Conversion(String),
}

/// Configuration parameters holder
#[derive(Debug, Default, PartialEq, Deserialize)]
#[serde(default)]
pub struct ConfigParameters {
    parameters: HashMap<String, String>,
}

impl ConfigParameters {
    /// Constructor
    pub fn new(parameters: HashMap<String, String>) -> Self {
        Self { parameters }
    }

    /// Useful constructor for testing
    #[cfg(test)]
    pub fn build(parameters: &[(&str, &str)]) -> Self {
        let parameters = parameters
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect();

        Self::new(parameters)
    }

    /// Add or replace a parameter in the holder
    #[cfg(test)]
    pub fn add_parameter(&mut self, name: &str, value: &str) -> &mut Self {
        let _ = self.parameters.insert(name.to_string(), value.to_string());

        self
    }

    /// Fill the holder with parameters from a source
    pub fn add_source(mut self, source: &impl ConfigSource) -> Result<Self, ConfigError> {
        let extra = source.collect()?;
        self.parameters.extend(extra);

        Ok(self)
    }

    /// Fetch a parameter from the holder.
    pub fn get(&self, name: &str) -> Option<String> {
        self.parameters.get(name).cloned()
    }

    /// Fetch a parameter from the holder. If the parameter is not set, the
    /// given default value is returned instead.
    pub fn get_or(&self, name: &str, default: &str) -> String {
        self.get(name).unwrap_or(default.to_string())
    }

    /// Fetch a parameter from the holder. If the parameter is not set, an error
    /// is raised.
    pub fn require(&self, name: &str) -> Result<String, ConfigError> {
        self.get(name)
            .ok_or_else(|| ConfigError::Required(name.to_string()))
    }
}

/// Describes a generic source of configuration parameters
pub trait ConfigSource {
    /// Collect all the configuration parameters from the source
    fn collect(&self) -> Result<HashMap<String, String>, ConfigError>;
}

#[cfg(test)]
mod tests {
    use super::*;

    struct TestSource {
        params: HashMap<String, String>,
    }

    impl<const N: usize> From<[(&str, &str); N]> for TestSource {
        fn from(arr: [(&str, &str); N]) -> Self {
            TestSource {
                params: arr
                    .into_iter()
                    .map(|(k, v)| (k.to_string(), v.to_string()))
                    .collect(),
            }
        }
    }

    impl ConfigSource for TestSource {
        fn collect(&self) -> Result<HashMap<String, String>, ConfigError> {
            Ok(self.params.clone())
        }
    }

    #[test]
    fn test_config_constructor() {
        let config = ConfigParameters::build(&[("pika", "chu")]);

        assert_eq!(
            ConfigParameters {
                parameters: [("pika".to_string(), "chu".to_string())]
                    .into_iter()
                    .collect()
            },
            config
        );
    }
    #[test]
    fn test_config_set() {
        let mut config = ConfigParameters::default();
        config.add_parameter("pika", "chu");

        assert_eq!(
            ConfigParameters {
                parameters: [("pika".to_string(), "chu".to_string())]
                    .into_iter()
                    .collect()
            },
            config
        );
    }

    #[test]
    fn test_config_get() {
        let mut config = ConfigParameters::default();
        config.add_parameter("pika", "chu");

        assert_eq!("chu".to_string(), config.get("pika").unwrap());
        assert!(config.get("whatever").is_none());
    }

    #[test]
    fn test_config_default() {
        let mut config = ConfigParameters::default();
        config.add_parameter("pika", "chu");

        assert_eq!("chu".to_string(), config.get("pika").unwrap());
        assert_eq!("default".to_string(), config.get_or("whatever", "default"));
    }

    #[test]
    fn test_config_require() {
        let mut config = ConfigParameters::default();
        config.add_parameter("pika", "chu");

        assert_eq!("chu".to_string(), config.require("pika").unwrap());
        config.require("whatever").unwrap_err();
    }

    #[test]
    fn test_add_source_to_config() {
        let config = ConfigParameters::build(&[("pika", "chu"), ("chari", "zard")])
            .add_source(&TestSource::from([("jiggly", "puff")]))
            .unwrap();

        assert_eq!(
            ConfigParameters {
                parameters: HashMap::from([
                    ("pika".to_string(), "chu".to_string()),
                    ("chari".to_string(), "zard".to_string()),
                    ("jiggly".to_string(), "puff".to_string())
                ])
            },
            config
        );
    }

    #[test]
    fn test_add_source_replace_existing_value() {
        let config = ConfigParameters::build(&[("pika", "pika")])
            .add_source(&TestSource::from([("pika", "not chu")]))
            .unwrap();

        assert_eq!(
            ConfigParameters {
                parameters: HashMap::from([("pika".to_string(), "not chu".to_string()),])
            },
            config
        );
    }
}