aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl_config/src/lib.rs
blob: 218a249c4d9e03ec0a7fdbd11f81dbaf2a275789 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (C) 2021-2021 The Whirlsplash Collective
// SPDX-License-Identifier: GPL-3.0-only

//! Configuration utilities, to interact with the configuration system.

#![feature(
  type_ascription,
  hash_set_entry,
  type_name_of_val,
  decl_macro,
  proc_macro_hygiene
)]
#![deny(
  warnings,
  nonstandard_style,
  unused,
  future_incompatible,
  rust_2018_idioms,
  unsafe_code
)]
#![deny(clippy::all, clippy::nursery, clippy::pedantic)]
#![recursion_limit = "128"]
#![doc(
  html_logo_url = "https://raw.githubusercontent.com/Whirlsplash/assets/master/Whirl.png",
  html_favicon_url = "https://raw.githubusercontent.com/Whirlsplash/assets/master/Whirl.png"
)]

#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate log;

mod structures;

use config::{ConfigError, File};

use crate::structures::{
  DistributorConfig,
  HubConfig,
  WhirlsplashApiConfig,
  WhirlsplashConfig,
  WhirlsplashLogConfig,
  WhirlsplashPromptConfig,
};

#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
  pub version:     String,
  pub whirlsplash: WhirlsplashConfig,
  pub distributor: DistributorConfig,
  pub hub:         HubConfig,
}
impl Config {
  /// Re-fetch the configuration from the configuration file.
  ///
  /// # Panics
  /// - May panic if the configuration is unable to be refreshed.
  #[deprecated(
    note = "the current implementation of the configurations system automatically performs \
            refreshes, this method has no effects"
  )]
  pub fn refresh() { let _ = config::Config::new().refresh().unwrap(); }

  fn load() -> Result<Self, ConfigError> {
    let mut s = config::Config::new();

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

  /// Get a certain configuration key or group from the configuration file.
  ///
  /// # Panics
  /// - May panic if the configuration is unable to be read.
  #[must_use]
  pub fn get() -> Self {
    return if let Err(why) = Self::load() {
      error!(
        "unable to load configuration file, reverting to default value: {}",
        why
      );
      warn!(
        "you should probably generate yourself a configuration file with `whirl config generate`!"
      );

      Self::default()
    } else {
      Self::load().unwrap()
    };
  }
}
impl Default for Config {
  fn default() -> Self {
    Self {
      version:     "0.1.0".to_string(),
      whirlsplash: WhirlsplashConfig {
        worldsmaster_username: "WORLDSMASTER".to_string(),
        ip:                    "0.0.0.0".to_string(),
        api:                   WhirlsplashApiConfig {
          port: 80
        },
        prompt:                WhirlsplashPromptConfig {
          enable: false,
          ps1:    "[WORLDSMASTER@Whirlsplash ~]$".to_string(),
        },
        log:                   WhirlsplashLogConfig {
          enable:     true,
          level:      1,
          everything: false,
          test:       false,
          file:       true,
        },
      },
      distributor: DistributorConfig {
        worldsmaster_greeting: "Welcome to Whirlsplash!".to_string(),
        port:                  6650,
      },
      hub:         HubConfig {
        port: 5673
      },
    }
  }
}