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    /// Consumes the wrapper and returns the inner value.
22    pub fn inner(self) -> T {
23        self.0
24    }
25}
26
27impl<T: FromInput> std::ops::Deref for Attr<T> {
28    type Target = T;
29
30    fn deref(&self) -> &Self::Target {
31        &self.0
32    }
33}
34
35impl<T: FromInput> std::ops::DerefMut for Attr<T> {
36    fn deref_mut(&mut self) -> &mut Self::Target {
37        &mut self.0
38    }
39}
40
41impl<T: FromInput> FromInput for Attr<T> {
42    fn from_input(input: &Input) -> crate::Result<Self> {
43        T::from_input(input).map(Attr)
44    }
45}