2020-02-14 12:20:17 +01:00
|
|
|
use std::str::FromStr;
|
2020-02-18 16:55:19 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-02-16 16:02:03 +01:00
|
|
|
use serde::{Serialize, Deserialize};
|
2020-02-18 16:55:19 +01:00
|
|
|
use std::io::Read;
|
|
|
|
use std::fs::File;
|
2020-02-14 12:20:17 +01:00
|
|
|
|
|
|
|
use crate::error::Result;
|
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
use std::default::Default;
|
|
|
|
|
2020-09-15 14:31:10 +02:00
|
|
|
use std::collections::HashMap;
|
2020-02-18 16:55:19 +01:00
|
|
|
|
2020-09-15 14:31:10 +02:00
|
|
|
use config::Config;
|
|
|
|
pub use config::ConfigError;
|
|
|
|
use glob::glob;
|
|
|
|
|
|
|
|
pub fn read(path: &Path) -> Result<Settings> {
|
|
|
|
let mut settings = Config::default();
|
|
|
|
settings
|
|
|
|
.merge(config::File::from(path)).unwrap();
|
|
|
|
|
|
|
|
Ok(settings.try_into()?)
|
2020-02-14 12:20:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 16:02:03 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2020-09-15 14:31:10 +02:00
|
|
|
pub struct Settings {
|
|
|
|
pub listens: Box<[Listen]>,
|
|
|
|
pub shelly: Option<ShellyCfg>
|
2020-02-14 12:20:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 16:02:03 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2020-09-15 14:31:10 +02:00
|
|
|
pub struct ShellyCfg {
|
|
|
|
pub mqtt_url: String
|
2020-02-14 12:20:17 +01:00
|
|
|
}
|
2020-02-18 16:55:19 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct Listen {
|
|
|
|
pub address: String,
|
|
|
|
pub port: Option<u16>,
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:31:10 +02:00
|
|
|
impl Default for Settings {
|
2020-02-18 16:55:19 +01:00
|
|
|
fn default() -> Self {
|
2020-09-15 14:31:10 +02:00
|
|
|
Settings {
|
|
|
|
listens: Box::new([Listen {
|
2020-02-18 16:55:19 +01:00
|
|
|
address: "127.0.0.1".to_string(),
|
|
|
|
port: Some(DEFAULT_PORT)
|
|
|
|
},
|
|
|
|
Listen {
|
|
|
|
address: "::1".to_string(),
|
|
|
|
port: Some(DEFAULT_PORT)
|
2020-09-15 14:31:10 +02:00
|
|
|
}]),
|
|
|
|
shelly: Some(ShellyCfg {
|
|
|
|
mqtt_url: "127.0.0.1:1883".to_string()
|
|
|
|
}),
|
2020-02-18 16:55:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The default port in the non-assignable i.e. free-use area
|
|
|
|
pub const DEFAULT_PORT: u16 = 59661;
|