pub fn matches_tool_pattern(pattern: &str, tool_name: &str) -> bool {
if pattern == "*" {
return true;
}
if tool_name == pattern {
return true;
}
if let Some(prefix) = pattern.strip_suffix("*)")
&& tool_name.starts_with(prefix)
{
return true;
}
if tool_name.starts_with(pattern)
&& tool_name.len() > pattern.len()
&& tool_name.as_bytes()[pattern.len()] == b'('
{
return true;
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_wildcard() {
assert!(matches_tool_pattern("*", "Bash"));
assert!(matches_tool_pattern("*", "Read"));
assert!(matches_tool_pattern("*", "Anything"));
}
#[test]
fn test_exact_match() {
assert!(matches_tool_pattern("Bash", "Bash"));
assert!(!matches_tool_pattern("Bash", "Read"));
assert!(!matches_tool_pattern("Bash", "BashExtra"));
}
#[test]
fn test_prefix_with_paren() {
assert!(matches_tool_pattern("Bash", "Bash(rm:*)"));
assert!(matches_tool_pattern("Bash", "Bash(git:status)"));
assert!(!matches_tool_pattern("Bash", "BashExtra"));
}
#[test]
fn test_wildcard_suffix() {
assert!(matches_tool_pattern("Bash(rm:*)", "Bash(rm:rf)"));
assert!(matches_tool_pattern("Bash(rm:*)", "Bash(rm:*)"));
assert!(!matches_tool_pattern("Bash(rm:*)", "Bash(git:status)"));
}
}