summaryrefslogtreecommitdiff
path: root/server/src/config/load.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/config/load.rs')
-rw-r--r--server/src/config/load.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/server/src/config/load.rs b/server/src/config/load.rs
new file mode 100644
index 0000000..7566ad9
--- /dev/null
+++ b/server/src/config/load.rs
@@ -0,0 +1,47 @@
+// Copyright 2022-2024 Gabriel Bjørnager Jensen.
+
+use crate::{Config, Error, Result};
+
+use configparser::ini::Ini;
+use std::env::args;
+use std::path::PathBuf;
+
+impl Config {
+ pub fn load() -> Result<Self> {
+ let path = {
+ let mut args = args().skip(0x1);
+
+ let Some(path) = args.next() else { return Err(Error::MissingConfig) };
+
+ if let Some(arg) = args.next() { return Err(Error::IllegalArgument { arg }) };
+
+ PathBuf::from(path)
+ };
+
+ let mut config = Ini::new();
+ config
+ .load(path)
+ .map_err(|e| Error::ConfigError { message: e })?;
+
+ macro_rules! get_field {
+ ($section:ident.$key:ident) => {{
+ const FIELD: &str = concat!(stringify!($section), ".", stringify!($key));
+
+ if let Some(value) = config.get(stringify!($section), stringify!($key)) {
+ value
+ .parse()
+ .map_err(|e| Error::IllegalFieldValue { field: FIELD, value, source: Box::new(e) })
+ } else {
+ Err(Error::MissingField { field: FIELD })
+ }
+ }};
+ }
+
+ Ok(Self {
+ name: get_field!(server.name)?,
+ port: get_field!(server.port)?,
+
+ max_player_count: get_field!(server.max_player_count)?,
+ })
+ }
+}