mockforge_core/overrides/
matcher.rs

1//! Target matching logic for overrides
2//!
3//! This module contains functions for determining whether an override rule
4//! should be applied to a given operation based on its targets.
5
6use super::models::OverrideRule;
7
8/// Check if an override rule matches the given operation
9pub fn matches_target(
10    rule: &OverrideRule,
11    operation_id: &str,
12    tags: &[String],
13    path: &str,
14    regex_cache: &std::collections::HashMap<String, regex::Regex>,
15) -> bool {
16    for target in &rule.targets {
17        if target.starts_with("operation:") {
18            let op_id = target.strip_prefix("operation:").unwrap();
19            if op_id == operation_id {
20                return true;
21            }
22        } else if target.starts_with("tag:") {
23            let tag = target.strip_prefix("tag:").unwrap();
24            if tags.contains(&tag.to_string()) {
25                return true;
26            }
27        } else if target.starts_with("regex:") {
28            let pattern = target.strip_prefix("regex:").unwrap();
29            if let Some(regex) = regex_cache.get(pattern) {
30                if regex.is_match(operation_id) {
31                    return true;
32                }
33            }
34        } else if target.starts_with("path:") {
35            let pattern = target.strip_prefix("path:").unwrap();
36            if let Some(regex) = regex_cache.get(pattern) {
37                if regex.is_match(path) {
38                    return true;
39                }
40            }
41        } else if target == "*" {
42            // Wildcard matches everything
43            return true;
44        }
45    }
46    false
47}