205 lines
8.8 KiB
Rust
205 lines
8.8 KiB
Rust
// file: crates/ksp-app-solprices-desk/src/tauri.rs
|
|
// version: 6
|
|
|
|
//! Tauri runtime assembly for the KSP SOL prices desktop application.
|
|
|
|
/// Runs the SOL prices desktop application.
|
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
|
pub fn run(arguments: &[std::ffi::OsString]) -> ksp_core_lib::Result<()> {
|
|
let context = tauri::generate_context!();
|
|
let runtime_layout = configure_packaged_runtime(&context);
|
|
if let std::result::Result::Err(error) = runtime_layout {
|
|
return std::result::Result::Err(error);
|
|
}
|
|
let app_state = crate::AppState::initialize(arguments);
|
|
let app_state = match app_state {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
|
};
|
|
let mut builder = tauri::Builder::default();
|
|
builder = configure_state(builder, app_state);
|
|
builder = configure_plugins(builder);
|
|
builder = configure_commands(builder);
|
|
builder = configure_setup(builder);
|
|
let run_result = builder.run(context);
|
|
return match run_result {
|
|
std::result::Result::Ok(()) => std::result::Result::Ok(()),
|
|
std::result::Result::Err(error) => std::result::Result::Err(
|
|
ksp_core_lib::Error::new(crate::ERROR_CODE_TAURI_RUNTIME_FAILED, "Cannot run KSP SOL Prices Desk Tauri runtime")
|
|
.with_context("tauri_error", error.to_string()),
|
|
),
|
|
};
|
|
}
|
|
|
|
fn configure_packaged_runtime(context: &tauri::Context<tauri::Wry>) -> ksp_core_lib::Result<()> {
|
|
if cfg!(debug_assertions) {
|
|
return std::result::Result::Ok(());
|
|
}
|
|
let resource_root = tauri::utils::platform::resource_dir(context.package_info(), &tauri::Env::default());
|
|
let resource_root = match resource_root {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => {
|
|
return std::result::Result::Err(
|
|
ksp_core_lib::Error::new(crate::ERROR_CODE_TAURI_RUNTIME_FAILED, "Cannot resolve packaged Tauri resource directory")
|
|
.with_context("tauri_error", error.to_string()),
|
|
);
|
|
},
|
|
};
|
|
let layout = ksp_config_lib::prepare_packaged_runtime(resource_root.as_path());
|
|
let layout = match layout {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
|
};
|
|
let working_directory = std::env::set_current_dir(layout.runtime_root());
|
|
return match working_directory {
|
|
std::result::Result::Ok(()) => std::result::Result::Ok(()),
|
|
std::result::Result::Err(error) => std::result::Result::Err(
|
|
ksp_core_lib::Error::new(crate::ERROR_CODE_TAURI_RUNTIME_FAILED, "Cannot activate packaged KSP runtime directory")
|
|
.with_context("runtime_root", layout.runtime_root().to_string_lossy().into_owned())
|
|
.with_source(error),
|
|
),
|
|
};
|
|
}
|
|
|
|
fn configure_state(builder: tauri::Builder<tauri::Wry>, app_state: crate::AppState) -> tauri::Builder<tauri::Wry> {
|
|
return builder.manage(app_state);
|
|
}
|
|
|
|
fn configure_plugins(builder: tauri::Builder<tauri::Wry>) -> tauri::Builder<tauri::Wry> {
|
|
let tracing_plugin = tauri_plugin_tracing::Builder::new().build::<tauri::Wry>();
|
|
return builder.plugin(tracing_plugin);
|
|
}
|
|
|
|
#[allow(clippy::question_mark_used)] // Tauri generates the question-mark operator internally for async command dispatch.
|
|
fn configure_commands(builder: tauri::Builder<tauri::Wry>) -> tauri::Builder<tauri::Wry> {
|
|
return builder.invoke_handler(tauri::generate_handler![
|
|
emit_frontend_log,
|
|
get_runtime_status,
|
|
list_market_prices,
|
|
refresh_all_market_prices,
|
|
refresh_market_price,
|
|
refresh_market_prices,
|
|
splash_frontend_ready,
|
|
]);
|
|
}
|
|
|
|
fn configure_setup(builder: tauri::Builder<tauri::Wry>) -> tauri::Builder<tauri::Wry> {
|
|
return builder.setup(|app| {
|
|
let splash = crate::require_splash_window(app);
|
|
if let std::result::Result::Err(error) = splash {
|
|
return std::result::Result::Err(std::boxed::Box::new(error));
|
|
}
|
|
let main = crate::require_main_window(app);
|
|
if let std::result::Result::Err(error) = main {
|
|
return std::result::Result::Err(std::boxed::Box::new(error));
|
|
}
|
|
ksp_logging_lib::debug!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_SHELL, "SOL Prices Desk Tauri shell setup completed");
|
|
return std::result::Result::Ok(());
|
|
});
|
|
}
|
|
|
|
fn project_command_error(command: &'static str, domain: &'static str, error: &ksp_core_lib::Error) -> crate::CommandErrorDto {
|
|
ksp_logging_lib::warn!(
|
|
target: crate::TRACING_TARGET,
|
|
domain = domain,
|
|
command = command,
|
|
error_domain = error.code().domain(),
|
|
error_code = error.code().code(),
|
|
"SOL Prices Desk Tauri command failed"
|
|
);
|
|
return crate::CommandErrorDto::from_error(error);
|
|
}
|
|
|
|
#[tauri::command]
|
|
fn emit_frontend_log(payload: crate::FrontendLogPayloadDto) -> std::result::Result<(), crate::CommandErrorDto> {
|
|
let result = crate::emit_frontend_log_event(payload);
|
|
return match result {
|
|
std::result::Result::Ok(()) => std::result::Result::Ok(()),
|
|
std::result::Result::Err(error) => std::result::Result::Err(project_command_error("emit_frontend_log", crate::TRACING_DOMAIN_FRONTEND, &error)),
|
|
};
|
|
}
|
|
|
|
#[tauri::command]
|
|
fn get_runtime_status(state: tauri::State<'_, crate::AppState>) -> std::result::Result<crate::MarketPriceRuntimeStatusDto, crate::CommandErrorDto> {
|
|
let result = state.runtime_status();
|
|
return match result {
|
|
std::result::Result::Ok(value) => std::result::Result::Ok(value),
|
|
std::result::Result::Err(error) => std::result::Result::Err(project_command_error("get_runtime_status", crate::TRACING_DOMAIN_SHELL, &error)),
|
|
};
|
|
}
|
|
|
|
#[tauri::command]
|
|
fn list_market_prices(
|
|
state: tauri::State<'_, crate::AppState>,
|
|
) -> std::result::Result<std::vec::Vec<crate::MarketPriceProviderRowDto>, crate::CommandErrorDto> {
|
|
let result = state.list_market_prices();
|
|
return match result {
|
|
std::result::Result::Ok(value) => {
|
|
ksp_logging_lib::trace!(
|
|
target: crate::TRACING_TARGET,
|
|
domain = crate::TRACING_DOMAIN_OFFCHAIN_TRANSPORT,
|
|
row_count = value.len(),
|
|
"projected SOL Prices Desk market-price registry rows"
|
|
);
|
|
std::result::Result::Ok(value)
|
|
},
|
|
std::result::Result::Err(error) => {
|
|
std::result::Result::Err(project_command_error("list_market_prices", crate::TRACING_DOMAIN_OFFCHAIN_TRANSPORT, &error))
|
|
},
|
|
};
|
|
}
|
|
|
|
#[tauri::command]
|
|
async fn refresh_all_market_prices(
|
|
state: tauri::State<'_, crate::AppState>,
|
|
) -> std::result::Result<crate::MarketPriceRefreshResultDto, crate::CommandErrorDto> {
|
|
let result = state.refresh_all_market_prices().await;
|
|
return match result {
|
|
std::result::Result::Ok(value) => std::result::Result::Ok(value),
|
|
std::result::Result::Err(error) => {
|
|
std::result::Result::Err(project_command_error("refresh_all_market_prices", crate::TRACING_DOMAIN_OFFCHAIN_TRANSPORT, &error))
|
|
},
|
|
};
|
|
}
|
|
|
|
#[tauri::command]
|
|
async fn refresh_market_price(
|
|
provider_id: String,
|
|
state: tauri::State<'_, crate::AppState>,
|
|
) -> std::result::Result<crate::MarketPriceProviderRowDto, crate::CommandErrorDto> {
|
|
let result = state.refresh_market_price(provider_id).await;
|
|
return match result {
|
|
std::result::Result::Ok(value) => std::result::Result::Ok(value),
|
|
std::result::Result::Err(error) => {
|
|
std::result::Result::Err(project_command_error("refresh_market_price", crate::TRACING_DOMAIN_OFFCHAIN_TRANSPORT, &error))
|
|
},
|
|
};
|
|
}
|
|
|
|
#[tauri::command]
|
|
async fn refresh_market_prices(
|
|
request: crate::MarketPriceRefreshManyRequestDto,
|
|
state: tauri::State<'_, crate::AppState>,
|
|
) -> std::result::Result<crate::MarketPriceRefreshResultDto, crate::CommandErrorDto> {
|
|
let result = state.refresh_market_prices(request).await;
|
|
return match result {
|
|
std::result::Result::Ok(value) => std::result::Result::Ok(value),
|
|
std::result::Result::Err(error) => {
|
|
std::result::Result::Err(project_command_error("refresh_market_prices", crate::TRACING_DOMAIN_OFFCHAIN_TRANSPORT, &error))
|
|
},
|
|
};
|
|
}
|
|
|
|
#[tauri::command]
|
|
async fn splash_frontend_ready(
|
|
app: tauri::AppHandle,
|
|
webview_window: tauri::WebviewWindow,
|
|
state: tauri::State<'_, crate::AppState>,
|
|
) -> std::result::Result<(), crate::CommandErrorDto> {
|
|
let result = crate::splash_frontend_ready_service(app, webview_window, &state).await;
|
|
return match result {
|
|
std::result::Result::Ok(()) => std::result::Result::Ok(()),
|
|
std::result::Result::Err(error) => std::result::Result::Err(project_command_error("splash_frontend_ready", crate::TRACING_DOMAIN_WINDOWS, &error)),
|
|
};
|
|
}
|