Skip to main content

brows3r_lib/commands/
diagnostics_cmd.rs

1//! Tauri commands for diagnostic bundle collection and export.
2//!
3//! # Commands
4//!
5//! - `diagnostics_collect` — build a redacted ZIP bundle from app files.
6//! - `diagnostics_export`  — copy the ZIP to a user-chosen path and clean up.
7//!
8//! # OCP
9//!
10//! New diagnostic commands are additive — each is an independent
11//! `#[tauri::command]` registered in `lib.rs`. `BundleConfig` is open for
12//! new `include_*` fields without changing these handlers.
13
14use std::path::PathBuf;
15
16use tauri::{AppHandle, Manager, State};
17
18use crate::{
19    diagnostics::{
20        bundle::{collect_bundle, export_bundle, AppPaths, BundleConfig, BundleRef},
21        DiagnosticsRedactorHandle,
22    },
23    error::AppError,
24};
25
26// ---------------------------------------------------------------------------
27// diagnostics_collect
28// ---------------------------------------------------------------------------
29
30/// Collect a diagnostic bundle according to `config`.
31///
32/// Resolves the app paths from the Tauri `AppHandle`, builds the bundle via
33/// `collect_bundle`, and returns a `BundleRef` that the frontend holds
34/// between the "Generate" and "Save" steps.
35#[tauri::command]
36pub async fn diagnostics_collect(
37    config: BundleConfig,
38    redactor: State<'_, DiagnosticsRedactorHandle>,
39    app: AppHandle,
40) -> Result<BundleRef, AppError> {
41    let app_paths = resolve_app_paths(&app)?;
42    collect_bundle(&config, &app_paths, &redactor)
43}
44
45// ---------------------------------------------------------------------------
46// diagnostics_export
47// ---------------------------------------------------------------------------
48
49/// Copy the collected bundle ZIP to `dest_path` and clean up the temp dir.
50///
51/// `bundle_ref` must be the value returned by a preceding `diagnostics_collect`
52/// call.  After a successful export the temp dir is removed automatically.
53#[tauri::command]
54pub async fn diagnostics_export(bundle_ref: BundleRef, dest_path: PathBuf) -> Result<(), AppError> {
55    export_bundle(&bundle_ref, &dest_path)
56}
57
58// ---------------------------------------------------------------------------
59// Private helpers
60// ---------------------------------------------------------------------------
61
62/// Build `AppPaths` from the Tauri `AppHandle`.
63///
64/// Falls back to temp-dir sub-directories when the Tauri resolver returns
65/// an error (e.g. in sandboxed environments or tests).
66fn resolve_app_paths(app: &AppHandle) -> Result<AppPaths, AppError> {
67    let path_resolver = app.path();
68
69    let app_config_dir = path_resolver
70        .app_config_dir()
71        .unwrap_or_else(|_| std::env::temp_dir().join("brows3r_config"));
72
73    let app_log_dir = path_resolver
74        .app_log_dir()
75        .unwrap_or_else(|_| std::env::temp_dir().join("brows3r_logs"));
76
77    let app_cache_dir = path_resolver
78        .app_cache_dir()
79        .unwrap_or_else(|_| std::env::temp_dir().join("brows3r_cache"));
80
81    Ok(AppPaths {
82        app_config_dir,
83        app_log_dir,
84        app_cache_dir,
85    })
86}