forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.rs
More file actions
146 lines (123 loc) · 3.77 KB
/
config.rs
File metadata and controls
146 lines (123 loc) · 3.77 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use std::fs::read_to_string;
use std::time::Duration;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ConfigError {
#[error("Badly formatted configuration file: {0}")]
Parse(toml::de::Error),
#[error("Failed to read configuration file: {0}")]
Io(std::io::Error),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Config {
pub io: IO,
pub proxy: Proxy,
pub server: Server,
pub gameplay: Gameplay,
pub log: Log,
pub resource_pack: ResourcePack,
pub world: World,
}
pub const DEFAULT_CONFIG_STR: &str = include_str!("../config/feather.toml");
impl Default for Config {
fn default() -> Self {
toml::from_str(DEFAULT_CONFIG_STR).unwrap()
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct IO {
pub compression_threshold: i32,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Proxy {
pub proxy_mode: ProxyMode,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Server {
pub online_mode: bool,
pub motd: String,
pub max_players: i32,
pub view_distance: u8,
pub address: String,
pub port: u16,
pub default_gamemode: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Gameplay {
pub monster_spawning: bool,
pub animal_spawning: bool,
pub pvp: bool,
pub nerf_spawner_mobs: bool,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Log {
pub level: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ResourcePack {
pub url: String,
pub hash: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct World {
pub name: String,
pub generator: String,
pub seed: String,
#[serde(with = "humantime_serde")]
pub save_interval: Duration,
}
/// Loads the configuration from the given file/
pub fn load_from_file(path: &str) -> Result<Config, ConfigError> {
let input = read_to_string(path).map_err(ConfigError::Io)?;
load(input)
}
/// Loads the configuration from the given string.
pub fn load(input: String) -> Result<Config, ConfigError> {
let config: Config = toml::from_str(&input).map_err(ConfigError::Parse)?;
Ok(config)
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum ProxyMode {
#[serde(alias = "none")]
None,
#[serde(alias = "bungeecord")]
BungeeCord,
#[serde(alias = "velocity")]
Velocity,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_load_config() {
let input = include_str!("../config/feather.toml");
let config = load(input.to_string()).expect("Config load failed");
let io = &config.io;
assert_eq!(io.compression_threshold, 256);
let server = &config.server;
assert_eq!(server.online_mode, true);
assert_eq!(server.motd, "A Feather server");
assert_eq!(server.max_players, 16);
assert_eq!(server.default_gamemode, "creative");
assert_eq!(server.view_distance, 6);
assert_eq!(server.address, "0.0.0.0");
assert_eq!(server.port, 25565);
let gameplay = &config.gameplay;
assert_eq!(gameplay.animal_spawning, true);
assert_eq!(gameplay.monster_spawning, true);
assert_eq!(gameplay.pvp, true);
assert_eq!(gameplay.nerf_spawner_mobs, false);
let log = &config.log;
assert_eq!(log.level, "debug");
let resource_pack = &config.resource_pack;
assert_eq!(resource_pack.url, "");
assert_eq!(resource_pack.hash, "");
let world = &config.world;
assert_eq!(world.name, "world");
assert_eq!(world.generator, "default");
assert_eq!(world.seed, "");
assert_eq!(world.save_interval.as_millis(), 1000 * 60);
let proxy = &config.proxy;
assert_eq!(proxy.proxy_mode, ProxyMode::None);
}
}