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

use crate::cmds::user::UserCliArgs;

use super::common::GetArgs;

#[derive(Parser)]
pub struct UserCommand {
    #[clap(subcommand)]
    subcommand: UserSubCommand,
}

#[derive(Parser)]
enum UserSubCommand {
    #[clap(about = "Gets user information")]
    Get(GetUser),
}

#[derive(Parser)]
struct GetUser {
    /// Retrieve user information by username
    #[clap()]
    username: String,
    #[clap(flatten)]
    get_args: GetArgs,
}

impl From<UserCommand> for UserOptions {
    fn from(cmd: UserCommand) -> Self {
        match cmd.subcommand {
            UserSubCommand::Get(options) => options.into(),
        }
    }
}

impl From<GetUser> for UserOptions {
    fn from(options: GetUser) -> Self {
        UserOptions::Get(
            UserCliArgs::builder()
                .username(options.username)
                .get_args(options.get_args.into())
                .build()
                .unwrap(),
        )
    }
}

pub enum UserOptions {
    Get(UserCliArgs),
}

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

    use super::*;

    #[test]
    fn test_user_command() {
        let args = Args::parse_from(&["gr", "us", "get", "octocat"]);
        let user_command = match args.command {
            Command::User(cmd) => cmd,
            _ => panic!("Expected user command"),
        };
        let options: UserOptions = user_command.into();
        match options {
            UserOptions::Get(args) => {
                assert_eq!(args.username, "octocat");
            }
        }
    }
}