profiler/
argparse.rs

1use super::clap::ArgMatches;
2use profiler::Profiler;
3use parse::cachegrind::Metric;
4use err::ProfError;
5use std::path::Path;
6
7/// match the profiler argument
8pub fn get_profiler<'a>(matches: &'a ArgMatches)
9                        -> Result<(&'a ArgMatches<'a>, Profiler), ProfError> {
10    match matches.subcommand_matches("profiler") {
11        Some(matches) => {
12            match matches.subcommand_matches("callgrind") {
13                Some(matches) => Ok((matches, Profiler::new_callgrind())),
14                None => {
15                    match matches.subcommand_matches("cachegrind") {
16                        Some(matches) => Ok((matches, Profiler::new_cachegrind())),
17                        None => Err(ProfError::InvalidProfiler),
18                    }
19                }
20            }
21        }
22        None => Err(ProfError::InvalidProfiler),
23    }
24}
25
26/// match the binary argument
27pub fn get_binary<'a>(matches: &'a ArgMatches) -> Result<&'a str, ProfError> {
28    // read binary argument, make sure it exists in the filesystem
29    match matches.value_of("binary") {
30        Some(z) => {
31            if !Path::new(z).exists() {
32                return Err(ProfError::InvalidBinary);
33            }
34            return Ok(z);
35        }
36        None => Err(ProfError::InvalidBinary),
37    }
38
39
40}
41
42/// parse the number argument into a usize
43pub fn get_num(matches: &ArgMatches) -> Result<usize, ProfError> {
44
45    match matches.value_of("n").map(|x| x.parse::<usize>()) {
46        Some(Ok(z)) => Ok(z),
47        Some(Err(_)) => Err(ProfError::InvalidNum),
48        None => Ok(10000), // some arbitrarily large number...
49    }
50
51}
52
53/// get the cachegrind metric user wants to sort on
54pub fn get_sort_metric(matches: &ArgMatches) -> Result<Metric, ProfError> {
55    match matches.value_of("sort") {
56        Some("ir") => Ok(Metric::Ir),
57        Some("i1mr") => Ok(Metric::I1mr),
58        Some("ilmr") => Ok(Metric::ILmr),
59        Some("dr") => Ok(Metric::Dr),
60        Some("d1mr") => Ok(Metric::D1mr),
61        Some("dlmr") => Ok(Metric::DLmr),
62        Some("dw") => Ok(Metric::Dw),
63        Some("d1mw") => Ok(Metric::D1mw),
64        Some("dlmw") => Ok(Metric::DLmw),
65        None => Ok(Metric::NAN),
66        _ => Err(ProfError::InvalidSortMetric),
67    }
68}
69
70#[cfg(test)]
71mod test {
72    #[test]
73    fn test_get_profiler() {
74        assert_eq!(1, 1);
75    }
76
77    #[test]
78    fn test_get_binary() {
79        assert_eq!(1, 1);
80        assert_eq!(1, 1);
81    }
82
83    #[test]
84    fn test_get_num() {
85        assert_eq!(1, 1);
86    }
87
88    #[test]
89    fn test_get_sort_metric() {
90        assert_eq!(1, 1);
91    }
92}