From 7992857e5f37f8df312ab1367ba39a9db5ac17e9 Mon Sep 17 00:00:00 2001 From: Mo8it Date: Thu, 23 Feb 2023 17:10:24 +0100 Subject: [PATCH] Document config --- src/config.rs | 27 +++++++++++++++++++-------- src/mailer.rs | 2 +- src/states.rs | 4 ++-- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/config.rs b/src/config.rs index 45fea36..598d531 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,9 +1,8 @@ use anyhow::{Context, Result}; use serde::Deserialize; -use std::env; -use std::fs::File; -use std::io::BufReader; +use std::{env, fs::File, io::BufReader}; +/// Email server credentials. #[derive(Deserialize)] pub struct EmailServer { pub server_name: String, @@ -11,30 +10,35 @@ pub struct EmailServer { pub password: String, } +/// UTC offset for time formatting. #[derive(Deserialize)] pub struct UtcOffset { pub hours: i8, pub minutes: i8, } +/// Error messages for localization. #[derive(Deserialize)] pub struct ErrorMessages { pub captcha_error: String, pub email_error: String, } +/// Field localization strings in a form with label and invalid feedback on wrong field input. #[derive(Deserialize)] pub struct Field { pub label: String, pub invalid_feedback: String, } +/// Localization strings. #[derive(Deserialize)] pub struct Strings { pub description: String, pub title: String, pub name_field: Field, pub email_field: Field, + /// No invalid feedback because it is optional. pub telefon_field_label: String, pub message_field: Field, pub captcha_field: Field, @@ -42,13 +46,19 @@ pub struct Strings { pub success: String, } +/// Configuration. #[derive(Deserialize)] pub struct Config { + /// The language tag of the HTML file. pub lang: String, + /// The path prefix of all routes. pub path_prefix: String, + /// The server socket address including port. pub socket_address: String, pub email_server: EmailServer, + /// From mailbox. pub email_from: String, + /// To mailbox. pub email_to: String, pub log_file: String, pub utc_offset: UtcOffset, @@ -57,16 +67,17 @@ pub struct Config { } impl Config { - pub fn new() -> Result { + pub fn build() -> Result { + // The environment variable with the path to the config file. let config_file_var = "CF_CONFIG_FILE"; let config_path = env::var(config_file_var) .with_context(|| format!("Environment variable {config_file_var} missing!"))?; - let config_file = File::open(&config_path) + let file = File::open(&config_path) .with_context(|| format!("Can not open the config file at the path {config_path}"))?; - let config_reader = BufReader::new(config_file); - let config: Self = serde_yaml::from_reader(config_reader) - .context("Can not parse the YAML config file!")?; + let reader = BufReader::new(file); + let config = + serde_yaml::from_reader(reader).context("Can not parse the YAML config file!")?; Ok(config) } diff --git a/src/mailer.rs b/src/mailer.rs index 998b2f1..576ab1d 100644 --- a/src/mailer.rs +++ b/src/mailer.rs @@ -13,7 +13,7 @@ pub struct Mailer { } impl Mailer { - pub fn new(config: &Config) -> Result { + pub fn build(config: &Config) -> Result { let creds = Credentials::new( config.email_server.email.clone(), config.email_server.password.clone(), diff --git a/src/states.rs b/src/states.rs index fcd346f..2238fdb 100644 --- a/src/states.rs +++ b/src/states.rs @@ -13,8 +13,8 @@ pub struct AppState { impl AppState { pub fn build() -> Result { - let config = Config::new()?; - let mailer = Arc::new(Mailer::new(&config)?); + let config = Config::build()?; + let mailer = Arc::new(Mailer::build(&config)?); let config = Arc::new(config); let captcha_solutions = Arc::new(Mutex::new(CaptchaSolutions::default()));