fb6b7cf7b82e7a28d38f4798e1c90f054bc092cd
[gandi_dns_update.git] / src / config.rs
1 use ron::{ de::from_reader, ser::to_writer };
2 use serde::{ Deserialize, Serialize };
3 use std::{ fs::File, time };
4
5 use crate::error::Result;
6
7 #[derive(Debug, Clone, Deserialize, Serialize)]
8 pub struct Config {
9 pub delay_between_check: time::Duration,
10 pub api_key: String,
11 pub domains: Vec<(String, String)>, // Hostname, domain.
12 pub ttl: i32
13 }
14
15 impl Config {
16 pub fn default() -> Self {
17 Config { delay_between_check: time::Duration::from_secs(120), api_key: String::from(""), domains: Vec::new(), ttl: 300 }
18 }
19
20 pub fn read(file_path: &str) -> Result<Config> {
21 match File::open(file_path) {
22 Ok(file) => from_reader(file).map_err(|e| e.into()),
23 // The file doesn't exit -> create it with default values.
24 Err(_) => {
25 let file = File::create(file_path)?;
26 let default_config = Config::default();
27 to_writer(file, &default_config)?;
28 Ok(default_config)
29 }
30 }
31 }
32 }