swiftide_agents/util.rs
1//! Internal utility functions and macros for anything agent
2
3/// Simple macro to consistently call hooks and clean up the code
4#[macro_export]
5macro_rules! invoke_hooks {
6 ($hook_type:ident, $self_expr:expr $(, $arg:expr)* ) => {{
7 // Iterate through every hook matching `HookTypes::$hook_type`
8 for hook in $self_expr.hooks_by_type(HookTypes::$hook_type) {
9 // Downcast to the correct closure variant
10 if let Hook::$hook_type(hook_fn) = hook {
11 // Create a tracing span for instrumentation
12 let span = tracing::info_span!(
13 "hook",
14 "otel.name" = format!("hook.{:?}", HookTypes::$hook_type)
15 );
16 tracing::debug!("Calling {} hook", HookTypes::$hook_type);
17
18 // Call the hook, instrument, and log on failure
19 if let Err(err) = hook_fn($self_expr $(, $arg)*)
20 .instrument(span.or_current())
21 .await
22 {
23 tracing::error!(
24 "Error in {hooktype} hook: {err}",
25 hooktype = HookTypes::$hook_type,
26 );
27 }
28 }
29 }
30 }};
31}