Add a handler inside a matcher group (`PreToolUse`, `PostToolUse`). Reuses an existing `""` matcher if present, otherwise appends a new one. Returns `true` if the handler was added.
(group: &mut Value, event: &str, command: &str)
| 1176 | /// Reuses an existing `""` matcher if present, otherwise appends a new one. |
| 1177 | /// Returns `true` if the handler was added. |
| 1178 | fn add_matcher_handler(group: &mut Value, event: &str, command: &str) -> bool { |
| 1179 | let Some(obj) = group.as_object_mut() else { |
| 1180 | return false; |
| 1181 | }; |
| 1182 | |
| 1183 | let entry = obj |
| 1184 | .entry(event.to_string()) |
| 1185 | .or_insert_with(|| Value::Array(Vec::new())); |
| 1186 | let Some(list) = entry.as_array_mut() else { |
| 1187 | return false; |
| 1188 | }; |
| 1189 | |
| 1190 | if event_list_has_atomic(list) { |
| 1191 | return false; |
| 1192 | } |
| 1193 | |
| 1194 | let mut handler = Map::new(); |
| 1195 | handler.insert("type".to_string(), Value::String("command".to_string())); |
| 1196 | handler.insert("command".to_string(), Value::String(command.to_string())); |
| 1197 | |
| 1198 | // Reuse an existing empty matcher group if one exists. |
| 1199 | for matcher_group in list.iter_mut() { |
| 1200 | if matcher_group.get("matcher").and_then(Value::as_str) == Some("") { |
| 1201 | if let Some(hooks) = matcher_group.get_mut("hooks").and_then(Value::as_array_mut) { |
| 1202 | hooks.push(Value::Object(handler)); |
| 1203 | return true; |
| 1204 | } |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | let mut matcher_group = Map::new(); |
| 1209 | matcher_group.insert("matcher".to_string(), Value::String(String::new())); |
| 1210 | matcher_group.insert( |
| 1211 | "hooks".to_string(), |
| 1212 | Value::Array(vec![Value::Object(handler)]), |
| 1213 | ); |
| 1214 | list.push(Value::Object(matcher_group)); |
| 1215 | true |
| 1216 | } |
| 1217 | |
| 1218 | /// Remove all Atomic hook commands from every event list in a group, |
| 1219 | /// dropping emptied matcher groups and empty event lists. |
no test coverage detected