can_viewer/
state.rs

1//! Application state management.
2
3use crate::config::SessionConfig;
4use dbc_rs::{Dbc, FastDbc};
5use parking_lot::Mutex;
6
7#[cfg(target_os = "linux")]
8use tokio::sync::oneshot;
9
10/// Initial file paths from command line arguments.
11#[derive(Default)]
12pub struct InitialFiles {
13    pub dbc_path: Option<String>,
14    pub mdf4_path: Option<String>,
15}
16
17/// Type alias for the stop channel sender.
18/// Sends a result channel that will receive the finalized file path.
19#[cfg(target_os = "linux")]
20pub type CaptureStopTx = oneshot::Sender<oneshot::Sender<Result<String, String>>>;
21
22/// Global application state shared across Tauri commands.
23pub struct AppState {
24    /// Loaded DBC database for signal decoding.
25    pub dbc: Mutex<Option<Dbc>>,
26
27    /// High-performance DBC wrapper with O(1) message lookup.
28    /// Created alongside dbc for fast decoding in hot paths.
29    pub fast_dbc: Mutex<Option<FastDbc>>,
30
31    /// Path to the currently loaded DBC file.
32    pub dbc_path: Mutex<Option<String>>,
33
34    /// Initial files from command line.
35    pub initial_files: Mutex<InitialFiles>,
36
37    /// Session configuration for persistence.
38    pub session: Mutex<SessionConfig>,
39
40    /// Whether a SocketCAN capture is currently running.
41    #[cfg(target_os = "linux")]
42    pub capture_running: Mutex<bool>,
43
44    /// Channel to signal capture to stop and receive result.
45    #[cfg(target_os = "linux")]
46    pub capture_stop_tx: Mutex<Option<CaptureStopTx>>,
47}
48
49impl AppState {
50    pub fn with_initial_files(initial_files: InitialFiles) -> Self {
51        let session = SessionConfig::load();
52        Self {
53            dbc: Mutex::new(None),
54            fast_dbc: Mutex::new(None),
55            dbc_path: Mutex::new(None),
56            initial_files: Mutex::new(initial_files),
57            session: Mutex::new(session),
58            #[cfg(target_os = "linux")]
59            capture_running: Mutex::new(false),
60            #[cfg(target_os = "linux")]
61            capture_stop_tx: Mutex::new(None),
62        }
63    }
64}
65
66impl Default for AppState {
67    fn default() -> Self {
68        let session = SessionConfig::load();
69        Self {
70            dbc: Mutex::new(None),
71            fast_dbc: Mutex::new(None),
72            dbc_path: Mutex::new(None),
73            initial_files: Mutex::new(InitialFiles::default()),
74            session: Mutex::new(session),
75            #[cfg(target_os = "linux")]
76            capture_running: Mutex::new(false),
77            #[cfg(target_os = "linux")]
78            capture_stop_tx: Mutex::new(None),
79        }
80    }
81}