openzeppelin_monitor/utils/
expression.rs

1/// Splits an expression into a tuple of (left, operator, right)
2///
3/// # Arguments
4/// * `expr` - The expression to split
5///
6/// # Returns
7/// An Option containing the split expression if successful, None otherwise
8pub fn split_expression(expr: &str) -> Option<(&str, &str, &str)> {
9	// Find the operator position while respecting quotes
10	let mut in_quotes = false;
11	let mut operator_start = None;
12	let mut operator_end = None;
13
14	let operators = [
15		"==",
16		"!=",
17		">=",
18		"<=",
19		">",
20		"<",
21		"contains",
22		"starts_with",
23		"ends_with",
24	];
25
26	// First pass - find operator position
27	for (i, c) in expr.char_indices() {
28		if c == '\'' || c == '"' {
29			in_quotes = !in_quotes;
30			continue;
31		}
32
33		if !in_quotes {
34			// Check each operator
35			for op in operators {
36				if expr[i..].starts_with(op) {
37					operator_start = Some(i);
38					operator_end = Some(i + op.len());
39					break;
40				}
41			}
42			if operator_start.is_some() {
43				break;
44			}
45		}
46	}
47
48	// Split based on operator position
49	if let (Some(op_start), Some(op_end)) = (operator_start, operator_end) {
50		let left = expr[..op_start].trim();
51		let operator = expr[op_start..op_end].trim();
52		let right = expr[op_end..].trim();
53
54		// Remove surrounding quotes from right side if present
55		let right = right.trim_matches(|c| c == '\'' || c == '"');
56
57		Some((left, operator, right))
58	} else {
59		None
60	}
61}