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
use crate::CliOption;

/// RServer's Config struct.
#[derive(Debug, Clone)]
pub struct Config {
    pub host: String,
    pub port: i32,
    pub enable_proxy: bool,
    pub proxy_host: String,
    pub proxy_port: i32,
}

impl Config {
    pub fn new(
        host: String,
        port: i32,
        enable_proxy: bool,
        proxy_host: String,
        proxy_port: i32,
    ) -> Self {
        Config {
            host,
            port,
            enable_proxy,
            proxy_host,
            proxy_port,
        }
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            host: String::from("127.0.0.1"),
            port: 8080,
            enable_proxy: false,
            proxy_host: String::default(),
            proxy_port: i32::default(),
        }
    }
}

impl From<CliOption> for Config {
    fn from(cli_option: CliOption) -> Self {
        Self {
            host: cli_option.host,
            port: cli_option.port,
            enable_proxy: matches!(cli_option.enable_proxy.as_str(), "true"),
            proxy_host: match cli_option.proxy_host {
                Some(p) => p,
                _ => String::default(),
            },
            proxy_port: match cli_option.proxy_port {
                Some(p) => p,
                _ => i32::default(),
            },
        }
    }
}