Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@tauri-apps/api": "^2.10.1",
"@tauri-apps/plugin-opener": "^2.5.3",
"lucide-svelte": "^0.474.0"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ zip = "2"
sevenz-rust = "0.6"
unrar = "0.5.8"
urlencoding = "2"
walkdir = "2.5.0"
thiserror = "1"

[dev-dependencies]
tempfile = "3"
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/commands/browse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub async fn browse_install_mod(
author: mod_author,
};

let result = crate::services::installer::install_with_metadata(&archive_path, &dir, &metadata);
let result = crate::services::installer::install(&archive_path, &dir, Some(&metadata));
let _ = std::fs::remove_file(&archive_path);
result
}
9 changes: 3 additions & 6 deletions src-tauri/src/commands/logs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use tauri::State;

use crate::errors::AppError;
use crate::errors::{AppError, Context};
use crate::state::AppState;

#[tauri::command]
Expand All @@ -17,10 +17,7 @@ pub async fn read_log_file(state: State<'_, AppState>) -> Result<String, AppErro
match tokio::fs::read(&log_path).await {
Ok(bytes) => Ok(String::from_utf8_lossy(&bytes).into_owned()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(String::new()),
Err(e) => Err(AppError::from(format!(
"Failed to read {}: {e}",
log_path.display()
))),
Err(e) => Err(e).with_context(|| format!("failed to read {}", log_path.display())),
}
}

Expand All @@ -37,5 +34,5 @@ pub async fn watch_log_file(
.ok_or_else(|| AppError::from("Game directory not set"))?;

let jeode_dir = dir.join("jeode");
crate::services::log_watcher::start(app, jeode_dir)
crate::services::jeodewatcher::start(app, jeode_dir)
}
8 changes: 4 additions & 4 deletions src-tauri/src/commands/mods.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tauri::State;
use tauri_plugin_dialog::DialogExt;

use crate::errors::AppError;
use crate::errors::{AppError, Context};
use crate::services::mods::{self, ModInfo};
use crate::state::AppState;

Expand Down Expand Up @@ -36,8 +36,8 @@ pub async fn remove_mod(id: String, state: State<'_, AppState>) -> Result<(), Ap
pub async fn open_mod_folder(id: String, state: State<'_, AppState>) -> Result<(), AppError> {
let dir = game_dir(&state)?;
let mod_path = mods::mod_folder_path(&dir, &id)?;
tauri_plugin_opener::reveal_item_in_dir(&mod_path)
.map_err(|e| AppError::from(format!("Failed to open mod folder: {e}")))
tauri_plugin_opener::reveal_item_in_dir(&mod_path).context("failed to open mod folder")?;
Ok(())
}

#[tauri::command]
Expand Down Expand Up @@ -71,7 +71,7 @@ pub async fn install_mod(
let path = file_path
.into_path()
.map_err(|_| AppError::from("Invalid file path selected"))?;
let result = crate::services::installer::install(&path, &dir)?;
let result = crate::services::installer::install(&path, &dir, None)?;
Ok(Some(result))
}
None => Ok(None),
Expand Down
5 changes: 3 additions & 2 deletions src-tauri/src/commands/onboarding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use tauri::State;
use tauri_plugin_dialog::DialogExt;

use crate::errors::AppError;
use crate::errors::{AppError, Context};
use crate::services::{game, jeode};
use crate::state::AppState;

Expand Down Expand Up @@ -85,5 +85,6 @@ const STEAM_LAUNCH_URL: &str = "steam://run/1419170";
#[tauri::command]
pub async fn launch_game() -> Result<(), AppError> {
tauri_plugin_opener::open_url(STEAM_LAUNCH_URL, None::<&str>)
.map_err(|e| AppError::from(format!("Failed to launch game: {e}")))
.context("failed to launch game")?;
Ok(())
}
107 changes: 77 additions & 30 deletions src-tauri/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,101 @@
use serde::Serialize;
use serde::{ser::SerializeStruct, Serialize, Serializer};
use thiserror::Error;

#[derive(Debug, Serialize, Clone)]
pub struct AppError {
pub message: String,
}
#[derive(Debug, Error)]
pub enum AppError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),

impl std::fmt::Display for AppError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
#[error("network error: {0}")]
Network(#[from] reqwest::Error),

impl std::error::Error for AppError {}
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),

impl From<std::io::Error> for AppError {
fn from(err: std::io::Error) -> Self {
Self {
message: err.to_string(),
}
}
#[error("{message}")]
Other {
message: String,
#[source]
source: Option<Box<dyn std::error::Error + Send + Sync>>,
},
}

impl From<reqwest::Error> for AppError {
fn from(err: reqwest::Error) -> Self {
Self {
message: err.to_string(),
pub type AppResult<T> = Result<T, AppError>;

impl AppError {
pub fn msg(message: impl Into<String>) -> Self {
Self::Other {
message: message.into(),
source: None,
}
}
}

impl From<serde_json::Error> for AppError {
fn from(err: serde_json::Error) -> Self {
Self {
message: err.to_string(),
fn kind(&self) -> &'static str {
match self {
Self::Io(_) => "io",
Self::Network(_) => "network",
Self::Json(_) => "json",
Self::Other { .. } => "other",
}
}
}

impl From<String> for AppError {
fn from(message: String) -> Self {
Self { message }
Self::msg(message)
}
}

impl From<&str> for AppError {
fn from(message: &str) -> Self {
Self {
message: message.to_string(),
Self::msg(message)
}
}

impl Serialize for AppError {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut message = self.to_string();
let mut src = std::error::Error::source(self);
while let Some(e) = src {
message.push_str(": ");
message.push_str(&e.to_string());
src = e.source();
}

let mut s = serializer.serialize_struct("AppError", 2)?;
s.serialize_field("kind", self.kind())?;
s.serialize_field("message", &message)?;
s.end()
}
}

pub type AppResult<T> = Result<T, AppError>;
pub trait Context<T> {
fn context(self, msg: impl Into<String>) -> AppResult<T>;
fn with_context<F, S>(self, f: F) -> AppResult<T>
where
F: FnOnce() -> S,
S: Into<String>;
}

impl<T, E> Context<T> for Result<T, E>
where
E: std::error::Error + Send + Sync + 'static,
{
fn context(self, msg: impl Into<String>) -> AppResult<T> {
self.map_err(|e| AppError::Other {
message: msg.into(),
source: Some(Box::new(e)),
})
}

fn with_context<F, S>(self, f: F) -> AppResult<T>
where
F: FnOnce() -> S,
S: Into<String>,
{
self.map_err(|e| AppError::Other {
message: f().into(),
source: Some(Box::new(e)),
})
}
}
6 changes: 3 additions & 3 deletions src-tauri/src/services/gamebanana.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

use crate::errors::{AppError, AppResult};
use crate::errors::{AppError, AppResult, Context};

const API_BASE: &str = "https://gamebanana.com/apiv11";
const MSM_GAME_ID: u32 = 9640;
Expand Down Expand Up @@ -323,14 +323,14 @@ pub async fn download_to_temp(file_id: u64, file_name: &str) -> AppResult<std::p
)));
}

let temp_dir = std::env::temp_dir().join("cantus/downloads");
let temp_dir = std::env::temp_dir().join("cantus").join("downloads");
std::fs::create_dir_all(&temp_dir)?;

let dest = temp_dir.join(file_name);
let bytes = response.bytes().await?;
tokio::fs::write(&dest, &bytes)
.await
.map_err(|e| AppError::from(format!("Failed to write download: {e}")))?;
.context("failed to write download")?;

Ok(dest)
}
Loading
Loading