vanguard_plugin_sdk/
command.rs1use async_trait::async_trait;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Command {
8 pub name: String,
10 pub description: String,
12 pub usage: String,
14 #[serde(default)]
16 pub aliases: Vec<String>,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
21pub enum CommandResult {
22 Success,
24 Failure(String),
26 NotHandled,
28 NeedsInput(String),
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct VanguardCommand {
35 pub name: String,
37 pub args: Vec<String>,
39 pub original: String,
41}
42
43#[derive(Debug, Clone)]
45pub struct CommandContext {
46 pub cwd: std::path::PathBuf,
48 pub env: HashMap<String, String>,
50 pub interactive: bool,
52 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#[async_trait]
69pub trait CommandHandler {
70 fn get_commands(&self) -> Vec<Command>;
72
73 async fn handle_command(
75 &self,
76 command: &VanguardCommand,
77 ctx: &CommandContext,
78 ) -> CommandResult;
79
80 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}