bevy_macro_utils/
shape.rs

1use proc_macro::Span;
2use syn::{punctuated::Punctuated, token::Comma, Data, DataStruct, Error, Field, Fields};
3
4/// Get the fields of a data structure if that structure is a struct with named fields;
5/// otherwise, return a compile error that points to the site of the macro invocation.
6pub fn get_struct_fields(data: &Data) -> syn::Result<&Punctuated<Field, Comma>> {
7    match data {
8        Data::Struct(DataStruct {
9            fields: Fields::Named(fields),
10            ..
11        }) => Ok(&fields.named),
12        Data::Struct(DataStruct {
13            fields: Fields::Unnamed(fields),
14            ..
15        }) => Ok(&fields.unnamed),
16        _ => Err(Error::new(
17            // This deliberately points to the call site rather than the structure
18            // body; marking the entire body as the source of the error makes it
19            // impossible to figure out which `derive` has a problem.
20            Span::call_site().into(),
21            "Only structs are supported",
22        )),
23    }
24}