rush_sync_server/commands/
handler.rs

1use super::registry::CommandRegistry;
2use crate::i18n;
3
4#[derive(Debug)]
5pub struct CommandResult {
6    pub message: String,
7    pub success: bool,
8    pub should_exit: bool,
9}
10
11pub struct CommandHandler {
12    registry: CommandRegistry,
13}
14
15impl CommandHandler {
16    pub fn new() -> Self {
17        Self {
18            registry: crate::create_default_registry(),
19        }
20    }
21
22    pub fn with_registry(registry: CommandRegistry) -> Self {
23        Self { registry }
24    }
25
26    pub fn handle_input(&self, input: &str) -> CommandResult {
27        let input = input.trim();
28        let parts: Vec<&str> = input.split_whitespace().collect();
29
30        if input.is_empty() {
31            return CommandResult {
32                message: String::new(),
33                success: false,
34                should_exit: false,
35            };
36        }
37
38        log::info!("🔧 CommandHandler processing: '{}'", input);
39
40        // FIX: Handle String/&str conversion properly
41        match self.registry.execute_sync(parts[0], &parts[1..]) {
42            Some(Ok(msg)) => {
43                // ✅ DEBUG: Log the result
44                log::info!(
45                    "🔧 Command returned {} chars: '{}'",
46                    msg.len(),
47                    &msg[..msg.len().min(100)]
48                );
49
50                let should_exit = self.should_exit_on_message(&msg);
51                let result = CommandResult {
52                    message: msg,
53                    success: true,
54                    should_exit,
55                };
56
57                // ✅ DEBUG: Log the final result
58                log::info!(
59                    "🔧 CommandResult: success={}, message_len={}, should_exit={}",
60                    result.success,
61                    result.message.len(),
62                    result.should_exit
63                );
64
65                result
66            }
67            Some(Err(e)) => {
68                log::error!("🔧 Command error: {}", e);
69                CommandResult {
70                    message: e.to_string(),
71                    success: false,
72                    should_exit: false,
73                }
74            }
75            None => {
76                log::warn!("🔧 Unknown command: {}", input);
77                CommandResult {
78                    message: crate::i18n::get_command_translation(
79                        "system.commands.unknown",
80                        &[input],
81                    ),
82                    success: false,
83                    should_exit: false,
84                }
85            }
86        }
87    }
88
89    pub async fn handle_input_async(&self, input: &str) -> CommandResult {
90        let input = input.trim();
91        let parts: Vec<&str> = input.split_whitespace().collect();
92
93        if input.is_empty() {
94            return CommandResult {
95                message: String::new(),
96                success: false,
97                should_exit: false,
98            };
99        }
100
101        // FIX: Handle String/&str conversion properly
102        match self.registry.execute_async(parts[0], &parts[1..]).await {
103            Some(Ok(msg)) => {
104                let should_exit = self.should_exit_on_message(&msg);
105                CommandResult {
106                    message: msg, // FIX: msg is already String from execute_async
107                    success: true,
108                    should_exit,
109                }
110            }
111            Some(Err(e)) => CommandResult {
112                message: e.to_string(),
113                success: false,
114                should_exit: false,
115            },
116            None => CommandResult {
117                message: i18n::get_command_translation("system.commands.unknown", &[input]),
118                success: false,
119                should_exit: false,
120            },
121        }
122    }
123
124    pub fn add_command<T: crate::commands::command::Command>(&mut self, command: T) {
125        self.registry.register(command);
126    }
127
128    pub fn list_commands(&self) -> Vec<(&str, &str)> {
129        self.registry.list_commands()
130    }
131
132    pub fn debug_info(&self) -> String {
133        self.registry.debug_info()
134    }
135
136    fn should_exit_on_message(&self, message: &str) -> bool {
137        message.starts_with("__EXIT__")
138            || message.starts_with("__CONFIRM_EXIT__")
139            || message.starts_with("__RESTART__")
140            || message.starts_with("__CONFIRM_RESTART__")
141    }
142}
143
144impl Default for CommandHandler {
145    fn default() -> Self {
146        Self::new()
147    }
148}