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
use serde::Deserialize;
use spitfire_glow::app::AppConfig;
use std::{error::Error, path::Path};

#[derive(Debug, Deserialize)]
pub struct Config {
    #[serde(default = "Config::default_width")]
    pub width: u32,
    #[serde(default = "Config::default_height")]
    pub height: u32,
    #[serde(default = "Config::default_fullscreen")]
    pub fullscreen: bool,
    #[serde(default = "Config::default_maximized")]
    pub maximized: bool,
    #[serde(default = "Config::default_vsync")]
    pub vsync: bool,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            width: Self::default_width(),
            height: Self::default_height(),
            fullscreen: Self::default_fullscreen(),
            maximized: Self::default_maximized(),
            vsync: Self::default_vsync(),
        }
    }
}

impl Config {
    fn default_width() -> u32 {
        1024
    }

    fn default_height() -> u32 {
        576
    }

    fn default_fullscreen() -> bool {
        true
    }

    fn default_maximized() -> bool {
        false
    }

    fn default_vsync() -> bool {
        true
    }

    pub fn load(path: impl AsRef<Path>) -> Result<Self, Box<dyn Error>> {
        Ok(toml::from_str(&std::fs::read_to_string(path)?)?)
    }

    pub fn to_app_config(&self, name: impl ToString) -> AppConfig {
        AppConfig {
            title: name.to_string(),
            width: self.width,
            height: self.height,
            fullscreen: self.fullscreen,
            maximized: self.maximized,
            vsync: self.vsync,
            color: [0.0, 0.0, 0.0],
            ..Default::default()
        }
    }
}