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

+1 -4
View File
@@ -90,10 +90,7 @@ pub async fn fetch_fabric_profile_with_raw(
"{}/versions/loader/{}/{}/profile/json",
FABRIC_META_BASE, game_version, loader_version
);
let raw = client.get_bytes(&url).await?;
let parsed: FabricProfile = serde_json::from_slice(&raw)
.map_err(|e| NetError::Parse(format!("Failed to parse Fabric profile: {e}")))?;
Ok((parsed, raw))
client.get_json_with_raw(&url, "Fabric profile").await
}
// each fabric library entry has a maven coordinate and a base url.
+1 -5
View File
@@ -279,13 +279,9 @@ pub(crate) async fn install_forge_from_profile(
// and whitespace may differ from the original installer JSON because
// the source is a serde_json::Value (which doesn't preserve order),
// but no field is silently dropped.
let profiles_dir = meta_dir.join("loader-profiles");
std::fs::create_dir_all(&profiles_dir)?;
let profile_path = profiles_dir.join(profile_filename);
let serialized = serde_json::to_vec(version_info)
.map_err(|e| NetError::Parse(format!("Failed to serialize Forge profile: {e}")))?;
std::fs::write(&profile_path, &serialized)?;
crate::instance::loader::save_profile_bytes(meta_dir, profile_filename, &serialized)?;
Ok(())
}
+19 -1
View File
@@ -80,6 +80,21 @@ impl HttpClient {
)
.await
}
// fetch JSON and also keep the raw bytes. used by install paths that
// want both the parsed shape (for downloading libraries from it) and
// the original bytes (to write byte-for-byte to the loader-profiles
// cache, so any field we don't know about survives).
pub async fn get_json_with_raw<T: DeserializeOwned>(
&self,
url: &str,
label: &str,
) -> Result<(T, Vec<u8>), NetError> {
let raw = self.get_bytes(url).await?;
let parsed: T = serde_json::from_slice(&raw)
.map_err(|e| NetError::Parse(format!("Failed to parse {label}: {e}")))?;
Ok((parsed, raw))
}
}
// shared retry envelope around `client.get(url).await? -> decode`. retries
@@ -183,7 +198,10 @@ async fn download_file_once(
Ok(())
}
// body decode errors and timeouts are worth retrying, but a 404 or disk error isn't
// body decode errors and timeouts are worth retrying, but a 404 or disk
// error isn't. Parse errors stay non-retryable: by the time we hit one
// the response body has fully arrived, so the failure means the upstream
// returned malformed JSON - retrying won't fix that.
fn is_retryable(err: &NetError) -> bool {
match err {
NetError::Http(e) => e.is_timeout() || e.is_body() || e.is_connect(),
+1 -4
View File
@@ -121,10 +121,7 @@ pub async fn fetch_version_meta_with_raw(
client: &HttpClient,
entry: &VersionEntry,
) -> Result<(VersionMeta, Vec<u8>), NetError> {
let raw = client.get_bytes(&entry.url).await?;
let parsed: VersionMeta = serde_json::from_slice(&raw)
.map_err(|e| NetError::Parse(format!("Failed to parse version meta: {e}")))?;
Ok((parsed, raw))
client.get_json_with_raw(&entry.url, "version meta").await
}
pub async fn download_client_jar(
+1 -4
View File
@@ -87,10 +87,7 @@ pub async fn fetch_quilt_profile_with_raw(
"{}/versions/loader/{}/{}/profile/json",
QUILT_META_BASE, game_version, loader_version
);
let raw = client.get_bytes(&url).await?;
let parsed: QuiltProfile = serde_json::from_slice(&raw)
.map_err(|e| NetError::Parse(format!("Failed to parse Quilt profile: {e}")))?;
Ok((parsed, raw))
client.get_json_with_raw(&url, "Quilt profile").await
}
pub async fn download_quilt_libraries(