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
use clap::Parser;

use crate::cmds::trending::TrendingCliArgs;

use super::common::GetArgs;

#[derive(Parser)]
pub struct TrendingCommand {
    #[clap(
        long,
        value_parser(validate_trends_domain),
        default_value = "github.com"
    )]
    pub domain: String,
    #[clap()]
    pub language: String,
    #[clap(flatten)]
    get_args: GetArgs,
}

pub enum TrendingOptions {
    Get(TrendingCliArgs),
}

impl From<TrendingCommand> for TrendingOptions {
    fn from(options: TrendingCommand) -> Self {
        TrendingOptions::Get(TrendingCliArgs {
            domain: options.domain,
            language: options.language,
            get_args: options.get_args.into(),
            flush: false,
        })
    }
}

impl From<TrendingOptions> for TrendingCliArgs {
    fn from(options: TrendingOptions) -> Self {
        match options {
            TrendingOptions::Get(args) => args,
        }
    }
}

fn validate_trends_domain(domain: &str) -> Result<String, String> {
    if domain == "github.com" {
        Ok(domain.to_string())
    } else {
        Err("Trending projects implemented for github.com only".to_string())
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_validate_trends_domain() {
        assert_eq!("github.com", validate_trends_domain("github.com").unwrap());
        assert!(validate_trends_domain("github.foo.com").is_err());
        assert!(validate_trends_domain("gitlab.com").is_err());
    }
}