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