p2p_chat/ui/runner/actions/commands/
mod.rs

1//! This module contains command dispatching logic for the UI runner.
2//!
3//! It maps command strings to their respective handler functions.
4mod friends;
5mod history;
6mod info;
7mod peers;
8mod send;
9
10use anyhow::Result;
11
12use super::context::CommandContext;
13
14/// Dispatches a command to the appropriate handler function.
15///
16/// This function takes a parsed command (parts) and the command context,
17/// then executes the corresponding command handler.
18///
19/// # Arguments
20///
21/// * `parts` - A slice of strings representing the command and its arguments.
22/// * `context` - The `CommandContext` providing access to the application's state.
23///
24/// # Returns
25///
26/// A `Result` indicating success or failure of the command execution.
27pub async fn dispatch(parts: &[&str], context: &CommandContext) -> Result<()> {
28    match parts[0] {
29        "send" => send::handle_send(parts, context).await,
30        "friend" => friends::add_friend(parts, context).await,
31        "friends" => friends::list_friends(context).await,
32        "history" => history::show_history(parts, context).await,
33        "peers" => peers::list_peers(context).await,
34        "info" => info::show_info(context).await,
35        "check" => info::show_check_message(context).await,
36        "help" => info::show_help(context).await,
37        "exit" => Ok(()),
38        _ => {
39            context.emit_chat(format!(
40                "❌ Unknown command: {}. Type 'help' for available commands.",
41                parts[0]
42            ));
43            Ok(())
44        }
45    }
46}