1use proc_macro::TokenStream;
6use quote::quote;
7use syn::*;
8
9#[proc_macro_attribute]
10pub fn dom_struct(args: TokenStream, input: TokenStream) -> TokenStream {
11 if !args.is_empty() {
12 panic!("#[dom_struct] takes no arguments");
13 }
14 let attributes = quote! {
15 #[derive(deny_public_fields::DenyPublicFields, domobject_derive::DomObject, JSTraceable, MallocSizeOf)]
16 #[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
17 #[repr(C)]
18 };
19
20 let attributes: TokenStream = attributes.to_string().parse().unwrap();
22
23 let output: TokenStream = attributes.into_iter().chain(input).collect();
24
25 let item: Item = syn::parse(output).unwrap();
26
27 if let Item::Struct(s) = item {
28 let s2 = s.clone();
29 if !s.generics.params.is_empty() {
30 return quote!(#s2).into();
31 }
32 if let Fields::Named(ref f) = s.fields {
33 let f = f.named.first().expect("Must have at least one field");
34 let ident = f.ident.as_ref().expect("Must have named fields");
35 let name = &s.ident;
36 let ty = &f.ty;
37
38 quote! (
39 #s2
40
41 impl crate::HasParent for #name {
42 type Parent = #ty;
43 fn as_parent(&self) -> &#ty {
46 &self.#ident
47 }
48 }
49 )
50 .into()
51 } else {
52 panic!("#[dom_struct] only applies to structs with named fields");
53 }
54 } else {
55 panic!("#[dom_struct] only applies to structs");
56 }
57}