Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_reflect/derive/src/custom_attributes.rs
6599 views
1
use proc_macro2::TokenStream;
2
use quote::quote;
3
use syn::{parse::ParseStream, Expr, Path, Token};
4
5
#[derive(Default, Clone)]
6
pub(crate) struct CustomAttributes {
7
attributes: Vec<Expr>,
8
}
9
10
impl 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
/// Is the collection empty?
32
pub fn is_empty(&self) -> bool {
33
self.attributes.is_empty()
34
}
35
36
/// Parse `@` (custom attribute) attribute.
37
///
38
/// Examples:
39
/// - `#[reflect(@Foo))]`
40
/// - `#[reflect(@Bar::baz("qux"))]`
41
/// - `#[reflect(@0..256u8)]`
42
pub fn parse_custom_attribute(&mut self, input: ParseStream) -> syn::Result<()> {
43
input.parse::<Token![@]>()?;
44
self.push(input.parse()?)
45
}
46
}
47
48