e3b1309555b508c1032bf40e05be402c3218e879
[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 fqdn: String,
12 pub domains: Vec<String>,
13 pub ttl: i32
14 }
15
16 impl Config {
17 pub fn default() -> Self {
18 Config { delay_between_check: time::Duration::from_secs(120), api_key: String::from(""), fqdn: String::from(""), domains: Vec::new(), ttl: 300 }
19 }
20
21 pub fn read(file_path: &str) -> Result<Config> {
22 match File::open(file_path) {
23 Ok(file) => from_reader(file).map_err(|e| e.into()),
24 // The file doesn't exit -> create it with default values.
25 Err(_) => {
26 let file = File::create(file_path)?;
27 let default_config = Config::default();
28 to_writer(file, &default_config)?;
29 Ok(default_config)
30 }
31 }
32 }
33 }