Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_macro_utils/src/shape.rs
6595 views
1
use syn::{
2
punctuated::Punctuated, spanned::Spanned, token::Comma, Data, DataEnum, DataUnion, Error,
3
Field, Fields,
4
};
5
6
/// Get the fields of a data structure if that structure is a struct with named fields;
7
/// otherwise, return a compile error that points to the site of the macro invocation.
8
///
9
/// `meta` should be the name of the macro calling this function.
10
pub fn get_struct_fields<'a>(
11
data: &'a Data,
12
meta: &str,
13
) -> syn::Result<&'a Punctuated<Field, Comma>> {
14
match data {
15
Data::Struct(data_struct) => match &data_struct.fields {
16
Fields::Named(fields_named) => Ok(&fields_named.named),
17
Fields::Unnamed(fields_unnamed) => Ok(&fields_unnamed.unnamed),
18
Fields::Unit => Ok(const { &Punctuated::new() }),
19
},
20
Data::Enum(DataEnum { enum_token, .. }) => Err(Error::new(
21
enum_token.span(),
22
format!("#[{meta}] only supports structs, not enums"),
23
)),
24
Data::Union(DataUnion { union_token, .. }) => Err(Error::new(
25
union_token.span(),
26
format!("#[{meta}] only supports structs, not unions"),
27
)),
28
}
29
}
30
31