From a1cd8f431dc12d42a81f86f48b48777eb2230fe8 Mon Sep 17 00:00:00 2001 From: Mo8it Date: Sat, 29 Oct 2022 00:41:02 +0200 Subject: [PATCH] Add config.rs --- src/config.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/config.rs diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..71bec9d --- /dev/null +++ b/src/config.rs @@ -0,0 +1,43 @@ +use serde::Deserialize; +use std::env; +use std::fs::File; +use std::io::BufReader; + +#[derive(Deserialize)] +pub struct EmailServer { + pub server_name: String, + pub email: String, + pub password: String, +} + +#[derive(Deserialize)] +pub struct Address { + pub name: String, + pub user: String, + pub domain: String, +} + +#[derive(Deserialize)] +pub struct Config { + pub path_prefix: String, + pub email_server: EmailServer, + pub email_from: Address, + pub email_to: Address, +} + +impl Config { + pub fn new() -> Self { + let config_file_var = "CF_CONFIG_FILE"; + let config_path = env::var(config_file_var) + .unwrap_or_else(|_| panic!("Environment variable {config_file_var} missing!")); + + let config_file = File::open(&config_path).unwrap_or_else(|e| { + panic!("Can not open the config file at the path {config_path}: {e}") + }); + let config_reader = BufReader::new(config_file); + let config: Self = + serde_json::from_reader(config_reader).expect("Can not parse the config file as JSON!"); + + config + } +}