Use defaultconfig instead of default on structs

This commit is contained in:
Philip Kristoffersen
2021-09-25 12:19:07 +02:00
parent 993251156d
commit f68db38a7f
6 changed files with 30 additions and 69 deletions
+4
View File
@@ -0,0 +1,4 @@
debug= false
[steamgrid_db]
enabled = true
-20
View File
@@ -6,24 +6,4 @@ pub struct EpicGamesLauncherSettings {
pub location: Option<String>, pub location: Option<String>,
} }
impl Default for EpicGamesLauncherSettings {
fn default() -> Self {
// On windows
// Even if no path is given, we can try to guess, so lets enable by default
#[cfg(target_os = "windows")]
let enabled = true;
// On linux
// We can not guess, so lets not enable by default
#[cfg(target_os = "linux")]
let enabled = false;
EpicGamesLauncherSettings {
enabled,
location: None,
}
}
}
+1 -10
View File
@@ -4,13 +4,4 @@ use serde::Deserialize;
pub struct LegendarySettings { pub struct LegendarySettings {
pub enabled: bool, pub enabled: bool,
pub executable: Option<String>, pub executable: Option<String>,
} }
impl Default for LegendarySettings {
fn default() -> Self {
Self {
enabled: true,
executable: None,
}
}
}
+6
View File
@@ -84,8 +84,14 @@ where
{ {
if platform.enabled() { if platform.enabled() {
let shortcuts_to_add_result = platform.get_shortcuts(); let shortcuts_to_add_result = platform.get_shortcuts();
match shortcuts_to_add_result { match shortcuts_to_add_result {
Ok(shortcuts_to_add) => { Ok(shortcuts_to_add) => {
println!(
"Found {} games for platform {}",
shortcuts_to_add.len(),
platform.name()
);
current_shortcuts.retain(|f| !f.tags.contains(&platform.name().to_owned())); current_shortcuts.retain(|f| !f.tags.contains(&platform.name().to_owned()));
for shortcut in shortcuts_to_add { for shortcut in shortcuts_to_add {
let shortcut_owned: ShortcutOwned = shortcut.into(); let shortcut_owned: ShortcutOwned = shortcut.into();
+18 -29
View File
@@ -4,9 +4,9 @@ use crate::{
use config::{Config, ConfigError, Environment, File}; use config::{Config, ConfigError, Environment, File};
use serde::Deserialize; use serde::Deserialize;
use std::{env, path::Path}; use std::{env};
#[derive(Debug, Deserialize, Default)] #[derive(Debug, Deserialize)]
pub struct Settings { pub struct Settings {
pub debug: bool, pub debug: bool,
pub epic_games: EpicGamesLauncherSettings, pub epic_games: EpicGamesLauncherSettings,
@@ -14,35 +14,27 @@ pub struct Settings {
pub steamgrid_db: SteamGridDbSettings, pub steamgrid_db: SteamGridDbSettings,
} }
#[derive(Debug, Deserialize, Default)]
struct SettingsOptional {
pub debug: Option<bool>,
pub epic_games: Option<EpicGamesLauncherSettings>,
pub legendary: Option<LegendarySettings>,
pub steamgrid_db: Option<SteamGridDbSettings>,
}
impl From<SettingsOptional> for Settings {
fn from(optional: SettingsOptional) -> Self {
Self {
debug: optional.debug.unwrap_or(false),
epic_games: optional.epic_games.unwrap_or(Default::default()),
legendary: optional.legendary.unwrap_or(Default::default()),
steamgrid_db: optional.steamgrid_db.unwrap_or(Default::default()),
}
}
}
//https://github.com/JosefNemec/Playnite/tree/master/source/Plugins/OriginLibrary //https://github.com/JosefNemec/Playnite/tree/master/source/Plugins/OriginLibrary
//https://github.com/JosefNemec/Playnite/blob/master/source/Plugins/OriginLibrary/Origin.cs#L109 //https://github.com/JosefNemec/Playnite/blob/master/source/Plugins/OriginLibrary/Origin.cs#L109
impl Settings { impl Settings {
pub fn new() -> Result<Self, ConfigError> { pub fn new() -> Result<Self, ConfigError> {
let default = Self::default();
if !Path::new("config.toml").exists() {
return Ok(default);
}
let mut s = Config::new(); let mut s = Config::new();
let default_str = include_str!("defaultconfig.toml");
s.merge(File::from_str(default_str, config::FileFormat::Toml))?;
let enable_legendary = true;
let enable_epic = false;
#[cfg(target_os = "windows")]
let enable_legendary = false;
#[cfg(target_os = "windows")]
let enable_epic = true;
s.set_default("legendary.enabled", enable_legendary)?;
s.set_default("epic_games.enabled", enable_epic)?;
// Start off by merging in the "default" configuration file // Start off by merging in the "default" configuration file
s.merge(File::with_name("config.toml").required(false))?; s.merge(File::with_name("config.toml").required(false))?;
@@ -60,9 +52,6 @@ impl Settings {
// Eg.. `STEAM_SYNC_DEBUG=1 ./target/app` would set the `debug` key // Eg.. `STEAM_SYNC_DEBUG=1 ./target/app` would set the `debug` key
s.merge(Environment::with_prefix("steam_sync"))?; s.merge(Environment::with_prefix("steam_sync"))?;
// You can deserialize (and thus freeze) the entire configuration as s.try_into()
let optionals: SettingsOptional = s.try_into()?;
Ok(optionals.into())
} }
} }
+1 -10
View File
@@ -4,13 +4,4 @@ use serde::Deserialize;
pub struct SteamGridDbSettings { pub struct SteamGridDbSettings {
pub enabled: bool, pub enabled: bool,
pub auth_key: Option<String>, pub auth_key: Option<String>,
} }
impl Default for SteamGridDbSettings {
fn default() -> Self {
Self {
enabled: true,
auth_key: None,
}
}
}