gr/cli/
amps.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
use clap::Parser;

#[derive(Parser)]
pub struct AmpsCommand {
    #[clap(subcommand)]
    subcommand: Option<AmpsSubcommand>,
}

#[derive(Parser)]
enum AmpsSubcommand {
    #[clap(about = "List available amps")]
    List,
    #[clap(
        name = "exec",
        about = "Execute an amp, either by name or through prompt",
        alias = "ex"
    )]
    Exec(ExecCommand),
}

#[derive(Parser)]
struct ExecCommand {
    /// The name of the amp to execute
    #[clap()]
    pub name: Option<String>,
}

pub enum AmpsOptions {
    List,
    Exec(String),
}

impl From<AmpsCommand> for AmpsOptions {
    fn from(options: AmpsCommand) -> Self {
        match options.subcommand {
            Some(AmpsSubcommand::List) => AmpsOptions::List,
            Some(AmpsSubcommand::Exec(options)) => options.into(),
            // defaults to list available amps
            None => AmpsOptions::List,
        }
    }
}

impl From<ExecCommand> for AmpsOptions {
    fn from(options: ExecCommand) -> Self {
        match options.name {
            Some(name) => AmpsOptions::Exec(name),
            // defaults to execute amp through prompt
            None => AmpsOptions::Exec(String::new()),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::cli::{Args, Command};

    use super::*;

    #[test]
    fn test_amps_list_command() {
        let args = Args::parse_from(vec!["gr", "amps", "list"]);
        match args.command {
            Command::Amps(AmpsCommand {
                subcommand: Some(AmpsSubcommand::List),
            }) => {}
            _ => panic!("Expected Amp ListCommand"),
        }
    }

    #[test]
    fn test_amps_exec_command() {
        let args = Args::parse_from(vec!["gr", "amps", "exec", "amp-name"]);
        match args.command {
            Command::Amps(AmpsCommand {
                subcommand: Some(AmpsSubcommand::Exec(ExecCommand { name })),
            }) => {
                assert_eq!(name, Some("amp-name".to_string()));
            }
            _ => panic!("Expected Amp ExecCommand"),
        }
    }
}