use std::{fs::read_to_string, io};
use crate::device::*;
use lazy_static::lazy_static;
use lifxi::http::Client;
lazy_static! {
pub static ref LIFX_CLIENT: Client = Client::new(LIFX_SECRET.to_string());
static ref LIFX_SECRET: String = CONFIG.lifx_secret.clone().expect("LIFX devices used without configuring a LIFX secret.");
pub static ref CONFIG: Config = Config::parse().expect("Failed to parse config file.");
}
#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Config {
pub devices: Vec<Device>,
pub(crate) lifx_secret: Option<String>,
}
impl Config {
pub fn parse() -> Result<Self, io::Error> {
let mut config_path = dirs::home_dir().unwrap();
config_path.push(".adm/config.toml");
let s = read_to_string(config_path)?;
Ok(toml::from_str(&s).unwrap())
}
pub fn find<'a, S: ToString>(&'a self, s: S) -> Option<&'a Device> {
let s = s.to_string();
self.devices.iter().find(|device| {
device.name.eq_ignore_ascii_case(&s)
|| device
.alternatives
.iter()
.map(|d| d.iter())
.flatten()
.any(|alt| alt.eq_ignore_ascii_case(&s))
})
}
}