hooq_helpers/
lib.rs

1#![doc = include_str!("../docs/README.md")]
2#![doc(
3    html_logo_url = "https://raw.githubusercontent.com/anotherhollow1125/hooq/refs/heads/main/assets/hooq_logo.svg"
4)]
5#![doc(
6    html_favicon_url = "https://raw.githubusercontent.com/anotherhollow1125/hooq/refs/heads/main/assets/hooq_logo.svg"
7)]
8
9use std::any::Any;
10use std::collections::HashMap;
11use std::fmt::Debug;
12use std::rc::Rc;
13
14/// A binding payload that contains the expression string and its value.
15///
16/// Value of this struct is stored in the `bindings` map's value of `HooqMeta` struct.
17pub struct BindingPayload {
18    pub expr: String,
19    pub value: Rc<dyn Any>,
20}
21
22impl Debug for BindingPayload {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        f.debug_struct("Binding").field("expr", &self.expr).finish()
25    }
26}
27
28/// Metadata about the invocation of the `hooq` macro.
29///
30/// You can use access this metadata via $hook_meta variable inside the macro body.
31/// You also can use this metadata via `hook` flavor method.
32#[derive(Debug)]
33pub struct HooqMeta {
34    pub line: usize,
35    pub column: usize,
36    pub path: &'static str,
37    pub abs_path: &'static str,
38    pub file: &'static str,
39    pub expr_str: &'static str,
40    pub count: &'static str,
41    pub bindings: HashMap<String, BindingPayload>,
42}
43
44impl HooqMeta {
45    pub fn get_binding<T>(&self, key: &str) -> Option<Rc<T>>
46    where
47        T: 'static,
48    {
49        self.bindings
50            .get(key)
51            .and_then(|binding| Rc::clone(&binding.value).downcast().ok())
52    }
53}