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/// 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 file: &'static str,
38    pub source_str: &'static str,
39    pub count: &'static str,
40    pub bindings: HashMap<String, BindingPayload>,
41}
42
43impl HooqMeta {
44    pub fn get_binding<T>(&self, key: &str) -> Option<Rc<T>>
45    where
46        T: 'static,
47    {
48        self.bindings
49            .get(key)
50            .and_then(|binding| Rc::clone(&binding.value).downcast().ok())
51    }
52}