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
//! Config
//!
//! Config related code

use clap::ArgMatches;

/// Config stores parsed data from running `cloudsctl env`
#[derive(Debug, PartialEq)]
pub struct Config {
    pub cloud: String,
    pub command: Vec<String>,
}

impl Config {
    /// Returns a Config struct wrapped in a Result parsed from matched arguments
    ///
    /// # Arguments
    ///
    /// * `matches` - ArgMatches struct that holds the cloud name and the command.
    pub fn new(matches: ArgMatches) -> Result<Config, &'static str> {
        Ok(Config {
            cloud: matches.value_of("cloud").unwrap().to_string(),
            command: matches
                .values_of("command")
                .unwrap()
                .map(String::from)
                .collect(),
        })
    }
}

#[cfg(test)]
mod config {
    use super::*;
    use clap::{App, Arg};

    #[test]
    fn new() {
        let app = App::new("test")
            .arg(Arg::with_name("cloud").takes_value(true))
            .arg(Arg::with_name("command").takes_value(true));

        let args = vec!["myprog", "test_cloud", "test_command"];

        let actual = Config::new(app.get_matches_from(args)).unwrap();
        let expected = Config {
            cloud: String::from("test_cloud"),
            command: vec![String::from("test_command")],
        };
        assert_eq!(actual, expected);
    }
}