Path: blob/main/crates/bevy_reflect/derive/src/ident.rs
6599 views
use proc_macro2::{Ident, Span};1use syn::Member;23/// Returns the "reflected" ident for a given string.4///5/// # Example6///7/// ```8/// # use proc_macro2::Ident;9/// # // We can't import this method because of its visibility.10/// # fn get_reflect_ident(name: &str) -> Ident {11/// # let reflected = format!("Reflect{name}");12/// # Ident::new(&reflected, proc_macro2::Span::call_site())13/// # }14/// let reflected: Ident = get_reflect_ident("Hash");15/// assert_eq!("ReflectHash", reflected.to_string());16/// ```17pub(crate) fn get_reflect_ident(name: &str) -> Ident {18let reflected = format!("Reflect{name}");19Ident::new(&reflected, Span::call_site())20}2122/// Returns a [`Member`] made of `ident` or `index` if `ident` is `None`.23///24/// Rust struct syntax allows for `Struct { foo: "string" }` with explicitly25/// named fields. It allows the `Struct { 0: "string" }` syntax when the struct26/// is declared as a tuple struct.27///28/// ```29/// struct Foo { field: &'static str }30/// struct Bar(&'static str);31/// let Foo { field } = Foo { field: "hi" };32/// let Bar { 0: field } = Bar { 0: "hello" };33/// let Bar(field) = Bar("hello"); // more common syntax34/// ```35///36/// This function helps field access in contexts where you are declaring either37/// a tuple struct or a struct with named fields. If you don't have a field name,38/// it means that you must access the field through an index.39pub(crate) fn ident_or_index(ident: Option<&Ident>, index: usize) -> Member {40ident.map_or_else(41|| Member::Unnamed(index.into()),42|ident| Member::Named(ident.clone()),43)44}454647