bevy_reflect_derive/
custom_attributes.rs

1use proc_macro2::TokenStream;
2use quote::quote;
3use syn::{parse::ParseStream, Expr, Path, Token};
4
5#[derive(Default, Clone)]
6pub(crate) struct CustomAttributes {
7    attributes: Vec<Expr>,
8}
9
10impl CustomAttributes {
11    /// Generates a `TokenStream` for `CustomAttributes` construction.
12    pub fn to_tokens(&self, bevy_reflect_path: &Path) -> TokenStream {
13        let attributes = self.attributes.iter().map(|value| {
14            quote! {
15                .with_attribute(#value)
16            }
17        });
18
19        quote! {
20            #bevy_reflect_path::attributes::CustomAttributes::default()
21                #(#attributes)*
22        }
23    }
24
25    /// Inserts a custom attribute into the list.
26    pub fn push(&mut self, value: Expr) -> syn::Result<()> {
27        self.attributes.push(value);
28        Ok(())
29    }
30
31    /// Parse `@` (custom attribute) attribute.
32    ///
33    /// Examples:
34    /// - `#[reflect(@Foo))]`
35    /// - `#[reflect(@Bar::baz("qux"))]`
36    /// - `#[reflect(@0..256u8)]`
37    pub fn parse_custom_attribute(&mut self, input: ParseStream) -> syn::Result<()> {
38        input.parse::<Token![@]>()?;
39        self.push(input.parse()?)
40    }
41}