|
| 1 | +use crate::slash_command::SlashCommand; |
| 2 | + |
| 3 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 4 | +pub(crate) enum FastSlashCommandArgs { |
| 5 | + On, |
| 6 | + Off, |
| 7 | + Status, |
| 8 | +} |
| 9 | + |
| 10 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 11 | +pub(crate) enum SlashCommandBareBehavior { |
| 12 | + DispatchesDirectly, |
| 13 | + OpensUi, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 17 | +pub(crate) enum ParsedSlashCommand { |
| 18 | + Bare(SlashCommandBareBehavior), |
| 19 | + Fast(FastSlashCommandArgs), |
| 20 | + Rename, |
| 21 | + Plan, |
| 22 | + Review, |
| 23 | + SandboxReadRoot, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 27 | +pub(crate) enum SlashCommandUsageErrorKind { |
| 28 | + UnexpectedInlineArgs, |
| 29 | + InvalidInlineArgs, |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 33 | +pub(crate) struct SlashCommandUsageError { |
| 34 | + command: SlashCommand, |
| 35 | + kind: SlashCommandUsageErrorKind, |
| 36 | +} |
| 37 | + |
| 38 | +impl SlashCommandUsageError { |
| 39 | + pub(crate) fn message(self) -> String { |
| 40 | + let usage = self.command.usage_lines().join(" | "); |
| 41 | + match self.kind { |
| 42 | + SlashCommandUsageErrorKind::UnexpectedInlineArgs => format!( |
| 43 | + "'/{}' does not accept inline arguments. Usage: {usage}", |
| 44 | + self.command.command() |
| 45 | + ), |
| 46 | + SlashCommandUsageErrorKind::InvalidInlineArgs => format!("Usage: {usage}"), |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl SlashCommand { |
| 52 | + pub(crate) fn bare_behavior(self) -> SlashCommandBareBehavior { |
| 53 | + match self { |
| 54 | + SlashCommand::Model |
| 55 | + | SlashCommand::Approvals |
| 56 | + | SlashCommand::Permissions |
| 57 | + | SlashCommand::Experimental |
| 58 | + | SlashCommand::Skills |
| 59 | + | SlashCommand::Review |
| 60 | + | SlashCommand::Rename |
| 61 | + | SlashCommand::Resume |
| 62 | + | SlashCommand::Collab |
| 63 | + | SlashCommand::Agent |
| 64 | + | SlashCommand::Statusline |
| 65 | + | SlashCommand::Theme |
| 66 | + | SlashCommand::Feedback |
| 67 | + | SlashCommand::Personality |
| 68 | + | SlashCommand::Settings |
| 69 | + | SlashCommand::MultiAgents => SlashCommandBareBehavior::OpensUi, |
| 70 | + SlashCommand::Fast |
| 71 | + | SlashCommand::ElevateSandbox |
| 72 | + | SlashCommand::SandboxReadRoot |
| 73 | + | SlashCommand::New |
| 74 | + | SlashCommand::Fork |
| 75 | + | SlashCommand::Init |
| 76 | + | SlashCommand::Compact |
| 77 | + | SlashCommand::Plan |
| 78 | + | SlashCommand::Diff |
| 79 | + | SlashCommand::Copy |
| 80 | + | SlashCommand::Mention |
| 81 | + | SlashCommand::Status |
| 82 | + | SlashCommand::DebugConfig |
| 83 | + | SlashCommand::Title |
| 84 | + | SlashCommand::Mcp |
| 85 | + | SlashCommand::Apps |
| 86 | + | SlashCommand::Logout |
| 87 | + | SlashCommand::Quit |
| 88 | + | SlashCommand::Exit |
| 89 | + | SlashCommand::Rollout |
| 90 | + | SlashCommand::Ps |
| 91 | + | SlashCommand::Stop |
| 92 | + | SlashCommand::Clear |
| 93 | + | SlashCommand::Realtime |
| 94 | + | SlashCommand::TestApproval |
| 95 | + | SlashCommand::MemoryDrop |
| 96 | + | SlashCommand::MemoryUpdate => SlashCommandBareBehavior::DispatchesDirectly, |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + pub(crate) fn parse_invocation( |
| 101 | + self, |
| 102 | + args: &str, |
| 103 | + ) -> Result<ParsedSlashCommand, SlashCommandUsageError> { |
| 104 | + let trimmed = args.trim(); |
| 105 | + if trimmed.is_empty() { |
| 106 | + return Ok(ParsedSlashCommand::Bare(self.bare_behavior())); |
| 107 | + } |
| 108 | + |
| 109 | + match self { |
| 110 | + SlashCommand::Fast => match trimmed.to_ascii_lowercase().as_str() { |
| 111 | + "on" => Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::On)), |
| 112 | + "off" => Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::Off)), |
| 113 | + "status" => Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::Status)), |
| 114 | + _ => Err(SlashCommandUsageError { |
| 115 | + command: self, |
| 116 | + kind: SlashCommandUsageErrorKind::InvalidInlineArgs, |
| 117 | + }), |
| 118 | + }, |
| 119 | + SlashCommand::Rename => Ok(ParsedSlashCommand::Rename), |
| 120 | + SlashCommand::Plan => Ok(ParsedSlashCommand::Plan), |
| 121 | + SlashCommand::Review => Ok(ParsedSlashCommand::Review), |
| 122 | + SlashCommand::SandboxReadRoot => Ok(ParsedSlashCommand::SandboxReadRoot), |
| 123 | + SlashCommand::Model |
| 124 | + | SlashCommand::Approvals |
| 125 | + | SlashCommand::Permissions |
| 126 | + | SlashCommand::ElevateSandbox |
| 127 | + | SlashCommand::Experimental |
| 128 | + | SlashCommand::Skills |
| 129 | + | SlashCommand::New |
| 130 | + | SlashCommand::Resume |
| 131 | + | SlashCommand::Fork |
| 132 | + | SlashCommand::Init |
| 133 | + | SlashCommand::Compact |
| 134 | + | SlashCommand::Collab |
| 135 | + | SlashCommand::Agent |
| 136 | + | SlashCommand::Diff |
| 137 | + | SlashCommand::Copy |
| 138 | + | SlashCommand::Mention |
| 139 | + | SlashCommand::Status |
| 140 | + | SlashCommand::DebugConfig |
| 141 | + | SlashCommand::Title |
| 142 | + | SlashCommand::Statusline |
| 143 | + | SlashCommand::Theme |
| 144 | + | SlashCommand::Mcp |
| 145 | + | SlashCommand::Apps |
| 146 | + | SlashCommand::Logout |
| 147 | + | SlashCommand::Quit |
| 148 | + | SlashCommand::Exit |
| 149 | + | SlashCommand::Feedback |
| 150 | + | SlashCommand::Rollout |
| 151 | + | SlashCommand::Ps |
| 152 | + | SlashCommand::Stop |
| 153 | + | SlashCommand::Clear |
| 154 | + | SlashCommand::Personality |
| 155 | + | SlashCommand::Realtime |
| 156 | + | SlashCommand::Settings |
| 157 | + | SlashCommand::TestApproval |
| 158 | + | SlashCommand::MultiAgents |
| 159 | + | SlashCommand::MemoryDrop |
| 160 | + | SlashCommand::MemoryUpdate => Err(SlashCommandUsageError { |
| 161 | + command: self, |
| 162 | + kind: SlashCommandUsageErrorKind::UnexpectedInlineArgs, |
| 163 | + }), |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +#[cfg(test)] |
| 169 | +mod tests { |
| 170 | + use pretty_assertions::assert_eq; |
| 171 | + |
| 172 | + use super::*; |
| 173 | + |
| 174 | + #[test] |
| 175 | + fn review_bare_form_is_marked_as_ui_driven() { |
| 176 | + assert_eq!( |
| 177 | + SlashCommand::Review.parse_invocation(""), |
| 178 | + Ok(ParsedSlashCommand::Bare(SlashCommandBareBehavior::OpensUi)) |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + #[test] |
| 183 | + fn fast_accepts_nonempty_inline_args() { |
| 184 | + assert_eq!( |
| 185 | + SlashCommand::Fast.parse_invocation("status"), |
| 186 | + Ok(ParsedSlashCommand::Fast(FastSlashCommandArgs::Status)) |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + #[test] |
| 191 | + fn clear_rejects_unexpected_inline_args() { |
| 192 | + assert_eq!( |
| 193 | + SlashCommand::Clear |
| 194 | + .parse_invocation("now") |
| 195 | + .unwrap_err() |
| 196 | + .message(), |
| 197 | + "'/clear' does not accept inline arguments. Usage: /clear" |
| 198 | + ); |
| 199 | + } |
| 200 | +} |
0 commit comments