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
use confitul::{ClusterOptions, ConfigCheck, ConfigCheckError};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CloudOptions {
    pub cluster: ClusterOptions,
}

impl CloudOptions {
    pub fn new(cluster_options: Option<ClusterOptions>) -> CloudOptions {
        let real_cluster_options = cluster_options.unwrap_or(ClusterOptions::default());
        let mut cloud_options = Self::default();
        cloud_options.cluster = real_cluster_options;
        cloud_options
    }
}

impl ConfigCheck for CloudOptions {
    fn check(&self) -> Result<CloudOptions, ConfigCheckError> {
        self.cluster.check()?;
        Ok(self.clone())
    }
}

impl std::default::Default for CloudOptions {
    fn default() -> CloudOptions {
        CloudOptions {
            cluster: ClusterOptions::default(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::CloudOptions;
    use gethostname::gethostname;
    use serde_json;

    #[test]
    fn test_cloud_options_serde_json() {
        let options = CloudOptions::new(None);
        let serialized = serde_json::to_string(&options).unwrap();
        assert_eq!(
            format!(
                "{{\"cluster\":{{\"size\":10,\"local_host\":{{\"name\":\"{}\",\"description\":\"\",\"urls\":[]}}}}}}",
                gethostname().to_str().unwrap()
            ),
            serialized
        );
    }
}