Files
STARTLINER/rust/src/cmd.rs
2025-02-25 19:27:37 +00:00

153 lines
4.1 KiB
Rust

use log;
use std::collections::HashMap;
use std::path::PathBuf;
use tokio::sync::Mutex;
use crate::pkg::{Package, PkgKey};
use crate::pkg_store::InstallResult;
use crate::profile::Profile;
use crate::appdata::AppData;
use crate::{liner, start};
use tauri::{AppHandle, Manager, State};
#[tauri::command]
pub async fn startline(app: AppHandle) -> Result<(), String> {
log::debug!("invoke: startline");
let app_copy = app.clone();
let state = app.state::<Mutex<AppData>>();
let appd = state.lock().await;
if let Some(p) = &appd.profile {
// TODO if p.needsUpdate
liner::line_up(p).await.expect("Line-up failed");
start::start(p, app_copy)
.map_err(|e| { log::error!("Error launching: {}", e.to_string()); e.to_string() })
//Ok(())
} else {
Err("No profile".to_owned())
}
}
#[tauri::command]
pub async fn install_package(state: State<'_, tokio::sync::Mutex<AppData>>, key: PkgKey) -> Result<InstallResult, String> {
log::debug!("invoke: install_package({})", key);
let mut appd = state.lock().await;
appd.pkgs.install_package(&key, true, true)
.await
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn delete_package(state: State<'_, tokio::sync::Mutex<AppData>>, key: PkgKey) -> Result<(), String> {
log::debug!("invoke: delete_package({})", key);
let mut appd = state.lock().await;
appd.pkgs.delete_package(&key, true)
.await
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn get_package(state: State<'_, tokio::sync::Mutex<AppData>>, key: PkgKey) -> Result<Package, String> {
log::debug!("invoke: get_package({})", key);
let appd = state.lock().await;
appd.pkgs.get(&key)
.map_err(|e| e.to_string())
.cloned()
}
#[tauri::command]
pub async fn toggle_package(state: State<'_, tokio::sync::Mutex<AppData>>, key: PkgKey, enable: bool) -> Result<(), String> {
log::debug!("invoke: toggle_package({}, {})", key, enable);
let mut appd = state.lock().await;
appd.toggle_package(key, enable)
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn reload_all_packages(state: State<'_, tokio::sync::Mutex<AppData>>) -> Result<(), String> {
log::debug!("invoke: reload_all_packages");
let mut appd = state.lock().await;
appd.pkgs.reload_all()
.await
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn get_all_packages(state: State<'_, Mutex<AppData>>) -> Result<HashMap<PkgKey, Package>, ()> {
log::debug!("invoke: get_all_packages");
let appd = state.lock().await;
Ok(appd.pkgs.get_all())
}
#[tauri::command]
pub async fn fetch_listings(state: State<'_, Mutex<AppData>>) -> Result<(), String> {
log::debug!("invoke: fetch_listings");
let mut appd = state.lock().await;
appd.pkgs.fetch_listings().await
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn get_current_profile(state: State<'_, Mutex<AppData>>) -> Result<Option<Profile>, ()> {
log::debug!("invoke: get_current_profile");
let appd = state.lock().await;
Ok(appd.profile.clone())
}
#[tauri::command]
pub async fn save_profile(state: State<'_, Mutex<AppData>>) -> Result<(), ()> {
log::debug!("invoke: save_profile");
let appd = state.lock().await;
if let Some(p) = &appd.profile {
p.save().await;
} else {
log::warn!("No profile to save");
}
Ok(())
}
#[tauri::command]
pub async fn init_profile(
state: State<'_, Mutex<AppData>>,
exe_path: PathBuf
) -> Result<Profile, String> {
log::debug!("invoke: init_profile({})", exe_path.to_string_lossy());
let mut appd = state.lock().await;
let new_profile = Profile::new(exe_path);
new_profile.save().await;
appd.profile = Some(new_profile.clone());
Ok(new_profile)
}
#[tauri::command]
pub async fn set_cfg(
state: State<'_, Mutex<AppData>>,
key: String,
value: serde_json::Value
) -> Result<(), ()> {
log::debug!("invoke: sync_cfg({}, {})", key, value);
let mut appd = state.lock().await;
if let Some(p) = &mut appd.profile {
p.cfg.insert(key, value);
}
Ok(())
}