Skip to main content

tui_dispatch_debug/
cli.rs

1use clap::Args;
2use std::path::PathBuf;
3
4use crate::debug::ActionLoggerConfig;
5
6/// Shared CLI flags for debug tooling.
7#[derive(Args, Debug, Clone, Default)]
8#[command(next_help_heading = "Debug")]
9pub struct DebugCliArgs {
10    /// Enable debug mode (F12 to toggle overlay)
11    #[arg(long = "debug")]
12    pub enabled: bool,
13
14    /// Render a single frame and exit (after applying debug state/actions)
15    #[arg(long = "debug-render-once")]
16    pub render_once: bool,
17
18    /// Load initial state from a JSON snapshot
19    #[arg(long = "debug-state-in")]
20    pub state_in: Option<PathBuf>,
21
22    /// Load and replay actions from a JSON snapshot
23    #[arg(long = "debug-actions-in")]
24    pub actions_in: Option<PathBuf>,
25
26    /// Save dispatched actions to a JSON snapshot
27    #[arg(long = "debug-actions-out")]
28    pub actions_out: Option<PathBuf>,
29
30    /// Include action patterns when recording debug actions (comma-separated)
31    #[arg(long = "debug-actions-include")]
32    pub actions_include: Option<String>,
33
34    /// Exclude action patterns when recording debug actions (comma-separated)
35    #[arg(long = "debug-actions-exclude")]
36    pub actions_exclude: Option<String>,
37
38    /// Save JSON schema for state type to file
39    #[arg(long = "debug-state-schema-out")]
40    pub state_schema_out: Option<PathBuf>,
41
42    /// Save JSON schema for action type to file
43    #[arg(long = "debug-actions-schema-out")]
44    pub actions_schema_out: Option<PathBuf>,
45
46    /// Timeout in seconds for awaiting async actions during replay
47    #[arg(long = "debug-replay-timeout", default_value_t = 30)]
48    pub replay_timeout: u64,
49}
50
51impl DebugCliArgs {
52    pub fn action_filter(&self) -> ActionLoggerConfig {
53        match (
54            self.actions_include.as_deref(),
55            self.actions_exclude.as_deref(),
56        ) {
57            (None, None) => ActionLoggerConfig::default(),
58            (Some(include), None) => {
59                ActionLoggerConfig::with_patterns(split_patterns(include), Vec::new())
60            }
61            (include, Some(exclude)) => ActionLoggerConfig::new(include, Some(exclude)),
62        }
63    }
64
65    pub fn auto_fetch(&self) -> bool {
66        self.state_in.is_none() && self.actions_in.is_none()
67    }
68}
69
70fn split_patterns(value: &str) -> Vec<String> {
71    value
72        .split(',')
73        .map(|pattern| pattern.trim())
74        .filter(|pattern| !pattern.is_empty())
75        .map(|pattern| pattern.to_string())
76        .collect()
77}