diff options
author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2025-05-03 20:00:17 +0100 |
---|---|---|
committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2025-05-03 20:00:17 +0100 |
commit | 6b2711c5703c2b724066c88967c1924f1f1dbedf (patch) | |
tree | 4561b0fb47a3429b86e82b19f335b6aed1081e5f /src/config.rs | |
download | z2m-utils-master.tar.gz z2m-utils-master.tar.xz z2m-utils-master.zip |
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..e8a4287 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,64 @@ +use std::{fs::File, io::Read, process, time::Duration}; + +use rumqttc::{Client, Connection, MqttOptions}; +use serde::Deserialize; + +use crate::PROGRAM; + +#[derive(Deserialize)] +pub struct Credentials { + pub username: String, + pub password: String, +} + +fn default_host() -> String { + "localhost".to_string() +} + +fn default_port() -> u16 { + 1883 +} + +fn default_id_prefix() -> String { + PROGRAM.to_string() +} + +#[derive(Deserialize)] +pub struct Config { + #[serde(default = "default_host")] + pub host: String, + #[serde(default = "default_port")] + pub port: u16, + #[serde(default = "default_id_prefix")] + pub id_prefix: String, + pub credentials: Option<Credentials>, +} + +impl Config { + pub fn mqtt_client(&self) -> (Client, Connection) { + let client_id = format!("{}_{}", self.id_prefix, process::id()); + let mut options = MqttOptions::new(client_id, &self.host, self.port); + if let Some(credentials) = &self.credentials { + options.set_credentials(&credentials.username, &credentials.password); + } + options.set_keep_alive(Duration::from_secs(5)); + Client::new(options, 10) + } +} + +pub fn get() -> Config { + let config = if let Ok(dirs) = xdg::BaseDirectories::with_prefix(PROGRAM) { + if let Some(config_file) = dirs.find_config_file("config.toml") { + let mut config_file = File::open(config_file).unwrap(); + let mut config = String::new(); + config_file.read_to_string(&mut config).unwrap(); + Some(config) + } else { + None + } + } else { + None + }; + let config = config.unwrap_or_default(); + toml::from_str(&config).unwrap() +} |