@@ -5,10 +5,8 @@ use shlex::Shlex;
5
5
use std:: collections:: HashMap ;
6
6
use std:: collections:: HashSet ;
7
7
8
- static PROMPT_ARG_REGEX : Lazy < Regex > = Lazy :: new ( || {
9
- // Regex is a hard-coded literal; abort if it ever fails to compile.
10
- Regex :: new ( r"\$[A-Z][A-Z0-9_]*" ) . unwrap_or_else ( |_| std:: process:: abort ( ) )
11
- } ) ;
8
+ static PROMPT_ARG_REGEX : Lazy < Regex > =
9
+ Lazy :: new ( || Regex :: new ( r"\$[A-Z][A-Z0-9_]*" ) . unwrap_or_else ( |_| std:: process:: abort ( ) ) ) ;
12
10
13
11
#[ derive( Debug ) ]
14
12
pub enum PromptArgsError {
@@ -55,6 +53,11 @@ impl PromptExpansionError {
55
53
}
56
54
}
57
55
56
+ /// Extracts the unique placeholder variable names from a prompt template.
57
+ ///
58
+ /// A placeholder is any token that matches the pattern `$[A-Z][A-Z0-9_]*`
59
+ /// (for example `$USER`). The function returns the variable names without
60
+ /// the leading `$`, de-duplicated and in the order of first appearance.
58
61
pub fn prompt_argument_names ( content : & str ) -> Vec < String > {
59
62
let mut seen = HashSet :: new ( ) ;
60
63
let mut names = Vec :: new ( ) ;
@@ -68,6 +71,11 @@ pub fn prompt_argument_names(content: &str) -> Vec<String> {
68
71
names
69
72
}
70
73
74
+ /// Parses the `key=value` pairs that follow a custom prompt name.
75
+ ///
76
+ /// The input is split using shlex rules, so quoted values are supported
77
+ /// (for example `USER="Alice Smith"`). The function returns a map of parsed
78
+ /// arguments, or an error if a token is missing `=` or if the key is empty.
71
79
pub fn parse_prompt_inputs ( rest : & str ) -> Result < HashMap < String , String > , PromptArgsError > {
72
80
let mut map = HashMap :: new ( ) ;
73
81
if rest. trim ( ) . is_empty ( ) {
@@ -86,6 +94,11 @@ pub fn parse_prompt_inputs(rest: &str) -> Result<HashMap<String, String>, Prompt
86
94
Ok ( map)
87
95
}
88
96
97
+ /// Expands a message of the form `/name key=value …` using a matching saved prompt.
98
+ ///
99
+ /// If the text does not start with `/`, or if no prompt named `name` exists,
100
+ /// the function returns `Ok(None)`. On success it returns
101
+ /// `Ok(Some(expanded))`; otherwise it returns a descriptive error.
89
102
pub fn expand_custom_prompt (
90
103
text : & str ,
91
104
custom_prompts : & [ CustomPrompt ] ,
0 commit comments