use super::errors::{LibError, MessagedError}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::fs::File; use std::io::prelude::*; use std::path::PathBuf; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Config { pub api_key: String, pub tags_url: String, pub tag_url: String, pub states_url: String, pub locations_url: String, pub current_locations_url: String, pub sharetoken_url: String, pub timestamp_format: String, pub timedate_format: String, pub date_format: String, pub time_format: String, pub throttle: u32, pub nicknames: HashMap, } impl Config { pub fn from_path(path: &PathBuf) -> Result { let mut file = File::open(&path) .with_msg(format!("Could not find {}", path.to_str().unwrap_or("")))?; let mut string = String::new(); file.read_to_string(&mut string) .with_msg("config file is not valid UTF-8")?; Ok(toml::from_str(&string).with_msg("given config file is not a valid config file")?) } pub fn write_to(&self, path: &PathBuf) -> Result<(), LibError> { let mut file = File::create(&path).unwrap(); file.write_all(&toml::to_vec(self).unwrap()) .with_msg("Could not write config.toml, make sure you have correct permissions.")?; Ok(()) } } impl Default for Config { fn default() -> Config { Config { api_key: "".to_owned(), tags_url: "https://platform.yepzon.com/tags".to_owned(), tag_url: "https://platform.yepzon.com/tags/{tag}".to_owned(), states_url: "https://platform.yepzon.com/tags/{tag}/states".to_owned(), locations_url: "https://platform.yepzon.com/tags/{tag}/locations/{state}".to_owned(), current_locations_url: "https://platform.yepzon.com/tags/{tag}/locations".to_owned(), sharetoken_url: "https://platform.yepzon.com/tags/{tag}/sharetoken".to_owned(), timestamp_format: "%Y-%m-%dT%H:%M:%S%.fZ".to_owned(), timedate_format: "%d.%m.%Y-%H:%M:%S".to_owned(), date_format: "%d.%m.%Y".to_owned(), time_format: "%H:%M:%S".to_owned(), throttle: 9, nicknames: HashMap::new(), } } }