Repository navigation
Expand file tree
/
Copy pathcommand.rs
More file actions
395 lines (317 loc) · 10.9 KB
/
Copy pathcommand.rs
File metadata and controls
395 lines (317 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//! Command structure and parsing.
use std::path::PathBuf;
use regex_lite::Regex;
use serde::{Deserialize, Serialize};
use thiserror::Error;
/// Errors that can occur when working with commands.
#[derive(Debug, Error)]
pub enum CommandError {
/// Invalid frontmatter format.
#[error("Invalid frontmatter: {0}")]
InvalidFrontmatter(String),
/// Missing required field.
#[error("Missing required field: {0}")]
MissingField(String),
/// YAML parsing error.
#[error("YAML parsing error: {0}")]
YamlError(#[from] serde_yaml::Error),
/// Invalid placeholder format.
#[error("Invalid placeholder format: {0}")]
InvalidPlaceholder(String),
}
/// Configuration for a custom command, parsed from YAML frontmatter.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CommandConfig {
/// Human-readable description of the command.
#[serde(default)]
pub description: Option<String>,
/// Specific agent to use for this command.
#[serde(default)]
pub agent: Option<String>,
/// Specific model to use for this command.
#[serde(default)]
pub model: Option<String>,
/// Whether to execute this command as a subtask.
#[serde(default)]
pub subtask: Option<bool>,
/// Aliases for this command.
#[serde(default)]
pub aliases: Vec<String>,
}
/// A custom command loaded from a markdown file.
#[derive(Debug, Clone)]
pub struct Command {
/// Name of the command (filename without extension).
pub name: String,
/// Configuration from frontmatter.
pub config: CommandConfig,
/// Template content (after frontmatter).
pub template: String,
/// Path to the source file.
pub source_path: PathBuf,
}
impl Command {
/// Create a new command from parsed components.
pub fn new(
name: impl Into<String>,
config: CommandConfig,
template: impl Into<String>,
source_path: PathBuf,
) -> Self {
Self {
name: name.into(),
config,
template: template.into(),
source_path,
}
}
/// Parse a command from file content.
///
/// The content should be a markdown file with optional YAML frontmatter.
pub fn parse(
name: impl Into<String>,
content: &str,
source_path: PathBuf,
) -> Result<Self, CommandError> {
let (config, template) = parse_frontmatter(content)?;
Ok(Self::new(name, config, template, source_path))
}
/// Get the description of the command.
pub fn description(&self) -> &str {
self.config
.description
.as_deref()
.unwrap_or("Custom command")
}
/// Get the hints (placeholders) expected by this command's template.
pub fn hints(&self) -> Vec<String> {
hints(&self.template)
}
/// Check if this command expects arguments.
pub fn expects_arguments(&self) -> bool {
!self.hints().is_empty()
}
/// Substitute placeholders in the template with the given arguments.
pub fn substitute(&self, arguments: &str) -> String {
substitute_placeholders(&self.template, arguments)
}
}
/// Parse YAML frontmatter from markdown content.
///
/// Frontmatter is delimited by `---` at the start and end.
/// Returns the parsed config and the remaining template content.
fn parse_frontmatter(content: &str) -> Result<(CommandConfig, String), CommandError> {
let content = content.trim();
// Check if content starts with frontmatter delimiter
if !content.starts_with("---") {
// No frontmatter, entire content is the template
return Ok((CommandConfig::default(), content.to_string()));
}
// Find the closing delimiter
let rest = &content[3..];
let end_delimiter = rest.find("\n---");
match end_delimiter {
Some(end_pos) => {
let yaml_content = &rest[..end_pos].trim();
let template = rest[end_pos + 4..].trim();
// Parse YAML
let config: CommandConfig = if yaml_content.is_empty() {
CommandConfig::default()
} else {
serde_yaml::from_str(yaml_content)?
};
Ok((config, template.to_string()))
}
None => {
// No closing delimiter found, treat as no frontmatter
Err(CommandError::InvalidFrontmatter(
"Missing closing '---' delimiter".to_string(),
))
}
}
}
/// Extract placeholder hints from a template.
///
/// Returns a list of unique placeholders found in the template.
/// Examples: `$ARGUMENTS`, `$1`, `$2`, etc.
pub fn hints(template: &str) -> Vec<String> {
let mut found = Vec::new();
// Match $ARGUMENTS
if template.contains("$ARGUMENTS") {
found.push("$ARGUMENTS".to_string());
}
// Match $1, $2, ..., $9, $10, etc.
let re = Regex::new(r"\$(\d+)").unwrap_or_else(|_| {
// Fallback simple check if regex fails
Regex::new(r"\$\d").unwrap_or_else(|_| panic!("Regex compilation failed"))
});
let mut numbered: Vec<u32> = re
.captures_iter(template)
.filter_map(|cap| cap.get(1))
.filter_map(|m| m.as_str().parse::<u32>().ok())
.collect();
numbered.sort_unstable();
numbered.dedup();
for n in numbered {
found.push(format!("${n}"));
}
found
}
/// Substitute placeholders in a template with arguments.
///
/// - `$ARGUMENTS` is replaced with all arguments.
/// - `$1`, `$2`, etc. are replaced with individual arguments.
/// - The last numbered placeholder captures all remaining arguments.
pub fn substitute_placeholders(template: &str, arguments: &str) -> String {
let mut result = template.to_string();
// Replace $ARGUMENTS first
result = result.replace("$ARGUMENTS", arguments);
// Parse arguments (respecting quotes)
let args: Vec<&str> = parse_arguments(arguments);
// Find the highest numbered placeholder
let max_placeholder = find_max_placeholder(&result);
// Replace numbered placeholders
for i in 1..=max_placeholder {
let placeholder = format!("${i}");
let replacement = if i == max_placeholder {
// Last placeholder captures all remaining arguments
if i as usize <= args.len() {
args[(i as usize - 1)..].join(" ")
} else {
String::new()
}
} else {
// Regular placeholder gets single argument
args.get(i as usize - 1).copied().unwrap_or("").to_string()
};
result = result.replace(&placeholder, &replacement);
}
result
}
/// Parse arguments, respecting quoted strings.
fn parse_arguments(input: &str) -> Vec<&str> {
let input = input.trim();
if input.is_empty() {
return Vec::new();
}
// Simple split by whitespace for now
// Basic quote handling - enhanced parsing planned for future
input.split_whitespace().collect()
}
/// Find the highest numbered placeholder in the template.
fn find_max_placeholder(template: &str) -> u32 {
let re = Regex::new(r"\$(\d+)").unwrap_or_else(|_| {
Regex::new(r"\$\d").unwrap_or_else(|_| panic!("Regex compilation failed"))
});
re.captures_iter(template)
.filter_map(|cap| cap.get(1))
.filter_map(|m| m.as_str().parse::<u32>().ok())
.max()
.unwrap_or(0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_frontmatter_with_yaml() {
let content = r#"---
description: "Test command"
agent: "build"
model: "gpt-4o"
subtask: true
---
This is the template content."#;
let (config, template) = parse_frontmatter(content).unwrap();
assert_eq!(config.description, Some("Test command".to_string()));
assert_eq!(config.agent, Some("build".to_string()));
assert_eq!(config.model, Some("gpt-4o".to_string()));
assert_eq!(config.subtask, Some(true));
assert_eq!(template, "This is the template content.");
}
#[test]
fn test_parse_frontmatter_empty() {
let content = r#"---
---
Template only."#;
let (config, template) = parse_frontmatter(content).unwrap();
assert!(config.description.is_none());
assert_eq!(template, "Template only.");
}
#[test]
fn test_parse_frontmatter_no_yaml() {
let content = "Just a template without frontmatter.";
let (config, template) = parse_frontmatter(content).unwrap();
assert!(config.description.is_none());
assert_eq!(template, "Just a template without frontmatter.");
}
#[test]
fn test_parse_frontmatter_missing_closing() {
let content = r#"---
description: "Test"
No closing delimiter"#;
let result = parse_frontmatter(content);
assert!(result.is_err());
}
#[test]
fn test_hints_with_arguments() {
let template = "Run with $ARGUMENTS and $1 then $2";
let hints = hints(template);
assert!(hints.contains(&"$ARGUMENTS".to_string()));
assert!(hints.contains(&"$1".to_string()));
assert!(hints.contains(&"$2".to_string()));
}
#[test]
fn test_hints_numbered_only() {
let template = "First: $1, Second: $2, Third: $3";
let hints = hints(template);
assert_eq!(hints.len(), 3);
assert_eq!(hints[0], "$1");
assert_eq!(hints[1], "$2");
assert_eq!(hints[2], "$3");
}
#[test]
fn test_hints_no_placeholders() {
let template = "Static template with no placeholders";
let hints = hints(template);
assert!(hints.is_empty());
}
#[test]
fn test_substitute_arguments() {
let template = "Echo: $ARGUMENTS";
let result = substitute_placeholders(template, "hello world");
assert_eq!(result, "Echo: hello world");
}
#[test]
fn test_substitute_numbered() {
let template = "First: $1, Second: $2";
let result = substitute_placeholders(template, "one two");
assert_eq!(result, "First: one, Second: two");
}
#[test]
fn test_substitute_last_captures_rest() {
let template = "Cmd: $1 with rest: $2";
let result = substitute_placeholders(template, "first second third fourth");
assert_eq!(result, "Cmd: first with rest: second third fourth");
}
#[test]
fn test_substitute_missing_args() {
let template = "A: $1, B: $2, C: $3";
let result = substitute_placeholders(template, "only_one");
assert_eq!(result, "A: only_one, B: , C: ");
}
#[test]
fn test_command_parse() {
let content = r#"---
description: "Build command"
---
Build the project with $ARGUMENTS"#;
let cmd = Command::parse("build", content, PathBuf::from("/test/build.md")).unwrap();
assert_eq!(cmd.name, "build");
assert_eq!(cmd.description(), "Build command");
assert!(cmd.expects_arguments());
assert_eq!(
cmd.substitute("--release"),
"Build the project with --release"
);
}
}