Path: blob/main/crates/bevy_reflect/derive/src/custom_attributes.rs
6599 views
use proc_macro2::TokenStream;1use quote::quote;2use syn::{parse::ParseStream, Expr, Path, Token};34#[derive(Default, Clone)]5pub(crate) struct CustomAttributes {6attributes: Vec<Expr>,7}89impl CustomAttributes {10/// Generates a `TokenStream` for `CustomAttributes` construction.11pub fn to_tokens(&self, bevy_reflect_path: &Path) -> TokenStream {12let attributes = self.attributes.iter().map(|value| {13quote! {14.with_attribute(#value)15}16});1718quote! {19#bevy_reflect_path::attributes::CustomAttributes::default()20#(#attributes)*21}22}2324/// Inserts a custom attribute into the list.25pub fn push(&mut self, value: Expr) -> syn::Result<()> {26self.attributes.push(value);27Ok(())28}2930/// Is the collection empty?31pub fn is_empty(&self) -> bool {32self.attributes.is_empty()33}3435/// Parse `@` (custom attribute) attribute.36///37/// Examples:38/// - `#[reflect(@Foo))]`39/// - `#[reflect(@Bar::baz("qux"))]`40/// - `#[reflect(@0..256u8)]`41pub fn parse_custom_attribute(&mut self, input: ParseStream) -> syn::Result<()> {42input.parse::<Token![@]>()?;43self.push(input.parse()?)44}45}464748