refactored: review findings - style and abstraction cleanup

- drop stale phase planning comments from launch_profile module headers.
 - simplify merge_libraries to HashSet<&str> (no string clones).
 - rewrite coord_key with match_indices (clearer intent).
 - read /proc/sys/kernel/osrelease on linux for mojang_os_version
   instead of shelling out to uname; return empty elsewhere.
 - add comment explaining why is_retryable excludes Parse.
 - shorten the launch_profile::... paths in launch/mod.rs with top-level
   use statements.
 - derive Default on LaunchProfile + nested types so test fixtures use
   ..Default::default() instead of listing every None field.
 - inline account_can_launch at its single call site, delete the helper
   and three tests that exercised trivially-correct one-line logic.
 - extract get_json_with_raw on HttpClient and save_profile_bytes in
   instance::loader; collapses three near-identical fetch_X_profile_with_raw
   helpers and three near-identical 'create dir + write bytes' blocks.
This commit is contained in:
objz committed 2026-05-25 19:05:48 +02:00
1 parent 412e6ff91a
commit 363ada8211
15 files changed
+155 -292

No files matched your search

+4 -5
View File
@@ -1,8 +1,7 @@
// foundation primitives for parsing, merging, and rendering mojang-format
// launch profiles. consumed by the vanilla launcher, forge/neoforge,
// fabric/quilt - anything that reads a mojang-style version JSON.
//
// phase 3 adds the `resolve` module that walks `inheritsFrom` chains.
// primitives for parsing, merging, and rendering mojang-format launch
// profiles. consumed by the vanilla launcher and the loader install paths
// (forge/neoforge/fabric/quilt) - anything that reads a mojang-style
// version JSON.
pub mod model;
pub mod render;
+8 -12
View File
@@ -4,16 +4,12 @@
// the fields we care about; unknown fields are silently dropped (serde
// default behavior) - which is fine because we write upstream JSON
// byte-for-byte on the install side.
//
// no consumers yet. phase 2 wires this into the vanilla launch path;
// phase 3 adds inheritsFrom resolution; phase 4 swaps the forge/neoforge
// installer profile writer over to use this shape.
use serde::{Deserialize, Serialize};
use super::rules::Rule;
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LaunchProfile {
pub id: String,
@@ -65,7 +61,7 @@ pub enum ArgumentValue {
Multiple(Vec<String>),
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct Library {
pub name: String,
pub downloads: Option<LibraryDownloads>,
@@ -73,12 +69,12 @@ pub struct Library {
pub url: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct LibraryDownloads {
pub artifact: Option<Artifact>,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct Artifact {
pub url: String,
pub path: String,
@@ -86,7 +82,7 @@ pub struct Artifact {
pub size: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct AssetIndex {
pub id: String,
pub url: String,
@@ -95,19 +91,19 @@ pub struct AssetIndex {
pub total_size: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct JavaVersion {
pub component: Option<String>,
pub major_version: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct VersionDownloads {
pub client: Download,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct Download {
pub url: String,
pub sha1: String,
+3 -14
View File
@@ -115,19 +115,8 @@ mod tests {
fn minimal_profile() -> LaunchProfile {
LaunchProfile {
id: "test".into(),
inherits_from: None,
main_class: Some("net.test.Main".into()),
libraries: Vec::new(),
arguments: None,
minecraft_arguments: None,
asset_index: None,
assets: None,
java_version: None,
downloads: None,
release_time: None,
time: None,
game_arguments: None,
type_: None,
..Default::default()
}
}
@@ -211,7 +200,7 @@ mod tests {
os: Some(OsCondition {
name: Some("osx".into()),
arch: None,
version: None,
..Default::default()
}),
features: None,
}],
@@ -252,7 +241,7 @@ mod tests {
os: Some(OsCondition {
name: Some("linux".into()),
arch: None,
version: None,
..Default::default()
}),
features: None,
}],
+7 -32
View File
@@ -67,16 +67,9 @@ pub fn merge_into(child: LaunchProfile, parent: LaunchProfile) -> LaunchProfile
fn coord_key(name: &str) -> &str {
// mojang maven coords are `group:artifact:version[:classifier]`. take
// everything up to the second colon.
let mut count = 0;
for (i, b) in name.bytes().enumerate() {
if b == b':' {
count += 1;
if count == 2 {
return &name[..i];
}
}
}
name
let mut it = name.match_indices(':').map(|(i, _)| i);
it.next();
it.next().map_or(name, |i| &name[..i])
}
// child entries take precedence over parent entries with the same
@@ -89,10 +82,7 @@ fn merge_libraries(
parent: Vec<crate::launch_profile::model::Library>,
) -> Vec<crate::launch_profile::model::Library> {
use std::collections::HashSet;
let child_keys: HashSet<String> = child
.iter()
.map(|l| coord_key(&l.name).to_string())
.collect();
let child_keys: HashSet<&str> = child.iter().map(|l| coord_key(&l.name)).collect();
let mut out: Vec<crate::launch_profile::model::Library> = parent
.into_iter()
@@ -164,28 +154,14 @@ mod tests {
fn empty_profile(id: &str) -> LaunchProfile {
LaunchProfile {
id: id.into(),
inherits_from: None,
main_class: None,
libraries: Vec::new(),
arguments: None,
minecraft_arguments: None,
asset_index: None,
assets: None,
java_version: None,
downloads: None,
release_time: None,
time: None,
game_arguments: None,
type_: None,
..Default::default()
}
}
fn lib(name: &str) -> Library {
Library {
name: name.into(),
downloads: None,
rules: None,
url: None,
..Default::default()
}
}
@@ -194,8 +170,7 @@ mod tests {
action: RuleAction::Allow,
os: Some(crate::launch_profile::rules::OsCondition {
name: Some("linux".into()),
arch: None,
version: None,
..Default::default()
}),
features: None,
}
+5 -5
View File
@@ -142,7 +142,7 @@ mod tests {
os: Some(OsCondition {
name: Some("linux".into()),
arch: None,
version: None,
..Default::default()
}),
features: None,
}];
@@ -158,7 +158,7 @@ mod tests {
os: Some(OsCondition {
name: Some("linux".into()),
arch: None,
version: None,
..Default::default()
}),
features: None,
}];
@@ -176,7 +176,7 @@ mod tests {
os: Some(OsCondition {
name: Some("windows".into()),
arch: None,
version: None,
..Default::default()
}),
features: None,
}];
@@ -198,7 +198,7 @@ mod tests {
os: Some(OsCondition {
name: Some("osx".into()),
arch: None,
version: None,
..Default::default()
}),
features: None,
},
@@ -229,7 +229,7 @@ mod tests {
os: Some(OsCondition {
name: Some("linux".into()),
arch: None,
version: None,
..Default::default()
}),
features: None,
},
+8 -20
View File
@@ -21,29 +21,17 @@ pub fn mojang_arch_name() -> &'static str {
// the host OS version string. mojang rules occasionally constrain natives
// selection on os.version with a regex (e.g. macOS 10.x-only natives).
// rust's stdlib doesn't expose this directly, so we synthesise something
// useful per-platform. on linux we read the kernel version; on macOS the
// product version is more useful but reading it requires shelling out so
// we fall back to the kernel version. on windows we use a similar
// approach. real-world profiles using os.version are rare; if this helper
// returns an empty string the rule evaluator treats version constraints
// as non-matching (defensive default).
// rust's stdlib doesn't expose this, so we read it where it's cheap and
// reliable: linux via /proc/sys/kernel/osrelease, other platforms return
// empty. when the host string is empty, version-gated rules don't match
// (conservative default in the rule evaluator) - which is fine because
// real-world profiles using os.version are vanishingly rare.
pub fn mojang_os_version() -> String {
#[cfg(unix)]
#[cfg(target_os = "linux")]
{
use std::process::Command;
if let Ok(out) = Command::new("uname").arg("-r").output()
&& out.status.success()
{
return String::from_utf8_lossy(&out.stdout).trim().to_string();
if let Ok(s) = std::fs::read_to_string("/proc/sys/kernel/osrelease") {
return s.trim().to_string();
}
}
#[cfg(windows)]
{
// crude - sufficient for the rare profile that needs windows version
// gating. real launchers use GetVersionEx via winapi; we can add
// that later if needed.
return std::env::var("OS").unwrap_or_default();
}
String::new()
}