From 3a7486e6b725bfdb8e2c511fcbf2bc2cb7ea595d Mon Sep 17 00:00:00 2001 From: Observer KRypt0n_ Date: Sat, 28 Jan 2023 19:54:53 +0200 Subject: [PATCH] config: changed some enums to use enum_ordinalize crate --- Cargo.toml | 4 +- .../enhancements/fps_unlocker/config/fps.rs | 4 +- .../fps_unlocker/config/window_mode.rs | 29 +--------- .../game/enhancements/fps_unlocker/mod.rs | 4 +- src/config/game/enhancements/hud.rs | 32 ++--------- src/config/game/wine/wine_lang.rs | 55 +------------------ src/config/game/wine/wine_sync.rs | 31 +---------- src/config/launcher/mod.rs | 16 ++---- src/config/resolution.rs | 32 +++++------ 9 files changed, 44 insertions(+), 163 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 45d202a..d7bf1c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,13 +14,15 @@ dirs = "4.0.0" serde = { version = "1.0", features = ["derive"], optional = true } serde_json = { version = "1.0", optional = true } +enum-ordinalize = { version = "3.1.12", optional = true } + wincompatlib = { version = "0.2.0", optional = true } lazy_static = { version = "1.4.0", optional = true } md5 = { version = "0.7.0", optional = true } [features] states = [] -config = ["dep:serde", "dep:serde_json"] +config = ["dep:serde", "dep:serde_json", "dep:enum-ordinalize"] components = ["dep:wincompatlib", "dep:lazy_static"] game = ["components", "config"] fps-unlocker = ["dep:md5"] diff --git a/src/config/game/enhancements/fps_unlocker/config/fps.rs b/src/config/game/enhancements/fps_unlocker/config/fps.rs index b9e605e..89ed290 100644 --- a/src/config/game/enhancements/fps_unlocker/config/fps.rs +++ b/src/config/game/enhancements/fps_unlocker/config/fps.rs @@ -46,6 +46,7 @@ impl Fps { 180 => Self::HundredEighty, 200 => Self::TwoHundred, 240 => Self::TwoHundredFourty, + num => Self::Custom(num) } } @@ -59,7 +60,8 @@ impl Fps { Self::HundredEighty => 180, Self::TwoHundred => 200, Self::TwoHundredFourty => 240, - Self::Custom(num) => *num + + Self::Custom(num) => *num } } } diff --git a/src/config/game/enhancements/fps_unlocker/config/window_mode.rs b/src/config/game/enhancements/fps_unlocker/config/window_mode.rs index 690696a..57d1910 100644 --- a/src/config/game/enhancements/fps_unlocker/config/window_mode.rs +++ b/src/config/game/enhancements/fps_unlocker/config/window_mode.rs @@ -1,7 +1,9 @@ use serde::{Serialize, Deserialize}; use serde_json::Value as JsonValue; -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +use enum_ordinalize::Ordinalize; + +#[derive(Ordinalize, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] pub enum WindowMode { None, Popup, @@ -19,28 +21,3 @@ impl From<&JsonValue> for WindowMode { serde_json::from_value(value.clone()).unwrap_or_default() } } - -impl TryFrom for WindowMode { - type Error = String; - - fn try_from(value: u32) -> Result { - match value { - 0 => Ok(Self::None), - 1 => Ok(Self::Popup), - 2 => Ok(Self::Fullscreen), - - _ => Err(String::from("Failed to convert number to WindowMode enum")) - } - } -} - -#[allow(clippy::from_over_into)] -impl Into for WindowMode { - fn into(self) -> u32 { - match self { - Self::None => 0, - Self::Popup => 1, - Self::Fullscreen => 2 - } - } -} diff --git a/src/config/game/enhancements/fps_unlocker/mod.rs b/src/config/game/enhancements/fps_unlocker/mod.rs index dfa43cc..abd4b88 100644 --- a/src/config/game/enhancements/fps_unlocker/mod.rs +++ b/src/config/game/enhancements/fps_unlocker/mod.rs @@ -8,9 +8,9 @@ use crate::consts::launcher_dir; pub mod config; pub mod prelude { - pub use super::config::Config; - pub use super::config::prelude::*; + + pub use super::config::Config; } use prelude::*; diff --git a/src/config/game/enhancements/hud.rs b/src/config/game/enhancements/hud.rs index 20b0d39..4ac14a7 100644 --- a/src/config/game/enhancements/hud.rs +++ b/src/config/game/enhancements/hud.rs @@ -1,11 +1,13 @@ -use std::collections::HashMap; - use serde::{Serialize, Deserialize}; use serde_json::Value as JsonValue; +use enum_ordinalize::Ordinalize; + +use std::collections::HashMap; + use crate::config::Config; -#[derive(Debug, Clone, Copy, Serialize, Deserialize)] +#[derive(Ordinalize, Debug, Clone, Copy, Serialize, Deserialize)] pub enum HUD { None, DXVK, @@ -24,30 +26,6 @@ impl From<&JsonValue> for HUD { } } -impl TryFrom for HUD { - type Error = String; - - fn try_from(value: u32) -> Result { - match value { - 0 => Ok(Self::None), - 1 => Ok(Self::DXVK), - 2 => Ok(Self::MangoHUD), - _ => Err(String::from("Failed to convert number to HUD enum")) - } - } -} - -#[allow(clippy::from_over_into)] -impl Into for HUD { - fn into(self) -> u32 { - match self { - Self::None => 0, - Self::DXVK => 1, - Self::MangoHUD => 2 - } - } -} - impl HUD { /// Get environment variables corresponding to used wine hud pub fn get_env_vars(&self, config: &Config) -> HashMap<&str, &str> { diff --git a/src/config/game/wine/wine_lang.rs b/src/config/game/wine/wine_lang.rs index 5707fb5..f70ab7c 100644 --- a/src/config/game/wine/wine_lang.rs +++ b/src/config/game/wine/wine_lang.rs @@ -3,7 +3,9 @@ use std::collections::HashMap; use serde::{Serialize, Deserialize}; use serde_json::Value as JsonValue; -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +use enum_ordinalize::Ordinalize; + +#[derive(Ordinalize, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] pub enum WineLang { System, English, @@ -30,58 +32,7 @@ impl From<&JsonValue> for WineLang { } } -impl TryFrom for WineLang { - type Error = String; - - fn try_from(value: u32) -> Result { - match value { - 0 => Ok(Self::System), - 1 => Ok(Self::English), - 2 => Ok(Self::Russian), - 3 => Ok(Self::German), - 4 => Ok(Self::Portuguese), - 5 => Ok(Self::Polish), - 6 => Ok(Self::French), - 7 => Ok(Self::Spanish), - 8 => Ok(Self::Chinese), - 9 => Ok(Self::Japanese), - 10 => Ok(Self::Korean), - - _ => Err(String::from("Failed to convert number to WineLang enum")) - } - } -} - -#[allow(clippy::from_over_into)] -impl Into for WineLang { - fn into(self) -> u32 { - for (i, lang) in Self::list().into_iter().enumerate() { - if lang == self { - return i as u32; - } - } - - unreachable!() - } -} - impl WineLang { - pub fn list() -> Vec { - vec![ - Self::System, - Self::English, - Self::Russian, - Self::German, - Self::Portuguese, - Self::Polish, - Self::French, - Self::Spanish, - Self::Chinese, - Self::Japanese, - Self::Korean - ] - } - /// Get environment variables corresponding to used wine language pub fn get_env_vars(&self) -> HashMap<&str, &str> { HashMap::from([("LANG", match self { diff --git a/src/config/game/wine/wine_sync.rs b/src/config/game/wine/wine_sync.rs index 183a834..d720c16 100644 --- a/src/config/game/wine/wine_sync.rs +++ b/src/config/game/wine/wine_sync.rs @@ -3,7 +3,9 @@ use std::collections::HashMap; use serde::{Serialize, Deserialize}; use serde_json::Value as JsonValue; -#[derive(Debug, Clone, Copy, Serialize, Deserialize)] +use enum_ordinalize::Ordinalize; + +#[derive(Ordinalize, Debug, Clone, Copy, Serialize, Deserialize)] pub enum WineSync { None, ESync, @@ -23,33 +25,6 @@ impl From<&JsonValue> for WineSync { } } -impl TryFrom for WineSync { - type Error = String; - - fn try_from(value: u32) -> Result { - match value { - 0 => Ok(Self::None), - 1 => Ok(Self::ESync), - 2 => Ok(Self::FSync), - 3 => Ok(Self::Futex2), - - _ => Err(String::from("Failed to convert number to WineSync enum")) - } - } -} - -#[allow(clippy::from_over_into)] -impl Into for WineSync { - fn into(self) -> u32 { - match self { - Self::None => 0, - Self::ESync => 1, - Self::FSync => 2, - Self::Futex2 => 3 - } - } -} - impl WineSync { /// Get environment variables corresponding to used wine sync pub fn get_env_vars(&self) -> HashMap<&str, &str> { diff --git a/src/config/launcher/mod.rs b/src/config/launcher/mod.rs index dc71170..1b9beb1 100644 --- a/src/config/launcher/mod.rs +++ b/src/config/launcher/mod.rs @@ -24,16 +24,12 @@ pub enum GameEdition { impl Default for GameEdition { fn default() -> Self { - let locale = match std::env::var("LC_ALL") { - Ok(locale) => locale, - Err(_) => match std::env::var("LC_MESSAGES") { - Ok(locale) => locale, - Err(_) => match std::env::var("LANG") { - Ok(locale) => locale, - Err(_) => return Self::Global - } - } - }; + #[allow(clippy::or_fun_call)] + let locale = std::env::var("LC_ALL").unwrap_or_else(|_| { + std::env::var("LC_MESSAGES").unwrap_or_else(|_| { + std::env::var("LANG").unwrap_or(String::from("en_us")) + }) + }); if locale.len() > 4 && &locale[..5].to_lowercase() == "zh_cn" { Self::China diff --git a/src/config/resolution.rs b/src/config/resolution.rs index 08058f8..b8ff30e 100644 --- a/src/config/resolution.rs +++ b/src/config/resolution.rs @@ -1,7 +1,5 @@ #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum Resolution { - Custom(u64, u64), - // qHD; 960x540 MiniHD, @@ -15,13 +13,14 @@ pub enum Resolution { QuadHD, // 3840x2160 - UltraHD + UltraHD, + + Custom(u64, u64) } impl Resolution { pub fn list() -> Vec { vec![ - Self::Custom(0, 0), Self::MiniHD, Self::HD, Self::FullHD, @@ -60,12 +59,13 @@ impl TryFrom for Resolution { fn try_from(value: u32) -> Result { match value { - 0 => Ok(Self::Custom(0, 0)), - 1 => Ok(Self::MiniHD), - 2 => Ok(Self::HD), - 3 => Ok(Self::FullHD), - 4 => Ok(Self::QuadHD), - 5 => Ok(Self::UltraHD), + 0 => Ok(Self::MiniHD), + 1 => Ok(Self::HD), + 2 => Ok(Self::FullHD), + 3 => Ok(Self::QuadHD), + 4 => Ok(Self::UltraHD), + + 5 => Ok(Self::Custom(0, 0)), _ => Err(String::from("Failed to convert number to Resolution enum")) } @@ -76,13 +76,13 @@ impl TryFrom for Resolution { impl Into for Resolution { fn into(self) -> u32 { match self { - Self::MiniHD => 1, - Self::HD => 2, - Self::FullHD => 3, - Self::QuadHD => 4, - Self::UltraHD => 5, + Self::MiniHD => 0, + Self::HD => 1, + Self::FullHD => 2, + Self::QuadHD => 3, + Self::UltraHD => 4, - _ => 0 // Custom resolution + _ => 5 // Custom resolution } } }