1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crate::prefs::PrefError;
use crate::prefs::PrefError::*;
use directories::ProjectDirs;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::collections::HashMap;
use std::fs;
use std::fs::remove_file;
use std::path::PathBuf;

#[derive(Clone, Debug)]
pub struct Preferences<T>
where
    T: Serialize + DeserializeOwned,
{
    data: HashMap<String, T>,
    file: PathBuf,
}

impl<T: Serialize + DeserializeOwned> Preferences<T> {
    pub fn new(mut path: PathBuf, filename: &str) -> Self {
        path.push(filename);
        Preferences {
            data: HashMap::new(),
            file: path,
        }
    }
}

impl<T: Serialize + DeserializeOwned> Preferences<T> {
    pub fn load(&mut self) -> Result<(), PrefError> {
        let json_str = if self.file.exists() {
            fs::read_to_string(&self.file)
                .map_err(|err| Loading(err.to_string(), self.file.to_string_lossy().to_string()))?
        } else {
            String::from("{}")
        };
        self.data =
            serde_json::from_str(&json_str).map_err(|err| Deserializing(err.to_string()))?;

        Ok(())
    }

    pub fn save(&self) -> Result<(), PrefError> {
        let json_str =
            serde_json::to_string(&self.data).map_err(|err| Serializing(err.to_string()))?;
        fs::write(&self.file, json_str)
            .map_err(|err| Saving(err.to_string(), self.file.to_string_lossy().to_string()))?;

        Ok(())
    }

    pub fn get(&self, key: &str) -> Option<&T> {
        self.data.get(key)
    }

    pub fn set(&mut self, key: &str, value: T) {
        self.data.insert(key.to_owned(), value);
    }

    pub fn clear(&mut self, key: &str) {
        self.data.remove(key);
    }

    pub fn delete_file(&self) -> bool {
        remove_file(&self.file).is_ok()
    }
}

pub fn get_pref_dir(
    qualifier: &str,
    organization: &str,
    application: &str,
) -> Result<PathBuf, PrefError> {
    return if let Some(dir) = ProjectDirs::from(qualifier, organization, application) {
        let path = dir.preference_dir().to_path_buf();
        fs::create_dir_all(path.clone())
            .map_err(|err| MakingDirs(err.to_string(), path.to_string_lossy().to_string()))?;
        Ok(path)
    } else {
        Err(AppPrefDir)
    };
}