yepzon-locationer/thingy_lib/src/config.rs

72 lines
2.3 KiB
Rust
Raw Normal View History

2020-08-29 01:27:23 +02:00
use super::errors::{LibError, MessagedError};
2020-08-23 00:53:49 +02:00
use serde::{Deserialize, Serialize};
2020-08-24 18:50:29 +02:00
use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
2020-08-23 00:53:49 +02:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub api_key: String,
pub tags_url: String,
2020-08-25 21:30:13 +02:00
pub tag_url: String,
2020-08-23 00:53:49 +02:00
pub states_url: String,
pub locations_url: String,
pub current_locations_url: String,
2020-08-25 21:18:59 +02:00
pub sharetoken_url: String,
2020-08-23 00:53:49 +02:00
pub timestamp_format: String,
2020-08-24 02:33:40 +02:00
pub timedate_format: String,
pub date_format: String,
pub time_format: String,
2020-08-23 00:53:49 +02:00
pub throttle: u32,
2020-08-24 18:50:29 +02:00
pub nicknames: HashMap<String, String>,
}
impl Config {
2020-08-29 01:27:23 +02:00
pub fn from_path(path: &PathBuf) -> Result<Config, LibError> {
2020-08-24 18:50:29 +02:00
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")?)
}
2020-08-29 01:27:23 +02:00
pub fn write_to(&self, path: &PathBuf) -> Result<(), LibError> {
2020-08-24 18:50:29 +02:00
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(())
}
2020-08-23 00:53:49 +02:00
}
impl Default for Config {
fn default() -> Config {
Config {
2020-08-24 01:17:50 +02:00
api_key: "<your API key here from https://developers.yepzon.com/>".to_owned(),
2020-08-23 00:53:49 +02:00
tags_url: "https://platform.yepzon.com/tags".to_owned(),
2020-08-25 21:30:13 +02:00
tag_url: "https://platform.yepzon.com/tags/{tag}".to_owned(),
2020-08-23 00:53:49 +02:00
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(),
2020-08-25 21:18:59 +02:00
sharetoken_url: "https://platform.yepzon.com/tags/{tag}/sharetoken".to_owned(),
2020-08-23 00:53:49 +02:00
timestamp_format: "%Y-%m-%dT%H:%M:%S%.fZ".to_owned(),
2020-08-24 02:33:40 +02:00
timedate_format: "%d.%m.%Y-%H:%M:%S".to_owned(),
date_format: "%d.%m.%Y".to_owned(),
time_format: "%H:%M:%S".to_owned(),
2020-08-23 00:53:49 +02:00
2020-08-24 01:17:50 +02:00
throttle: 9,
2020-08-24 18:50:29 +02:00
nicknames: HashMap::new(),
2020-08-23 00:53:49 +02:00
}
}
}