Add better defaults support

Former-commit-id: fa77acae3ee2b99cbf37a8fec71ee1913e889d82
This commit is contained in:
Louis Pearson 2019-04-16 11:41:39 -06:00
parent 515039c62d
commit 780e034674

View File

@ -7,9 +7,11 @@ use glutin::VirtualKeyCode;
use toml; use toml;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::default::Default;
/// Settings contains everything that can be configured in the Settings.toml file /// Settings contains everything that can be configured in the Settings.toml file
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct Settings { pub struct Settings {
pub controls: ControlSettings, pub controls: ControlSettings,
} }
@ -35,8 +37,8 @@ pub struct ControlSettings {
pub interface: VirtualKeyCode, pub interface: VirtualKeyCode,
} }
impl Settings { impl Default for Settings {
pub fn default() -> Self { fn default() -> Self {
Settings { Settings {
controls: ControlSettings { controls: ControlSettings {
toggle_cursor: VirtualKeyCode::Tab, toggle_cursor: VirtualKeyCode::Tab,
@ -53,14 +55,18 @@ impl Settings {
social: VirtualKeyCode::O, social: VirtualKeyCode::O,
spellbook: VirtualKeyCode::P, spellbook: VirtualKeyCode::P,
settings: VirtualKeyCode::N, settings: VirtualKeyCode::N,
help: VirtualKeyCode::F2, help: VirtualKeyCode::F1,
interface: VirtualKeyCode::F2, interface: VirtualKeyCode::F2,
}, },
} }
} }
}
impl Settings {
pub fn load() -> Result<Self, ConfigError> { pub fn load() -> Result<Self, ConfigError> {
let mut config = Config::new(); let mut config = Config::new();
config.merge(Config::try_from(&Settings::default())?);
config.merge(config::File::with_name("settings"))?; config.merge(config::File::with_name("settings"))?;
config.try_into() config.try_into()
} }