aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
blob: 67721b4c7cf6a7b2f11d9d9548390c406ad56231 (plain)
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
use directories::{ProjectDirs, UserDirs};
use serde::{Deserialize, Serialize};
use std::{collections::HashSet, fs};

use expanduser::expanduser;

#[derive(Serialize, Deserialize)]
pub struct Config {
    pub music_root: String,
    pub audio_exts: HashSet<String>,
    pub skip_window_secs: u64,
    pub temperature: f32,
    pub low_weight: f32,
    pub mid_weight: f32,
    pub high_weight: f32,
    pub max_weight: f32,
}

fn default_config_path() -> String {
    let name = "mood.json";
    ProjectDirs::from("qualifier", "organisation", "mood")
        .and_then(|pd| {
            pd.config_dir()
                .join(name)
                .to_str()
                .map(std::string::ToString::to_string)
        })
        .unwrap_or(format!("~/.config/mood/{name}"))
}

impl Config {
    pub fn try_load() -> Result<Config, String> {
        expanduser(default_config_path())
            .map_err(|e| e.to_string())
            .and_then(|p| fs::read_to_string(p).map_err(|e| e.to_string()))
            .and_then(|contents| serde_json::from_str(&contents).map_err(|e| e.to_string()))
    }

    pub fn try_save(&self) -> Result<(), String> {
        let path = expanduser(default_config_path()).map_err(|e| e.to_string())?;
        _ = path.parent().map(std::fs::create_dir_all);
        let serialised = serde_json::to_string_pretty(self)
            .map_err(|e| format!("Failed to serialize config: {e}"))?;
        fs::write(&path, serialised).map_err(|e| format!("Failed to write config file: {e}"))?;
        Ok(())
    }
}

impl Default for Config {
    fn default() -> Config {
        Config {
            music_root: UserDirs::new()
                .and_then(|u| {
                    u.audio_dir()
                        .and_then(|p| p.to_str().map(std::string::ToString::to_string))
                })
                .unwrap_or("~/Music".to_string()),
            audio_exts: ["mp3", "flac", "wav", "m4a", "ogg", "vorbis", "mp4"]
                .into_iter()
                .map(String::from)
                .collect(),
            skip_window_secs: 60,
            temperature: 0.6,
            low_weight: 0.25,
            mid_weight: 0.5,
            high_weight: 0.75,
            max_weight: 1.0,
        }
    }
}