|
| 1 | +use proc_macro::TokenStream; |
| 2 | +use proc_macro2::TokenStream as TokenStream2; |
| 3 | +use quote::{quote, quote_spanned}; |
| 4 | +use syn::{parse_quote, spanned::Spanned}; |
| 5 | + |
| 6 | +/// Derive macro for `HasLoadProgress` |
| 7 | +/// |
| 8 | +/// May be used to implement `HasLoadProgress` on structs where all fields implement |
| 9 | +/// `HasLoadProgress`. |
| 10 | +/// |
| 11 | +/// Fields not implementing `HasLoadProgress` may be skipped with `#[has_load_progress(none)]` added |
| 12 | +/// to the field. |
| 13 | +/// |
| 14 | +/// `#[has_load_progress(none)]` may also be added to the struct itself to use the default |
| 15 | +/// implementation of `HasLoadProgress` which returns no progress and nothing to load. |
| 16 | +/// |
| 17 | +/// `#[has_load_progress(none)]` could also be added to enums to derive the default implementation |
| 18 | +/// with no load progress, but cannot be added to enum variants. |
| 19 | +#[proc_macro_derive(HasLoadProgress, attributes(has_load_progress))] |
| 20 | +pub fn has_load_progress(input: TokenStream) -> TokenStream { |
| 21 | + let input = syn::parse(input).unwrap(); |
| 22 | + |
| 23 | + impl_has_load_progress(&input).into() |
| 24 | +} |
| 25 | + |
| 26 | +fn impl_has_load_progress(input: &syn::DeriveInput) -> TokenStream2 { |
| 27 | + // The attribute that may be added to skip fields or use the default implementation. |
| 28 | + let no_load_attr: syn::Attribute = parse_quote! { |
| 29 | + #[has_load_progress(none)] |
| 30 | + }; |
| 31 | + |
| 32 | + let item_ident = &input.ident; |
| 33 | + let mut impl_function_body = quote! {}; |
| 34 | + |
| 35 | + // Check for `#[has_load_progress(none)]` on the item itself |
| 36 | + let mut skip_all_fields = false; |
| 37 | + for attr in &input.attrs { |
| 38 | + if attr.path == parse_quote!(has_load_progress) { |
| 39 | + if attr == &no_load_attr { |
| 40 | + skip_all_fields = true; |
| 41 | + } else { |
| 42 | + return quote_spanned!(attr.span() => |
| 43 | + compile_error!("Attribute must be `#[has_load_progress(none)]` if specified"); |
| 44 | + ); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // If we are skipping all fields |
| 50 | + if skip_all_fields { |
| 51 | + impl_function_body = quote! { |
| 52 | + crate::loading::progress::LoadProgress::default() |
| 53 | + }; |
| 54 | + |
| 55 | + // If we should process struct fields |
| 56 | + } else { |
| 57 | + // Parse the struct |
| 58 | + let in_struct = match &input.data { |
| 59 | + syn::Data::Struct(s) => s, |
| 60 | + syn::Data::Enum(_) | syn::Data::Union(_) => { |
| 61 | + return quote_spanned! { input.ident.span() => |
| 62 | + compile_error!("You may only derive HasLoadProgress on structs"); |
| 63 | + }; |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + // Start a list of the progresses for each field |
| 68 | + let mut progresses = Vec::new(); |
| 69 | + 'field: for field in &in_struct.fields { |
| 70 | + // Skip this field if it has `#[has_load_progress(none)]` |
| 71 | + for attr in &field.attrs { |
| 72 | + if attr.path == parse_quote!(has_load_progress) { |
| 73 | + if attr == &no_load_attr { |
| 74 | + continue 'field; |
| 75 | + } else { |
| 76 | + impl_function_body = quote_spanned! { attr.span() => |
| 77 | + compile_error!("Attribute be `#[has_load_progress(none)]` if specified"); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Add this fields load progress to the list of progresses |
| 84 | + let field_ident = field.ident.as_ref().expect("Field ident"); |
| 85 | + progresses.push(quote_spanned! { field_ident.span() => |
| 86 | + crate::loading::progress::HasLoadProgress::load_progress( |
| 87 | + &self.#field_ident, |
| 88 | + loading_resources |
| 89 | + ) |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + // Retrun the merged progress result |
| 94 | + impl_function_body = quote! { |
| 95 | + #impl_function_body |
| 96 | + crate::loading::progress::LoadProgress::merged([ #( #progresses),* ]) |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + // Fill out rest of impl block |
| 101 | + quote! { |
| 102 | + impl crate::loading::progress::HasLoadProgress for #item_ident { |
| 103 | + fn load_progress( |
| 104 | + &self, |
| 105 | + loading_resources: &crate::loading::progress::LoadingResources |
| 106 | + ) -> crate::loading::progress::LoadProgress { |
| 107 | + #impl_function_body |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments