From d37f31dd8add905f6efada74b6d5a73a04461bfc Mon Sep 17 00:00:00 2001 From: finga Date: Tue, 24 Jan 2023 00:46:25 +0100 Subject: [PATCH] Add config file parsing Use the `serde` and `toml` crates to handle config file parsing. --- Cargo.lock | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ config.toml | 6 ++++ src/config.rs | 47 ++++++++++++++++++++++++++++ src/main.rs | 33 ++++++++++++-------- 5 files changed, 160 insertions(+), 12 deletions(-) create mode 100644 config.toml create mode 100644 src/config.rs diff --git a/Cargo.lock b/Cargo.lock index 2345679..cc0eb0b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -160,6 +160,12 @@ dependencies = [ "slab", ] +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "hostname" version = "0.3.1" @@ -188,6 +194,16 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indexmap" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "instant" version = "0.1.12" @@ -315,6 +331,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "nom8" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae01545c9c7fc4486ab7debaf2aad7003ac19431791868fb2e8066df97fad2f8" +dependencies = [ + "memchr", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -561,6 +586,29 @@ name = "serde" version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_spanned" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c68e921cef53841b8925c2abadd27c9b891d9613bdc43d6b823062866df38e8" +dependencies = [ + "serde", +] [[package]] name = "sharded-slab" @@ -672,6 +720,40 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" +[[package]] +name = "toml" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb9d890e4dc9298b70f740f615f2e05b9db37dce531f6b24fb77ac993f9f217" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4553f467ac8e3d374bc9a177a26801e5d0f9b211aa1673fb137a403afd1c9cf5" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "729bfd096e40da9c001f778f5cdecbd2957929a24e10e5883d9392220a751581" +dependencies = [ + "indexmap", + "nom8", + "serde", + "serde_spanned", + "toml_datetime", +] + [[package]] name = "tracing" version = "0.1.37" @@ -777,7 +859,9 @@ dependencies = [ "diesel", "lettre", "log", + "serde", "time", + "toml", "tracing", "tracing-subscriber", ] diff --git a/Cargo.toml b/Cargo.toml index c37d84a..bb9ee7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,5 @@ tracing-subscriber = "0.3" time = "0.3" lettre = { version = "0.10", features = ["tracing"] } diesel = { version = "2", features = ["postgres", "r2d2", "time"] } +serde = { version = "1", features = ["derive"] } +toml = "0.6" diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..25af680 --- /dev/null +++ b/config.toml @@ -0,0 +1,6 @@ +[database] +# host = "localhost" +# port = 5432 +# name = "whakarite" +# user = "whakarite" +pass = "whakarite" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..8ee0351 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,47 @@ +use serde::Deserialize; + +fn default_host() -> String { + "localhost".to_string() +} + +const fn default_port() -> u16 { + 5432 +} + +fn default_name() -> String { + "whakarite".to_string() +} + +fn default_user() -> String { + "whakarite".to_string() +} + +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Database { + /// Host of the database + #[serde(default = "default_host")] + pub host: String, + + /// Port of the database + #[serde(default = "default_port")] + pub port: u16, + + /// Name of the database + #[serde(default = "default_name")] + pub name: String, + + /// Name of the user to connect to the database + #[serde(default = "default_user")] + pub user: String, + + /// Password of the user to connect to the database + pub pass: String, +} + +#[derive(Debug, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Config { + /// Database configuration + pub database: Database, +} diff --git a/src/main.rs b/src/main.rs index 36ace9f..be987ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,16 +8,18 @@ use std::{env, thread::park_timeout}; use time::{Duration, OffsetDateTime}; use tracing::{debug, info, trace}; +mod config; mod models; mod schema; +use config::Config; use models::{NewReminder, Reminder}; fn remind( db: &mut PooledConnection>, mailer: &SmtpTransport, ) -> Result<()> { - debug!("executing remind run"); + info!("checking for reminders"); for reminder in schema::reminders::dsl::reminders .filter(schema::reminders::executed.is_null()) @@ -40,16 +42,12 @@ fn remind( debug!("email sent to {}", reminder.receiver); } else { - info!( - "parking reminder for {:?}", - reminder.planned - OffsetDateTime::now_utc(), - ); + let duration = reminder.planned - OffsetDateTime::now_utc(); + info!(?duration, "parking reminder"); - park_timeout(::try_from( - reminder.planned - OffsetDateTime::now_utc(), - )?); + park_timeout(::try_from(duration)?); } - // Check for another remnd job if none dont loop forever + // Check for another remind job if none dont loop forever } Ok(()) @@ -64,9 +62,20 @@ fn main() -> Result<()> { env!("CARGO_PKG_VERSION") ); - let db_pool = Pool::builder().build(ConnectionManager::::new( - "postgresql://whakarite:whakarite@localhost/whakarite", - ))?; + let config_path = "./config.toml"; + + let config: Config = toml::from_str(&std::fs::read_to_string(config_path)?)?; + + trace!(?config, config_path, "loaded config"); + + let db_pool = Pool::builder().build(ConnectionManager::::new(format!( + "postgresql://{}:{}@{}:{}/{}", + config.database.user, + config.database.pass, + config.database.host, + config.database.port, + config.database.name + )))?; let test_reminder = NewReminder { created: OffsetDateTime::now_utc(),