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
use super::clap::ArgMatches;
use profiler::Profiler;
use parse::cachegrind::Metric;
use err::ProfError;
use std::path::Path;

/// match the profiler argument
pub fn get_profiler<'a>(matches: &'a ArgMatches)
                        -> Result<(&'a ArgMatches<'a>, Profiler), ProfError> {
    match matches.subcommand_matches("profiler") {
        Some(matches) => {
            match matches.subcommand_matches("callgrind") {
                Some(matches) => Ok((matches, Profiler::new_callgrind())),
                None => {
                    match matches.subcommand_matches("cachegrind") {
                        Some(matches) => Ok((matches, Profiler::new_cachegrind())),
                        None => Err(ProfError::InvalidProfiler),
                    }
                }
            }
        }
        None => Err(ProfError::InvalidProfiler),
    }
}

/// match the binary argument
pub fn get_binary<'a>(matches: &'a ArgMatches) -> Result<&'a str, ProfError> {
    // read binary argument, make sure it exists in the filesystem
    match matches.value_of("binary") {
        Some(z) => {
            if !Path::new(z).exists() {
                return Err(ProfError::InvalidBinary);
            }
            return Ok(z);
        }
        None => Err(ProfError::InvalidBinary),
    }


}

/// parse the number argument into a usize
pub fn get_num(matches: &ArgMatches) -> Result<usize, ProfError> {

    match matches.value_of("n").map(|x| x.parse::<usize>()) {
        Some(Ok(z)) => Ok(z),
        Some(Err(_)) => Err(ProfError::InvalidNum),
        None => Ok(10000), // some arbitrarily large number...
    }

}

/// get the cachegrind metric user wants to sort on
pub fn get_sort_metric(matches: &ArgMatches) -> Result<Metric, ProfError> {
    match matches.value_of("sort") {
        Some("ir") => Ok(Metric::Ir),
        Some("i1mr") => Ok(Metric::I1mr),
        Some("ilmr") => Ok(Metric::ILmr),
        Some("dr") => Ok(Metric::Dr),
        Some("d1mr") => Ok(Metric::D1mr),
        Some("dlmr") => Ok(Metric::DLmr),
        Some("dw") => Ok(Metric::Dw),
        Some("d1mw") => Ok(Metric::D1mw),
        Some("dlmw") => Ok(Metric::DLmw),
        None => Ok(Metric::NAN),
        _ => Err(ProfError::InvalidSortMetric),
    }
}

#[cfg(test)]
mod test {
    #[test]
    fn test_get_profiler() {
        assert_eq!(1, 1);
    }

    #[test]
    fn test_get_binary() {
        assert_eq!(1, 1);
        assert_eq!(1, 1);
    }

    #[test]
    fn test_get_num() {
        assert_eq!(1, 1);
    }

    #[test]
    fn test_get_sort_metric() {
        assert_eq!(1, 1);
    }
}