rlib/
config.rs

1use crate::files;
2use serde::{Deserialize, Serialize};
3use std::fs::File;
4use std::io::prelude::*;
5
6#[derive(Clone, Debug, Serialize, Deserialize)]
7pub struct Config {
8    pub clear_copy_timeout: u64,
9}
10
11impl Config {
12    pub fn load() -> Result<Self, ()> {
13        let fname = files::rpwd_path("config.json");
14        match File::open(&fname) {
15            Ok(f) => Ok(serde_json::from_reader::<File, Config>(f)
16                .expect("Failed deserializing configuration")),
17            Err(_) => Err(()),
18        }
19    }
20
21    pub fn save(&self) -> Self {
22        let fname = files::rpwd_path("config.json");
23        let json = serde_json::to_string_pretty(&self).expect("Failed to serialize passwords");
24
25        std::fs::create_dir_all(&files::rpwd()).expect("Failed to create rpw dir");
26        File::create(&fname)
27            .and_then(|mut f| {
28                f.write_all(&json.as_bytes()).expect("Failed to write file");
29                Ok(())
30            })
31            .or_else(|_| Err(format!("Failed to create database {}", fname.display())))
32            .expect("Failed to create vault file");
33        return self.clone();
34    }
35
36    pub fn new() -> Self {
37        Config {
38            clear_copy_timeout: 5,
39        }
40    }
41}