Skip to main content

zyn_core/extract/
attr.rs

1use crate::types::Input;
2
3use super::FromInput;
4
5/// Element extractor that resolves a `#[derive(Attribute)]` struct from the
6/// input's attributes.
7///
8/// Use as an element parameter type to auto-extract a parsed attribute config
9/// without passing it as a prop at the call site. Access the inner value via
10/// `Deref` or consume with `inner()`.
11///
12/// ```ignore
13/// #[zyn::element]
14/// fn my_element(#[zyn(input)] cfg: zyn::Attr<MyConfig>) -> proc_macro2::TokenStream {
15///     // cfg.my_field — accessed via Deref
16/// }
17/// ```
18pub struct Attr<T: FromInput>(T);
19
20impl<T: FromInput> Attr<T> {
21    pub fn inner(self) -> T {
22        self.0
23    }
24}
25
26impl<T: FromInput> std::ops::Deref for Attr<T> {
27    type Target = T;
28
29    fn deref(&self) -> &Self::Target {
30        &self.0
31    }
32}
33
34impl<T: FromInput> std::ops::DerefMut for Attr<T> {
35    fn deref_mut(&mut self) -> &mut Self::Target {
36        &mut self.0
37    }
38}
39
40impl<T: FromInput> FromInput for Attr<T> {
41    fn from_input(input: &Input) -> crate::Result<Self> {
42        T::from_input(input).map(Attr)
43    }
44}