Skip to content

Commit

Permalink
Actually useful errors
Browse files Browse the repository at this point in the history
  • Loading branch information
RedstoneWizard08 committed Jun 3, 2024
1 parent 13c3889 commit 46e1e24
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 141 deletions.
15 changes: 10 additions & 5 deletions crates/commands/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ use api::{plugin::PluginInfo, register::PLUGINS};
use data::source::SourceMapping;
use whcore::{dirs::Dirs, manager::CoreManager};

use crate::AppState;
use crate::{AppState, Result};

/// Gets the list of registered plugins as [`PluginInfo`].
#[whmacros::serde_call]
#[tauri::command]
#[specta::specta]
pub async fn get_plugins(_pool: AppState<'_>) -> Result<Vec<PluginInfo>, bool> {
pub async fn get_plugins(_pool: AppState<'_>) -> Result<Vec<PluginInfo>> {
let mut res = Vec::new();
let lock = PLUGINS.lock().await;

for plugin in lock.values() {
res.push(plugin.as_info().await.ok_or(false)?);
res.push(
plugin
.as_info()
.await
.ok_or::<String>("Tried to unwrap a None value!".into())?,
);
}

Ok(res)
Expand All @@ -26,14 +31,14 @@ pub async fn get_plugins(_pool: AppState<'_>) -> Result<Vec<PluginInfo>, bool> {
#[whmacros::serde_call]
#[tauri::command]
#[specta::specta]
pub async fn get_dirs(_pool: AppState<'_>) -> Result<Dirs, bool> {
pub async fn get_dirs(_pool: AppState<'_>) -> Result<Dirs> {
Ok(CoreManager::get().dirs())
}

/// Gets the string source ID from a source (mapping) ID.
#[whmacros::serde_call]
#[tauri::command]
#[specta::specta]
pub async fn get_source_id(sid: i32, _pool: AppState<'_>) -> Result<String, bool> {
pub async fn get_source_id(sid: i32, _pool: AppState<'_>) -> Result<String> {
Ok(SourceMapping::from(sid).as_str().to_string())
}
66 changes: 31 additions & 35 deletions crates/commands/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use data::{
schema::instances,
};
use mcmeta::cmd::modded::GetLoader;
use whcore::Boolify;
use whcore::Stringify;

use crate::AppState;
use crate::{AppState, Result};

/// Gets all instances for the given game.
///
Expand All @@ -23,14 +23,14 @@ use crate::AppState;
#[whmacros::serde_call]
#[tauri::command]
#[specta::specta]
pub async fn get_instances(game_id: i32, pool: AppState<'_>) -> Result<Vec<Instance>, bool> {
let mut db = pool.get().bool()?;
pub async fn get_instances(game_id: i32, pool: AppState<'_>) -> Result<Vec<Instance>> {
let mut db = pool.get().stringify()?;

let items = instances::table
.select(Instance::as_select())
.filter(instances::game_id.eq(game_id))
.load(&mut db)
.bool()?;
.stringify()?;

Ok(items)
}
Expand All @@ -42,12 +42,12 @@ pub async fn get_instances(game_id: i32, pool: AppState<'_>) -> Result<Vec<Insta
#[whmacros::serde_call]
#[tauri::command]
#[specta::specta]
pub async fn delete_instance(instance_id: i32, pool: AppState<'_>) -> Result<(), bool> {
let mut db = pool.get().bool()?;
pub async fn delete_instance(instance_id: i32, pool: AppState<'_>) -> Result<()> {
let mut db = pool.get().stringify()?;

delete(instances::table.filter(instances::id.eq(instance_id)))
.execute(&mut db)
.bool()?;
.stringify()?;

Ok(())
}
Expand All @@ -64,13 +64,9 @@ pub async fn delete_instance(instance_id: i32, pool: AppState<'_>) -> Result<(),
#[whmacros::serde_call]
#[tauri::command]
#[specta::specta]
pub async fn create_instance(
name: String,
game_id: i32,
pool: AppState<'_>,
) -> Result<Instance, bool> {
pub async fn create_instance(name: String, game_id: i32, pool: AppState<'_>) -> Result<Instance> {
let lock = PLUGINS.lock().await;
let plugin = lock.get(&game_id).bool()?;
let plugin = lock.get(&game_id).stringify()?;
let dirs = plugin.dirs();
let now = DateTime::<Utc>::default().timestamp_millis();

Expand All @@ -90,31 +86,31 @@ pub async fn create_instance(
.to_str()
.unwrap()
.to_string(),
install_dir: plugin.find().bool()?.to_str().unwrap().to_string(),
install_dir: plugin.find().stringify()?.to_str().unwrap().to_string(),
description: String::new(),
name,
loader: None,
})
.returning(Instance::as_returning())
.get_result(&mut pool.get().bool()?)
.bool()?;
.get_result(&mut pool.get().stringify()?)
.stringify()?;

info!("Installing loader...");

it.loader = Some(serde_json::to_string(&it.loader().await.bool()?).bool()?);
it.loader = Some(serde_json::to_string(&it.loader().await.stringify()?).stringify()?);

info!("Updating database...");

let it = update(instances::table)
.filter(instances::id.eq(it.id))
.set(instances::loader.eq(it.loader))
.returning(Instance::as_returning())
.get_result(&mut pool.get().bool()?)
.bool()?;
.get_result(&mut pool.get().stringify()?)
.stringify()?;

info!("Installing instance...");

plugin.install_instance(&it).await.bool()?;
plugin.install_instance(&it).await.stringify()?;

Ok(it)
}
Expand All @@ -133,13 +129,13 @@ pub async fn update_instance(
instance_id: i32,
desc: String,
pool: AppState<'_>,
) -> Result<Instance, bool> {
) -> Result<Instance> {
Ok(update(instances::table)
.filter(instances::id.eq(instance_id))
.set(instances::description.eq(desc))
.returning(Instance::as_returning())
.get_result(&mut pool.get().bool()?)
.bool()?)
.get_result(&mut pool.get().stringify()?)
.stringify()?)
}

/// Creates a new instance, without installing a mod loader.
Expand All @@ -149,30 +145,30 @@ pub async fn update_instance(
#[whmacros::serde_call]
#[tauri::command]
#[specta::specta]
pub async fn add_instance(instance: Instance, pool: AppState<'_>) -> Result<Instance, bool> {
pub async fn add_instance(instance: Instance, pool: AppState<'_>) -> Result<Instance> {
let mut it = insert_into(instances::table)
.values(instance)
.returning(Instance::as_returning())
.get_result(&mut pool.get().bool()?)
.bool()?;
.get_result(&mut pool.get().stringify()?)
.stringify()?;

it.loader = Some(serde_json::to_string(&it.loader().await.bool()?).bool()?);
it.loader = Some(serde_json::to_string(&it.loader().await.stringify()?).stringify()?);

let it = update(instances::table)
.filter(instances::id.eq(it.id))
.set(instances::loader.eq(it.loader))
.returning(Instance::as_returning())
.get_result(&mut pool.get().bool()?)
.bool()?;
.get_result(&mut pool.get().stringify()?)
.stringify()?;

PLUGINS
.lock()
.await
.get(&it.game_id)
.bool()?
.stringify()?
.install_instance(&it)
.await
.bool()?;
.stringify()?;

Ok(it)
}
Expand All @@ -184,10 +180,10 @@ pub async fn add_instance(instance: Instance, pool: AppState<'_>) -> Result<Inst
#[whmacros::serde_call]
#[tauri::command]
#[specta::specta]
pub async fn get_instance(instance_id: i32, pool: AppState<'_>) -> Result<Instance, bool> {
pub async fn get_instance(instance_id: i32, pool: AppState<'_>) -> Result<Instance> {
Ok(instances::table
.select(Instance::as_select())
.filter(instances::id.eq(instance_id))
.get_result(&mut pool.get().bool()?)
.bool()?)
.get_result(&mut pool.get().stringify()?)
.stringify()?)
}
3 changes: 3 additions & 0 deletions crates/commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ use whcore::state::TState;
/// This is wrapped in a [`TState`] to work with [`specta`] and [`tauri_specta`].
pub type AppState<'a> = TState<'a, Pool<ConnectionManager<SqliteConnection>>>;

/// The result type.
pub type Result<T, E = String> = core::result::Result<T, E>;

pub mod base;
pub mod instance;
pub mod loader;
Expand Down
49 changes: 23 additions & 26 deletions crates/commands/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use mcmeta::{
piston::manifest::get_manifest,
quilt::get_quilt_versions,
};
use whcore::Boolify;
use whcore::Stringify;

use crate::AppState;
use crate::{AppState, Result};

/// Get a loader's latest version.
///
Expand All @@ -25,16 +25,13 @@ use crate::AppState;
#[whmacros::serde_call]
#[tauri::command]
#[specta::specta]
pub async fn get_latest_loader(
loader: ModLoaderType,
_pool: AppState<'_>,
) -> Result<ModLoader, bool> {
pub async fn get_latest_loader(loader: ModLoaderType, _pool: AppState<'_>) -> Result<ModLoader> {
Ok(match loader {
ModLoaderType::Forge => ModLoader::forge_latest().await.bool()?,
ModLoaderType::NeoForge => ModLoader::neo_latest().await.bool()?,
ModLoaderType::Fabric => ModLoader::fabric_latest().await.bool()?,
ModLoaderType::Quilt => ModLoader::quilt_latest().await.bool()?,
ModLoaderType::Vanilla => ModLoader::vanilla_latest().await.bool()?,
ModLoaderType::Forge => ModLoader::forge_latest().await.stringify()?,
ModLoaderType::NeoForge => ModLoader::neo_latest().await.stringify()?,
ModLoaderType::Fabric => ModLoader::fabric_latest().await.stringify()?,
ModLoaderType::Quilt => ModLoader::quilt_latest().await.stringify()?,
ModLoaderType::Vanilla => ModLoader::vanilla_latest().await.stringify()?,
ModLoaderType::None => ModLoader::None,
})
}
Expand All @@ -46,14 +43,11 @@ pub async fn get_latest_loader(
#[whmacros::serde_call]
#[tauri::command]
#[specta::specta]
pub async fn get_loaders(
loader: ModLoaderType,
_pool: AppState<'_>,
) -> Result<Vec<ModLoader>, bool> {
pub async fn get_loaders(loader: ModLoaderType, _pool: AppState<'_>) -> Result<Vec<ModLoader>> {
Ok(match loader {
ModLoaderType::Forge => get_forge_versions()
.await
.bool()?
.stringify()?
.versioning
.versions
.iter()
Expand All @@ -65,7 +59,7 @@ pub async fn get_loaders(

ModLoaderType::NeoForge => get_neoforge_versions()
.await
.bool()?
.stringify()?
.0
.iter()
.map(|v| {
Expand All @@ -76,7 +70,7 @@ pub async fn get_loaders(

ModLoaderType::Fabric => get_fabric_versions()
.await
.bool()?
.stringify()?
.versioning
.versions
.iter()
Expand All @@ -85,7 +79,7 @@ pub async fn get_loaders(

ModLoaderType::Quilt => get_quilt_versions()
.await
.bool()?
.stringify()?
.versioning
.versions
.iter()
Expand All @@ -94,7 +88,7 @@ pub async fn get_loaders(

ModLoaderType::Vanilla => get_manifest()
.await
.bool()?
.stringify()?
.versions
.iter()
.map(|v| ModLoader::Vanilla(v.id.clone()))
Expand All @@ -116,18 +110,21 @@ pub async fn install_loader(
loader: ModLoader,
instance: Instance,
pool: AppState<'_>,
) -> Result<Instance, bool> {
) -> Result<Instance> {
let mut instance = instance;
let lock = PLUGINS.lock().await;
let plugin = lock.get(&instance.game_id).bool()?;
let plugin = lock.get(&instance.game_id).stringify()?;

instance.loader = Some(serde_json::to_string(&loader).bool()?);
plugin.install_loader(&instance, &loader).await.bool()?;
instance.loader = Some(serde_json::to_string(&loader).stringify()?);
plugin
.install_loader(&instance, &loader)
.await
.stringify()?;

Ok(update(instances::table)
.filter(instances::id.eq(instance.id))
.set(instances::loader.eq(instance.loader))
.returning(Instance::as_returning())
.get_result(&mut pool.get().bool()?)
.bool()?)
.get_result(&mut pool.get().stringify()?)
.stringify()?)
}
Loading

0 comments on commit 46e1e24

Please sign in to comment.