Adapt settings system from woppleblox

This commit is contained in:
Starbeamrainbowlabs 2021-09-28 23:55:34 +01:00
parent f6563932c9
commit f55fd2f91b
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
5 changed files with 64 additions and 3 deletions

View file

@ -7,12 +7,15 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
log = "0.4" # Apparently Rust has an entire logging setup - this is the base crate
log = "0.4" # Apparently Rust has an entire logging setup - this is the base crate
pretty_env_logger = "0.4" # The logging implementation we've chosen for the above pretty_env_logger = "0.4" # The logging implementation we've chosen for the above
clap = "2" # CLI argument parser clap = "2" # CLI argument parser
serde = { version = "1.0", features = ["derive"] } # Serialisation
serde_derive = "1.0" # Serialisation helper
toml = "0.5" # TOML serialisation module
psk-client = { version = "0.1", features = ["vendored-openssl"] } # Pre-shared key TLS support psk-client = { version = "0.1", features = ["vendored-openssl"] } # Pre-shared key TLS support

0
src/agent/mod.rs Normal file
View file

View file

@ -6,6 +6,12 @@ use std::process;
use clap::{ App, AppSettings, Arg, SubCommand }; use clap::{ App, AppSettings, Arg, SubCommand };
mod settings;
use settings::Settings;
fn main() { fn main() {
// Initialise the logging system // Initialise the logging system
pretty_env_logger::init(); pretty_env_logger::init();

View file

@ -0,0 +1,20 @@
use serde::{ Serialize, Deserialize };
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Settings {
pub bind_address : String,
pub port : i16,
pub secret_join : String,
pub secret_query : String
}
impl Default for Settings {
fn default() -> Settings {
Settings {
bind_address: "127.0.0.1".to_string(),
port: 3015,
secret_join: "CHANGE_ME".to_string(),
secret_query: "CHANGE_ME".to_string()
}
}
}

32
src/settings/mod.rs Normal file
View file

@ -0,0 +1,32 @@
use std::fs;
// for toml::toml!, which are aren't currently using
// #[macro_use]
// use toml;
mod definitions;
pub use definitions::Settings;
impl Settings {
pub fn new() -> Settings {
Settings::default()
}
/// Creates a new Settings object from the contents of a given (TOML) file.
pub fn from_file(filename : String) -> Settings {
let config_str = match fs::read_to_string(&filename) {
Ok(str) => str,
Err(error) => {
// println!("{}", error);
panic!("{}", error); // Oops, something went wrong while reading the config file
}
};
let config_obj : Settings = toml::from_str(&config_str).unwrap();
// info!("{:?}", config_obj);
return config_obj;
}
}