aboutsummaryrefslogtreecommitdiff
path: root/src/config/mod.rs
blob: ef86810f854bfb911d783cef0b8cdcfb03b30e12 (plain) (blame)
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
// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
// SPDX-License-Identifier: GPL-3.0-only

use config::{ConfigError, File};

#[derive(Serialize, Deserialize, Debug)]
pub struct WhirlsplashConfig {
  pub worldsmaster_username: String,
  pub log_level:             i64,
  pub ip:                    String,
  pub prompt_ps1:            String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DistributorConfig {
  pub worldsmaster_greeting: String,
  pub port:                  i64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct HubConfig {
  pub port: i64,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
  pub whirlsplash: WhirlsplashConfig,
  pub distributor: DistributorConfig,
  pub hub:         HubConfig,
}
impl Config {
  fn load() -> Result<Self, ConfigError> {
    let mut s = config::Config::new();

    s.merge(File::with_name("./Whirl.toml").required(false))?;
    s.try_into()
  }

  pub fn get() -> Config {
    return if let Err(why) = Self::load() {
      error!(
        "unable to load configuration file, reverting to default value: {}",
        why
      );
      Self::default()
    } else {
      Self::load().unwrap()
    };
  }
}
impl Default for Config {
  fn default() -> Self {
    Config {
      whirlsplash: WhirlsplashConfig {
        worldsmaster_username: "WORLDSMASTER".to_string(),
        log_level:             1,
        ip:                    "0.0.0.0".to_string(),
        prompt_ps1:            "[WORLDSMASTER@Whirlsplash ~]$".to_string(),
      },
      distributor: DistributorConfig {
        worldsmaster_greeting: "Welcome to Whirlsplash!".to_string(),
        port:                  6650,
      },
      hub:         HubConfig {
        port: 5673
      },
    }
  }
}