|
| 1 | +use { |
| 2 | + proc_macro2::{Ident, Span}, |
| 3 | + quote::{quote, quote_spanned}, |
| 4 | + syn::{ |
| 5 | + Error, Generics, Item, ItemStruct, LitStr, |
| 6 | + parse::{Parse, ParseStream}, |
| 7 | + parse_macro_input, |
| 8 | + spanned::Spanned, |
| 9 | + }, |
| 10 | +}; |
| 11 | + |
| 12 | +pub fn derive_prepare_drm_object_properties( |
| 13 | + input: proc_macro::TokenStream, |
| 14 | +) -> proc_macro::TokenStream { |
| 15 | + let input: Input = parse_macro_input!(input as Input); |
| 16 | + let mut reset_body = vec![]; |
| 17 | + let mut update_body = vec![]; |
| 18 | + let mut prepare_body = vec![]; |
| 19 | + let mut differs_body = vec![]; |
| 20 | + let mut prepare_conditional_body = vec![]; |
| 21 | + for field in input.fields { |
| 22 | + let name = LitStr::new(&field.to_string(), field.span()); |
| 23 | + reset_body.push(quote! { |
| 24 | + crate::video::drm::PrepareDrmObjectProperty::reset(&mut self.#field); |
| 25 | + }); |
| 26 | + update_body.push(quote! { |
| 27 | + crate::video::drm::PrepareDrmObjectProperty::update(&mut self.#field, p); |
| 28 | + }); |
| 29 | + prepare_body.push(quote! { |
| 30 | + crate::video::drm::PrepareDrmObjectProperty::prepare(&self.#field, change, #name, logging); |
| 31 | + }); |
| 32 | + differs_body.push(quote! { |
| 33 | + crate::video::drm::PrepareDrmObjectProperty::differs(&self.#field, &old.#field) |
| 34 | + }); |
| 35 | + prepare_conditional_body.push(quote! { |
| 36 | + crate::video::drm::PrepareDrmObjectProperty::prepare_conditional(&self.#field, &old.#field, change, #name, logging); |
| 37 | + }); |
| 38 | + } |
| 39 | + let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl(); |
| 40 | + let ident = input.ident; |
| 41 | + let res = quote_spanned! { input.span => |
| 42 | + const _: () = { |
| 43 | + impl #impl_generics |
| 44 | + crate::utils::reset::Reset for #ident #type_generics |
| 45 | + #where_clause |
| 46 | + { |
| 47 | + fn reset(&mut self) { |
| 48 | + #(#reset_body)* |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + impl #impl_generics |
| 53 | + crate::video::drm::PrepareDrmObjectProperties for #ident #type_generics |
| 54 | + #where_clause |
| 55 | + { |
| 56 | + fn update( |
| 57 | + &mut self, |
| 58 | + p: &crate::utils::bhash::BHashMap<crate::video::drm::DrmProperty, u64>, |
| 59 | + ) { |
| 60 | + #(#update_body)* |
| 61 | + } |
| 62 | + fn prepare( |
| 63 | + &self, |
| 64 | + change: &mut crate::video::drm::ObjectChange<'_>, |
| 65 | + logging: Option<&crate::video::drm::Logging>, |
| 66 | + ) { |
| 67 | + #(#prepare_body)* |
| 68 | + } |
| 69 | + fn differs( |
| 70 | + &self, |
| 71 | + old: &Self, |
| 72 | + ) -> bool { |
| 73 | + #(#differs_body)||* |
| 74 | + } |
| 75 | + fn prepare_conditional( |
| 76 | + &self, |
| 77 | + old: &Self, |
| 78 | + change: &mut crate::video::drm::ObjectChange<'_>, |
| 79 | + logging: Option<&crate::video::drm::Logging>, |
| 80 | + ) { |
| 81 | + #(#prepare_conditional_body)* |
| 82 | + } |
| 83 | + } |
| 84 | + }; |
| 85 | + }; |
| 86 | + res.into() |
| 87 | +} |
| 88 | + |
| 89 | +struct Input { |
| 90 | + span: Span, |
| 91 | + ident: Ident, |
| 92 | + generics: Generics, |
| 93 | + fields: Vec<Ident>, |
| 94 | +} |
| 95 | + |
| 96 | +impl Input { |
| 97 | + fn parse_struct(input: ItemStruct) -> syn::Result<Self> { |
| 98 | + let span = input.span(); |
| 99 | + let mut fields = vec![]; |
| 100 | + for field in input.fields { |
| 101 | + let span = field.span(); |
| 102 | + let ident = field |
| 103 | + .ident |
| 104 | + .ok_or(Error::new(span, "field names are required"))?; |
| 105 | + fields.push(ident); |
| 106 | + } |
| 107 | + Ok(Self { |
| 108 | + span, |
| 109 | + ident: input.ident, |
| 110 | + generics: input.generics, |
| 111 | + fields, |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl Parse for Input { |
| 117 | + fn parse(input: ParseStream) -> syn::Result<Self> { |
| 118 | + let item: Item = input.parse()?; |
| 119 | + match item { |
| 120 | + Item::Struct(s) => Self::parse_struct(s), |
| 121 | + _ => Err(Error::new(item.span(), "expected struct")), |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments