Send an email in case of error
[stakingWatchdog.git] / src / config.rs
1 use std::{fs::File, time};
2
3 use anyhow::Result;
4 use ron::{
5 de::from_reader,
6 ser::{to_writer_pretty, PrettyConfig},
7 };
8 use serde::{Deserialize, Serialize};
9
10 #[derive(Debug, Clone, Deserialize, Serialize)]
11 pub struct Config {
12 pub pub_keys: Vec<String>,
13 pub smtp_login: String,
14 pub smtp_password: String,
15 }
16
17 impl Config {
18 pub fn default() -> Self {
19 Config {
20 pub_keys: vec![],
21 smtp_login: "login".to_string(),
22 smtp_password: "password".to_string(),
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_pretty(file, &default_config, PrettyConfig::new())?;
34 Ok(default_config)
35 }
36 }
37 }
38 }