bevy/crates/bevy_crevice/bevy-crevice-derive/src/glsl.rs
François 79d36e7c28 Prepare crevice for vendored release (#3394)
# Objective

- Our crevice is still called "crevice", which we can't use for a release
- Users would need to use our "crevice" directly to be able to use the derive macro

## Solution

- Rename crevice to bevy_crevice, and crevice-derive to bevy-crevice-derive
- Re-export it from bevy_render, and use it from bevy_render everywhere
- Fix derive macro to work either from bevy_render, from bevy_crevice, or from bevy

## Remaining

- It is currently re-exported as `bevy::render::bevy_crevice`, is it the path we want?
- After a brief suggestion to Cart, I changed the version to follow Bevy version instead of crevice, do we want that?
- Crevice README.md need to be updated
- in the `Cargo.toml`, there are a few things to change. How do we want to change them? How do we keep attributions to original Crevice?
```
authors = ["Lucien Greathouse <me@lpghatguy.com>"]
documentation = "https://docs.rs/crevice"
homepage = "https://github.com/LPGhatguy/crevice"
repository = "https://github.com/LPGhatguy/crevice"
```


Co-authored-by: François <8672791+mockersf@users.noreply.github.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-12-23 22:49:12 +00:00

51 lines
1.8 KiB
Rust

use proc_macro2::{Literal, TokenStream};
use quote::quote;
use syn::{parse_quote, Data, DeriveInput, Fields, Path};
pub fn emit(input: DeriveInput) -> TokenStream {
let bevy_crevice_path = crate::bevy_crevice_path();
let fields = match &input.data {
Data::Struct(data) => match &data.fields {
Fields::Named(fields) => fields,
Fields::Unnamed(_) => panic!("Tuple structs are not supported"),
Fields::Unit => panic!("Unit structs are not supported"),
},
Data::Enum(_) | Data::Union(_) => panic!("Only structs are supported"),
};
let base_trait_path: Path = parse_quote!(#bevy_crevice_path::glsl::Glsl);
let struct_trait_path: Path = parse_quote!(#bevy_crevice_path::glsl::GlslStruct);
let name = input.ident;
let name_str = Literal::string(&name.to_string());
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let glsl_fields = fields.named.iter().map(|field| {
let field_ty = &field.ty;
let field_name_str = Literal::string(&field.ident.as_ref().unwrap().to_string());
let field_as = quote! {<#field_ty as #bevy_crevice_path::glsl::GlslArray>};
quote! {
s.push_str("\t");
s.push_str(#field_as::NAME);
s.push_str(" ");
s.push_str(#field_name_str);
<#field_as::ArraySize as #bevy_crevice_path::glsl::DimensionList>::push_to_string(s);
s.push_str(";\n");
}
});
quote! {
unsafe impl #impl_generics #base_trait_path for #name #ty_generics #where_clause {
const NAME: &'static str = #name_str;
}
unsafe impl #impl_generics #struct_trait_path for #name #ty_generics #where_clause {
fn enumerate_fields(s: &mut String) {
#( #glsl_fields )*
}
}
}
}