vanguard_plugin_sdk/
command.rs

1use async_trait::async_trait;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5/// Represents a plugin command
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Command {
8    /// The name of the command (e.g., "hello")
9    pub name: String,
10    /// Brief description of what the command does
11    pub description: String,
12    /// Usage example (e.g., "plugin-name hello [name]")
13    pub usage: String,
14    /// Optional aliases for the command
15    #[serde(default)]
16    pub aliases: Vec<String>,
17}
18
19/// Result of a command execution
20#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
21pub enum CommandResult {
22    /// Command executed successfully
23    Success,
24    /// Command failed with an error message
25    Failure(String),
26    /// Command was not handled by this plugin
27    NotHandled,
28    /// Command requires additional input
29    NeedsInput(String),
30}
31
32/// A command to be executed by a plugin
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct VanguardCommand {
35    /// The name of the command to execute
36    pub name: String,
37    /// Arguments passed to the command
38    pub args: Vec<String>,
39    /// Original command string
40    pub original: String,
41}
42
43/// Context for command execution
44#[derive(Debug, Clone)]
45pub struct CommandContext {
46    /// Current working directory
47    pub cwd: std::path::PathBuf,
48    /// Environment variables
49    pub env: HashMap<String, String>,
50    /// Whether command is running in interactive mode
51    pub interactive: bool,
52    /// Additional context data
53    pub data: HashMap<String, String>,
54}
55
56impl Default for CommandContext {
57    fn default() -> Self {
58        Self {
59            cwd: std::env::current_dir().unwrap_or_default(),
60            env: std::env::vars().collect(),
61            interactive: true,
62            data: HashMap::new(),
63        }
64    }
65}
66
67/// Trait for handling commands
68#[async_trait]
69pub trait CommandHandler {
70    /// Get a list of commands supported by this handler
71    fn get_commands(&self) -> Vec<Command>;
72
73    /// Handle a command
74    async fn handle_command(
75        &self,
76        command: &VanguardCommand,
77        ctx: &CommandContext,
78    ) -> CommandResult;
79
80    /// Check if this handler can handle a command
81    fn can_handle(&self, command_name: &str) -> bool {
82        self.get_commands()
83            .iter()
84            .any(|cmd| cmd.name == command_name || cmd.aliases.contains(&command_name.to_string()))
85    }
86}