From c99c2b6989df65079655b66cb8c8ed0699a823d3 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Wed, 18 Dec 2024 19:49:36 -0600 Subject: use paths instead of dumb strings --- src/launcher.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/launcher.rs b/src/launcher.rs index 27fdf9c..82abef0 100644 --- a/src/launcher.rs +++ b/src/launcher.rs @@ -1,5 +1,5 @@ use std::{collections::{BTreeMap, HashMap}, error::Error, fmt::Write, io::ErrorKind}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use chrono::{DateTime, Utc}; use serde::{Serialize, Deserialize}; @@ -34,11 +34,13 @@ struct RemoteVersionList { } impl RemoteVersionList { - async fn new(home: &str) -> Result> { + async fn new(home: impl AsRef) -> Result> { let text = reqwest::get(URL_VERSION_MANIFEST).await?.error_for_status()?.text().await?; let manifest: VersionManifest = serde_json::from_str(text.as_str())?; - let fname = format!("{home}/.remote.json"); + let mut fname = home.as_ref().to_path_buf(); + fname.push(".version.json"); + let index: RemoteVersionIndex = match tokio::fs::read_to_string(fname).await { Ok(s) => serde_json::from_str(s.as_str())?, Err(e) => { @@ -71,11 +73,13 @@ impl LocalVersionList { serde_json::from_str(tokio::fs::read_to_string(path).await?)?; } - async fn load_versions(home: &str, skip: impl Fn(&str) -> bool) -> Result> { + async fn load_versions(home: impl AsRef, skip: impl Fn(&str) -> bool) -> Result> { let mut rd = tokio::fs::read_dir(home).await?; let versions = BTreeMap::new(); while let Some(ent) = rd.next_entry().await? { + if !ent.file_type().await?.is_dir() { continue; } + // when the code is fugly let path = match ent.file_name().to_str() { Some(s) => { @@ -124,7 +128,7 @@ struct VersionList { } impl VersionList { - pub async fn online(home: &str) -> Result> { + pub async fn online(home: impl AsRef) -> Result> { let remote = RemoteVersionList::new(home).await?; let local = LocalVersionList::load_versions(home, |s| remote.versions.contains_key(s)).await?; @@ -135,7 +139,7 @@ impl VersionList { }) } - pub async fn offline(home: &str) -> Result> { + pub async fn offline(home: impl AsRef) -> Result> { let local = LocalVersionList::load_versions(home, |_| false).await?; Ok(VersionList { -- cgit v1.2.3-70-g09d2