hyperlane/hook/fn.rs
1use crate::*;
2
3/// Creates a new `ServerHookHandler` from a trait object.
4///
5/// # Arguments
6///
7/// - `ServerHook` - The trait object implementing `ServerHook`.
8///
9/// # Returns
10///
11/// - `ServerHookHandler` - A new `ServerHookHandler` instance.
12#[inline(always)]
13pub fn server_hook_factory<R>() -> ServerHookHandler
14where
15 R: ServerHook,
16{
17 Arc::new(move |ctx: &Context| -> SendableAsyncTask<()> {
18 let ctx: Context = ctx.clone();
19 Box::pin(async move {
20 R::new(&ctx).await.handle(&ctx).await;
21 })
22 })
23}
24
25/// Verify that each `Hook` in the list with the same type and non-zero priority is unique.
26///
27/// This function iterates over all provided `Hook` items and ensures that no two
28/// `Hook` items of the same type define the same non-zero `order`. If a duplicate
29/// is found, the function will panic at runtime.
30///
31/// # Arguments
32///
33/// - `Vec<HookMacro>`- A vector of `HookMacro` instances to be checked.
34///
35/// # Panics
36///
37/// - Panics if two or more `Hook` items of the same type define the same non-zero `order`.
38#[inline(always)]
39pub fn assert_hook_unique_order(list: Vec<HookMacro>) {
40 let mut seen: HashSet<(HookType, isize)> = HashSet::new();
41 list.iter()
42 .filter_map(|hook| {
43 hook.hook_type
44 .try_get()
45 .map(|order| (hook.hook_type, order))
46 })
47 .for_each(|(key, order)| {
48 if !seen.insert((key, order)) {
49 panic!("Duplicate hook detected: {} with order {}", key, order);
50 }
51 });
52}