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
use config::{Config, ConfigError, Environment, File, FileFormat};
use directories::{ProjectDirs, UserDirs};
use serde::{Deserialize, Serialize};

static QUALIFIER: &str = "net";
static ORGANIZATION: &str = "upflitinglemma";
static APPLICATION: &str = "klondike-rs";

static CONFIG_FILE: &str = "config.toml";
static HOME_CONFIG_FILE: &str = ".klondike-rs.toml";

static ENV_PREFIX: &str = "klondike_";
static ENV_SEPARATOR: &str = "__";

#[derive(Default, Debug, Deserialize, Serialize)]
#[serde(default)]
pub struct Settings {
    pub display: DisplaySettings,
    pub game: GameSettings,
}

impl Settings {
    // TODO: Return a snafu-defined error type
    pub fn read_from_system() -> Result<Settings, ConfigError> {
        let mut config = Config::new();

        if let Some(user_dirs) = UserDirs::new() {
            let mut path = user_dirs.home_dir().to_path_buf();
            path.push(HOME_CONFIG_FILE);
            config.merge(File::from(path).format(FileFormat::Toml).required(false))?;
        }

        if let Some(project_dirs) = ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION) {
            let mut path = project_dirs.config_dir().to_path_buf();
            path.push(CONFIG_FILE);
            config.merge(File::from(path).format(FileFormat::Toml).required(false))?;
        }

        config.merge(Environment::with_prefix(ENV_PREFIX).separator(ENV_SEPARATOR))?;

        config.try_into()
    }
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(default)]
pub struct DisplaySettings {
    pub color: bool,
    pub unicode: bool,
}

impl Default for DisplaySettings {
    fn default() -> Self {
        DisplaySettings {
            color: true,
            unicode: true,
        }
    }
}

#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
pub enum DealerMode {
    AutoWin,
    InOrder,
    Random,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(default)]
pub struct GameSettings {
    pub dealer: DealerMode,
    pub draw_from_stock_len: usize,
    pub tableaux_len: u8,
    pub take_from_foundation: bool,
}

impl Default for GameSettings {
    fn default() -> Self {
        GameSettings {
            dealer: DealerMode::Random,
            draw_from_stock_len: 3,
            tableaux_len: 7,
            take_from_foundation: true,
        }
    }
}