Add config file parsing

Use the `serde` and `toml` crates to handle config file parsing.
This commit is contained in:
finga 2023-01-24 00:46:25 +01:00
parent 665015c296
commit d37f31dd8a
5 changed files with 160 additions and 12 deletions

84
Cargo.lock generated
View file

@ -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",
]

View file

@ -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"

6
config.toml Normal file
View file

@ -0,0 +1,6 @@
[database]
# host = "localhost"
# port = 5432
# name = "whakarite"
# user = "whakarite"
pass = "whakarite"

47
src/config.rs Normal file
View file

@ -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,
}

View file

@ -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<ConnectionManager<PgConnection>>,
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(<std::time::Duration>::try_from(
reminder.planned - OffsetDateTime::now_utc(),
)?);
park_timeout(<std::time::Duration>::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::<PgConnection>::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::<PgConnection>::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(),