fix(settings): normalize persisted values
This commit is contained in:
1 parent
9a408769c5
commit
ccc09419fb
3 files changed
+77
-14
No files matched your search
+5
-10
@@ -60,15 +60,6 @@ pub enum ContentProvider {
|
|||||||
CurseForge,
|
CurseForge,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ContentProvider {
|
|
||||||
pub fn as_str(self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
Self::Modrinth => "modrinth",
|
|
||||||
Self::CurseForge => "curseforge",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for ContentProvider {
|
impl fmt::Display for ContentProvider {
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
@@ -379,7 +370,11 @@ impl Config {
|
|||||||
.memory_max
|
.memory_max
|
||||||
.clone_from(&self.defaults.memory_min);
|
.clone_from(&self.defaults.memory_min);
|
||||||
}
|
}
|
||||||
if self.defaults.resolution.is_none() {
|
if self
|
||||||
|
.defaults
|
||||||
|
.resolution
|
||||||
|
.is_none_or(|(width, height)| width == 0 || height == 0)
|
||||||
|
{
|
||||||
self.defaults.resolution = default_resolution();
|
self.defaults.resolution = default_resolution();
|
||||||
}
|
}
|
||||||
self.ui.error_auto_dismiss_ms = self.ui.error_auto_dismiss_ms.max(1);
|
self.ui.error_auto_dismiss_ms = self.ui.error_auto_dismiss_ms.max(1);
|
||||||
|
|||||||
+30
-4
@@ -77,7 +77,7 @@ pub struct InstanceConfig {
|
|||||||
pub created: DateTime<Utc>,
|
pub created: DateTime<Utc>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub last_played: Option<DateTime<Utc>>,
|
pub last_played: Option<DateTime<Utc>>,
|
||||||
#[serde(default)]
|
#[serde(default, deserialize_with = "deserialize_optional_non_empty_string")]
|
||||||
pub java_path: Option<String>,
|
pub java_path: Option<String>,
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_memory")]
|
#[serde(default, deserialize_with = "deserialize_optional_memory")]
|
||||||
pub memory_max: Option<String>,
|
pub memory_max: Option<String>,
|
||||||
@@ -91,17 +91,25 @@ pub struct InstanceConfig {
|
|||||||
pub window_mode: WindowMode,
|
pub window_mode: WindowMode,
|
||||||
#[serde(default, skip_serializing_if = "is_false")]
|
#[serde(default, skip_serializing_if = "is_false")]
|
||||||
pub inherit_window_mode: bool,
|
pub inherit_window_mode: bool,
|
||||||
#[serde(default)]
|
#[serde(default, deserialize_with = "deserialize_optional_resolution")]
|
||||||
pub resolution: Option<(u32, u32)>,
|
pub resolution: Option<(u32, u32)>,
|
||||||
#[serde(default, skip_serializing_if = "is_false")]
|
#[serde(default, skip_serializing_if = "is_false")]
|
||||||
pub inherit_resolution: bool,
|
pub inherit_resolution: bool,
|
||||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
#[serde(
|
||||||
|
default,
|
||||||
|
deserialize_with = "deserialize_optional_non_empty_string",
|
||||||
|
skip_serializing_if = "Option::is_none"
|
||||||
|
)]
|
||||||
pub preferred_account: Option<String>,
|
pub preferred_account: Option<String>,
|
||||||
#[serde(default, skip_serializing_if = "LaunchCommand::is_default")]
|
#[serde(default, skip_serializing_if = "LaunchCommand::is_default")]
|
||||||
pub pre_launch_command: LaunchCommand,
|
pub pre_launch_command: LaunchCommand,
|
||||||
#[serde(default, skip_serializing_if = "LaunchCommand::is_default")]
|
#[serde(default, skip_serializing_if = "LaunchCommand::is_default")]
|
||||||
pub post_exit_command: LaunchCommand,
|
pub post_exit_command: LaunchCommand,
|
||||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
#[serde(
|
||||||
|
default,
|
||||||
|
deserialize_with = "deserialize_optional_non_empty_string",
|
||||||
|
skip_serializing_if = "Option::is_none"
|
||||||
|
)]
|
||||||
pub glfw_path: Option<String>,
|
pub glfw_path: Option<String>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub config_sync_profile: Option<String>,
|
pub config_sync_profile: Option<String>,
|
||||||
@@ -131,6 +139,24 @@ fn is_false(value: &bool) -> bool {
|
|||||||
!value
|
!value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn deserialize_optional_resolution<'de, D>(deserializer: D) -> Result<Option<(u32, u32)>, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
Ok(Option::<(u32, u32)>::deserialize(deserializer)?
|
||||||
|
.filter(|(width, height)| *width > 0 && *height > 0))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_optional_non_empty_string<'de, D>(
|
||||||
|
deserializer: D,
|
||||||
|
) -> Result<Option<String>, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
Ok(Option::<String>::deserialize(deserializer)?
|
||||||
|
.and_then(|value| (!value.trim().is_empty()).then(|| value.trim().to_owned())))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse_resolution(input: &str) -> Result<(u32, u32), String> {
|
pub fn parse_resolution(input: &str) -> Result<(u32, u32), String> {
|
||||||
let (width, height) = input
|
let (width, height) = input
|
||||||
.trim()
|
.trim()
|
||||||
|
|||||||
@@ -135,6 +135,48 @@ fn instance_config_ignores_invalid_memory_values() {
|
|||||||
assert_eq!(parsed.memory_min, None);
|
assert_eq!(parsed.memory_min, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn instance_config_normalizes_optional_paths_and_resolution() {
|
||||||
|
let json = r#"
|
||||||
|
{
|
||||||
|
"name": "test",
|
||||||
|
"game_version": "1.21.1",
|
||||||
|
"loader": "vanilla",
|
||||||
|
"loader_version": null,
|
||||||
|
"created": "2026-04-20T18:04:25.567993893Z",
|
||||||
|
"java_path": " ",
|
||||||
|
"glfw_path": " /usr/lib/libglfw.so ",
|
||||||
|
"preferred_account": "",
|
||||||
|
"resolution": [0, 1080]
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
|
||||||
|
let parsed: InstanceConfig = serde_json::from_str(json).expect("deserialize");
|
||||||
|
|
||||||
|
assert!(parsed.java_path.is_none());
|
||||||
|
assert_eq!(parsed.glfw_path.as_deref(), Some("/usr/lib/libglfw.so"));
|
||||||
|
assert!(parsed.preferred_account.is_none());
|
||||||
|
assert!(parsed.resolution.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn instance_config_rejects_zero_resolution_height() {
|
||||||
|
let json = r#"
|
||||||
|
{
|
||||||
|
"name": "test",
|
||||||
|
"game_version": "1.21.1",
|
||||||
|
"loader": "vanilla",
|
||||||
|
"loader_version": null,
|
||||||
|
"created": "2026-04-20T18:04:25.567993893Z",
|
||||||
|
"resolution": [1920, 0]
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
|
||||||
|
let parsed: InstanceConfig = serde_json::from_str(json).expect("deserialize");
|
||||||
|
|
||||||
|
assert!(parsed.resolution.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn normalize_memory_value_rejects_invalid_values() {
|
fn normalize_memory_value_rejects_invalid_values() {
|
||||||
assert_eq!(normalize_memory_value("0"), None);
|
assert_eq!(normalize_memory_value("0"), None);
|
||||||
|
|||||||
Reference in new issue
Block a user