Replace all .unwrap() statements with proper code with Claude Code

This commit is contained in:
2026-02-17 00:39:20 -07:00
parent e7e624f042
commit 513f1a0c16

View File

@@ -27,11 +27,10 @@ use std::os::unix::fs::PermissionsExt;
#[allow(unused)] #[allow(unused)]
fn is_running_by_path(path: &Path) -> bool { fn is_running_by_path(path: &Path) -> bool {
let sys = System::new_all(); let sys = System::new_all();
let target = path.canonicalize().ok(); let target = match path.canonicalize() {
if target.is_none() { Ok(t) => t,
return false; Err(_) => return false,
} };
let target = target.unwrap();
sys.processes().values().any(|proc| { sys.processes().values().any(|proc| {
proc.exe() proc.exe()
.and_then(|exe| exe.canonicalize().ok()) .and_then(|exe| exe.canonicalize().ok())
@@ -95,12 +94,10 @@ async fn unzip_to_dir(zip_path: PathBuf, out_dir: PathBuf) -> String {
#[tauri::command] #[tauri::command]
fn folder_size(app: AppHandle, version: String) -> String { fn folder_size(app: AppHandle, version: String) -> String {
let path = app let path = match app.path().app_local_data_dir() {
.path() Ok(p) => p.join("game").join(&version),
.app_local_data_dir() Err(_) => return "-1".to_string(),
.unwrap() };
.join("game")
.join(&version);
fn inner(path: &Path) -> u64 { fn inner(path: &Path) -> u64 {
let mut size = 0; let mut size = 0;
if let Ok(entries) = fs::read_dir(path) { if let Ok(entries) = fs::read_dir(path) {
@@ -146,8 +143,14 @@ async fn download(
let mut downloaded: u64 = 0; let mut downloaded: u64 = 0;
let mut stream = resp.bytes_stream(); let mut stream = resp.bytes_stream();
let downloads_path = app.path().app_local_data_dir().unwrap().join("downloads"); let downloads_path = match app.path().app_local_data_dir() {
let game_path = app.path().app_local_data_dir().unwrap().join("game"); Ok(p) => p.join("downloads"),
Err(_) => return "-1".to_string(),
};
let game_path = match app.path().app_local_data_dir() {
Ok(p) => p.join("game"),
Err(_) => return "-1".to_string(),
};
let download_part_path = downloads_path.join(format!("{}.part", name)); let download_part_path = downloads_path.join(format!("{}.part", name));
let download_zip_path = downloads_path.join(format!("{}.zip", name)); let download_zip_path = downloads_path.join(format!("{}.zip", name));
@@ -161,9 +164,10 @@ async fn download(
let _ = tokio::fs::remove_dir_all(&game_path.join(name.clone())).await; let _ = tokio::fs::remove_dir_all(&game_path.join(name.clone())).await;
} }
let _ = tokio::fs::create_dir_all(&game_path.join(&name)).await; let _ = tokio::fs::create_dir_all(&game_path.join(&name)).await;
let mut file = tokio::fs::File::create(download_part_path.clone()) let mut file = match tokio::fs::File::create(download_part_path.clone()).await {
.await Ok(f) => f,
.unwrap(); Err(_) => return "-1".to_string(),
};
let start = Instant::now(); let start = Instant::now();
@@ -195,32 +199,34 @@ async fn download(
0.0 0.0
}; };
app.emit( let _ = app.emit(
"download-progress", "download-progress",
format!( format!(
"{}:{:.8}:{}:{}:{:.2}", "{}:{:.8}:{}:{}:{:.2}",
&name, progress, downloaded, speed, eta_secs &name, progress, downloaded, speed, eta_secs
), ),
) );
.unwrap();
} }
if total_size > 0 && downloaded < total_size { if total_size > 0 && downloaded < total_size {
return "-1".to_string(); return "-1".to_string();
} }
app.emit("download-hash-checking", format!("{}", &name)) let _ = app.emit("download-hash-checking", format!("{}", &name));
.unwrap();
let download_hash = { let download_hash = {
let mut file = tokio::fs::File::open(download_part_path.clone()) let mut file = match tokio::fs::File::open(download_part_path.clone()).await {
.await Ok(f) => f,
.unwrap(); Err(_) => return "-1".to_string(),
};
let mut hasher = Sha512::new(); let mut hasher = Sha512::new();
{ {
let mut buffer = [0; 8192]; let mut buffer = [0; 8192];
loop { loop {
let bytes_read = file.read(&mut buffer).await.unwrap(); let bytes_read = match file.read(&mut buffer).await {
Ok(n) => n,
Err(_) => return "-1".to_string(),
};
if bytes_read == 0 { if bytes_read == 0 {
break; break;
} }
@@ -232,21 +238,16 @@ async fn download(
}; };
if hash != download_hash { if hash != download_hash {
tokio::fs::remove_file(download_part_path.clone()) let _ = tokio::fs::remove_file(download_part_path.clone()).await;
.await
.unwrap();
return "-1".to_string(); return "-1".to_string();
} }
app.emit("download-finishing", format!("{}", &name)) let _ = app.emit("download-finishing", format!("{}", &name));
.unwrap();
tokio::fs::rename(download_part_path.clone(), download_zip_path.clone()) if let Err(_) = tokio::fs::rename(download_part_path.clone(), download_zip_path.clone()).await {
.await return "-1".to_string();
.unwrap(); }
let unzip_res = unzip_to_dir(download_zip_path.clone(), game_path.join(&name)).await; let unzip_res = unzip_to_dir(download_zip_path.clone(), game_path.join(&name)).await;
tokio::fs::remove_file(download_zip_path.clone()) let _ = tokio::fs::remove_file(download_zip_path.clone()).await;
.await
.unwrap();
if unzip_res == "-1" { if unzip_res == "-1" {
return "-1".to_string(); return "-1".to_string();
} }
@@ -264,12 +265,10 @@ fn launch_game(
use_wine: bool, use_wine: bool,
wine_command: String, wine_command: String,
) { ) {
let game_folder = app let game_folder = match app.path().app_local_data_dir() {
.path() Ok(p) => p.join("game").join(&name),
.app_local_data_dir() Err(_) => return,
.unwrap() };
.join("game")
.join(&name);
if !game_folder.exists() { if !game_folder.exists() {
return; return;
} }
@@ -301,28 +300,31 @@ fn launch_game(
let quoted_path = format!("\"{}\"", exe_path.to_string_lossy()); let quoted_path = format!("\"{}\"", exe_path.to_string_lossy());
let cmd = wine_command.replace("%path%", &quoted_path); let cmd = wine_command.replace("%path%", &quoted_path);
Command::new("bash") if let Err(_) = Command::new("bash")
.arg("-c") .arg("-c")
.arg(cmd) .arg(cmd)
.current_dir(&game_folder) .current_dir(&game_folder)
.spawn() .spawn() {
.unwrap(); eprintln!("Failed to launch game with Wine");
}
return; return;
} }
} }
if platform() == "macos" { if platform() == "macos" {
Command::new("open") if let Err(_) = Command::new("open")
.arg(&executable) .arg(&executable)
.current_dir(&game_folder) .current_dir(&game_folder)
.spawn() .spawn() {
.unwrap(); eprintln!("Failed to launch game on macOS");
}
} else { } else {
Command::new(&exe_path) if let Err(_) = Command::new(&exe_path)
.current_dir(&game_folder) .current_dir(&game_folder)
.spawn() .spawn() {
.unwrap(); eprintln!("Failed to launch game");
}
} }
} }
@@ -350,10 +352,9 @@ pub fn run() {
.plugin(tauri_plugin_clipboard_manager::init()) .plugin(tauri_plugin_clipboard_manager::init())
.plugin(tauri_plugin_notification::init()) .plugin(tauri_plugin_notification::init())
.plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| { .plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| {
let _ = app if let Some(window) = app.get_webview_window("main") {
.get_webview_window("main") let _ = window.set_focus();
.expect("no main window") }
.set_focus();
})) }))
.plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_fs::init())
@@ -364,8 +365,11 @@ pub fn run() {
.setup(|app| { .setup(|app| {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ {
let main_window = app.get_webview_window("main").unwrap(); if let Some(main_window) = app.get_webview_window("main") {
main_window.create_overlay_titlebar().unwrap(); if let Err(e) = main_window.create_overlay_titlebar() {
eprintln!("Failed to create overlay titlebar: {:?}", e);
}
}
} }
Ok(()) Ok(())
}) })