75 lines
1.8 KiB
Rust
75 lines
1.8 KiB
Rust
use config::{Config, ConfigError, Environment, File};
|
|
use serde::Deserialize;
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct Settings {
|
|
#[serde(default = "default_ha_url")]
|
|
pub ha_url: String,
|
|
|
|
#[serde(default = "default_ha_token")]
|
|
pub ha_token: String,
|
|
|
|
#[serde(default = "default_target_fps")]
|
|
pub target_fps: u32,
|
|
|
|
#[serde(default = "default_screenshot_fps")]
|
|
pub screenshot_fps: u32,
|
|
|
|
#[serde(default = "default_smoothing")]
|
|
pub smoothing: f32,
|
|
|
|
#[serde(default)]
|
|
pub lights: Vec<String>,
|
|
|
|
#[serde(default = "default_restore_on_exit")]
|
|
pub restore_on_exit: bool,
|
|
|
|
#[serde(default = "default_min_diff_percent")]
|
|
pub min_diff_percent: f32,
|
|
}
|
|
|
|
fn default_min_diff_percent() -> f32 {
|
|
1.0
|
|
}
|
|
|
|
fn default_restore_on_exit() -> bool {
|
|
true
|
|
}
|
|
|
|
fn default_target_fps() -> u32 {
|
|
3
|
|
}
|
|
|
|
fn default_screenshot_fps() -> u32 {
|
|
15
|
|
}
|
|
|
|
fn default_smoothing() -> f32 {
|
|
0.0
|
|
}
|
|
|
|
fn default_ha_url() -> String {
|
|
"http://localhost:8123".to_string()
|
|
}
|
|
|
|
fn default_ha_token() -> String {
|
|
"YOUR_TOKEN".to_string()
|
|
}
|
|
|
|
impl Settings {
|
|
pub fn new() -> Result<Self, ConfigError> {
|
|
let builder = Config::builder()
|
|
// Load settings from a configuration file named "config" (e.g. config.toml, config.json)
|
|
// It's optional, so the program won't crash if the file doesn't exist
|
|
.add_source(File::with_name("config").required(false))
|
|
// Merge settings from environment variables
|
|
// Variables like HA_URL and HA_TOKEN will automatically map to the struct fields
|
|
.add_source(Environment::default());
|
|
|
|
let config = builder.build()?;
|
|
|
|
// Deserialize the gathered configuration into the Settings struct
|
|
config.try_deserialize()
|
|
}
|
|
}
|