(&self, query: &str, input: Value)
| 9 | } |
| 10 | |
| 11 | pub fn execute(&self, query: &str, input: Value) -> Result<Vec<Value>> { |
| 12 | // Parse the jq-like query and execute it |
| 13 | let query = query.trim(); |
| 14 | |
| 15 | if query == "." { |
| 16 | return Ok(vec![input]); |
| 17 | } |
| 18 | |
| 19 | if query.starts_with(".[]") { |
| 20 | // Iterate over array |
| 21 | let rest = query.strip_prefix(".[]").unwrap_or(""); |
| 22 | return match input { |
| 23 | Value::Array(arr) => { |
| 24 | if rest.is_empty() { |
| 25 | Ok(arr) |
| 26 | } else { |
| 27 | let engine = QueryEngine::new(); |
| 28 | let mut results = Vec::new(); |
| 29 | for item in arr { |
| 30 | results.extend(engine.execute(rest, item)?); |
| 31 | } |
| 32 | Ok(results) |
| 33 | } |
| 34 | } |
| 35 | _ => Ok(vec![]), |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | if query.starts_with(".[") { |
| 40 | // Array index |
| 41 | if let Some(end) = query.find(']') { |
| 42 | let idx_str = &query[2..end]; |
| 43 | if let Ok(idx) = idx_str.parse::<usize>() { |
| 44 | let rest = &query[end + 1..]; |
| 45 | return match &input { |
| 46 | Value::Array(arr) => { |
| 47 | if let Some(item) = arr.get(idx) { |
| 48 | if rest.is_empty() { |
| 49 | Ok(vec![item.clone()]) |
| 50 | } else { |
| 51 | self.execute(rest, item.clone()) |
| 52 | } |
| 53 | } else { |
| 54 | Ok(vec![]) |
| 55 | } |
| 56 | } |
| 57 | _ => Ok(vec![]), |
| 58 | }; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if let Some(field_query) = query.strip_prefix('.') { |
| 64 | // Field access |
| 65 | let (field, rest) = if let Some(pos) = field_query.find(['.', '[', '|']) { |
| 66 | (&field_query[..pos], &field_query[pos..]) |
| 67 | } else { |
| 68 | (field_query, "") |
no test coverage detected