aboutsummaryrefslogtreecommitdiff
path: root/src/configuration.rs
blob: 5cce15e69aaa725eebe726f3913749734ffcd336 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use serde::{Deserialize, Serialize};
use serde_json::{from_str, to_string};
use std::env;
use std::fs::{File, create_dir_all, write};
use std::io::{Read, Result};
use std::path::PathBuf;

const CONFIG_ENV_VARIABLE: &str = "LYRICLI_CONFIG_DIRECTORY";
const CONFIG_DEFAULT_LOCATION: &str = "XDG_CONFIG_HOME";
const CONFIG_FALLBACK_LOCATION: &str = ".config";
const CONFIG_SUBDIRECTORY: &str = "lyricli";
const CONFIG_FILENAME: &str = "lyricli.conf";

#[derive(Serialize, Deserialize, Debug)]
pub struct Configuration {
    enabled_sources: Vec<String>,
}

impl Configuration {
    pub fn new() -> Self {
        if let Some(configuration) = Configuration::read() {
            return configuration;
        }

        let configuration = Configuration::default();

        // Write the defaults.
        Configuration::write(&configuration).ok();
        configuration
    }

    // Public API

    pub fn is_enabled(&self, source_name: &String) -> bool {
        self.enabled_sources.contains(source_name)
    }

    pub fn enable_source(&mut self, source_name: &String) -> Result<()> {
        if !self.enabled_sources.contains(source_name) {
            self.enabled_sources.push(source_name.to_owned())
        }
        Configuration::write(self)
    }

    pub fn disable_source(&mut self, source_name: &String) -> Result<()> {
        self.enabled_sources.retain(|source| source != source_name);
        Configuration::write(self)
    }

    // Helpers

    fn default() -> Configuration {
        Configuration {
            enabled_sources: vec![
                "apple_music".to_string(),
                "spotify".to_string(),
                "swinsian".to_string(),
                "mpris".to_string(),
            ],
        }
    }

    fn read() -> Option<Configuration> {
        let config_file_path = Configuration::file_path();

        let mut config_file = File::open(&config_file_path).ok()?;
        let mut config_contents = String::new();
        config_file.read_to_string(&mut config_contents).ok()?;
        from_str(&config_contents).ok()?
    }

    fn write(configuration: &Configuration) -> Result<()> {
        let config_file_path = Configuration::file_path();
        if let Ok(serialized_configuration) = to_string(&configuration) {
            write(&config_file_path, serialized_configuration)?;
        }
        Ok(())
    }

    fn file_path() -> PathBuf {
        let config_directory = Configuration::directory();
        create_dir_all(&config_directory).ok();
        config_directory.join(CONFIG_FILENAME)
    }

    fn directory() -> PathBuf {
        match env::var(CONFIG_ENV_VARIABLE) {
            Ok(directory) => PathBuf::from(directory),
            Err(_) => match env::var(CONFIG_DEFAULT_LOCATION) {
                Ok(directory) => PathBuf::from(directory),
                Err(_) => match env::var("HOME") {
                    Ok(directory) => PathBuf::from(directory).join(CONFIG_FALLBACK_LOCATION),
                    Err(_) => panic!(
                        "Could not find required directory, {CONFIG_ENV_VARIABLE} or {CONFIG_DEFAULT_LOCATION} should be set and readable."
                    ),
                },
            },
        }
        .join(CONFIG_SUBDIRECTORY)
    }
}