MCPcopy Create free account
hub / github.com/aiscriptdev/aiscript / from_directive

Method from_directive

aiscript-directive/src/validator/regex.rs:40–65  ·  view source on GitHub ↗
(directive: Directive)

Source from the content-addressed store, hash-verified

38
39impl FromDirective for RegexValidator {
40 fn from_directive(directive: Directive) -> Result<Self, String> {
41 // Only support KeyValue format with "pattern" parameter
42 match &directive.params {
43 DirectiveParams::KeyValue(params) => {
44 // Get the pattern parameter
45 let pattern_str = params
46 .get("pattern")
47 .and_then(|v| v.as_str())
48 .ok_or_else(|| "@regex directive requires a 'pattern' parameter".to_string())?;
49
50 // Compile the regex
51 let regex = match Regex::new(pattern_str) {
52 Ok(re) => re,
53 Err(e) => return Err(format!("Invalid regex pattern: {}", e)),
54 };
55
56 Ok(Self {
57 pattern: regex,
58 raw_pattern: pattern_str.to_string(),
59 })
60 }
61 _ => {
62 Err("Invalid format for @regex directive. Use @regex(pattern=\"...\")".to_string())
63 }
64 }
65 }
66}
67
68#[cfg(test)]

Callers

nothing calls this directly

Calls 2

getMethod · 0.45
as_strMethod · 0.45

Tested by

no test coverage detected