/// Expand the visitor model into its implementation
Expand the visitor model into its implementation
Expand the visitor model into its implementation
pub fn expand(&self) -> TokenStream {
        // Store each of the three parts
        let mut trait_functions: Vec<TokenStream> = Vec::new();
        let mut helpers: Vec<TokenStream> = Vec::new();
        let mut visitables: Vec<TokenStream> = Vec::new();
        // Loop over each type given
        for t in self.types.iter() {
            let elem_name = t.inner_type.ident.to_lowercase();
            let elem_type = &t.inner_type;
            let fn_name = format_ident!("visit_{}", elem_name);
            let options = Options::new(&t.attrs.options);
            // Get trait function
            if options.no_default {
                trait_functions.push(quote! {
                    fn #fn_name(&mut self, #elem_name: &#elem_type);
                })
            } else {
                trait_functions.push(quote! {
                    fn #fn_name(&mut self, #elem_name: &#elem_type) {
                        #fn_name(self, #elem_name)
                    }
                })
            };
            // Get helper function
            if options.has_helper {
                if let Some(inner) = options.helper_tmpl {
                    helpers.push(quote! {
                        pub fn #fn_name<V>(visitor: &mut V, #elem_name: &#elem_type)
                        where
                            V: Visitor + ?Sized,
                        {
                            #inner
                        }
                    });
                } else {
                    let unused_elem_name = format_ident!("_{}", elem_name);
                    helpers.push(quote! {
                        pub fn #fn_name<V>(_visitor: &mut V, #unused_elem_name: &#elem_type)
                        where
                            V: Visitor + ?Sized,
                        {
                        }
                    });
                }
            };
            // Make visitable
            visitables.push(quote! {
                impl Visitable for #elem_type {
                    fn apply(&self, visitor: &mut dyn Visitor) {
                        visitor.#fn_name(self);
                    }
                }
            });
        }
        // Built complete visitor implementation
        quote! {
            pub trait Visitor {
                #(#trait_functions)*
            }
            #(#helpers)*
            trait Visitable {
                fn apply(&self, visitor: &mut dyn Visitor);
            }
            #(#visitables)*
        }
    }
pub fn expand ( & self ) -> TokenStream { // Store each of the three parts let mut trait_functions : Vec < TokenStream > = Vec :: new ( ) ; let mut helpers : Vec < TokenStream > = Vec :: new ( ) ; let mut visitables : Vec < TokenStream > = Vec :: new ( ) ; // Loop over each type given for t in self . types . iter ( ) { let elem_name = t . inner_type . ident . to_lowercase ( ) ; let elem_type = & t . inner_type ; let fn_name = format_ident ! ( " " elem_name ) ; let options = Options :: new ( & t . attrs . options ) ; // Get trait function if options . no_default { trait_functions . push ( quote ! { fn fn_name ( mut self elem_name elem_type ) } ) } else { trait_functions . push ( quote ! { fn fn_name ( mut self elem_name elem_type ) { fn_name ( self elem_name ) } } ) } ; // Get helper function if options . has_helper { if let Some ( inner ) = options . helper_tmpl { helpers . push ( quote ! { pub fn fn_name V ( visitor mut V elem_name elem_type ) where V Visitor Sized { inner } } ) ; } else { let unused_elem_name = format_ident ! ( " " elem_name ) ; helpers . push ( quote ! { pub fn fn_name V ( _visitor mut V unused_elem_name elem_type ) where V Visitor Sized { } } ) ; } } ; // Make visitable visitables . push ( quote ! { impl Visitable for elem_type { fn apply ( self visitor mut dyn Visitor ) { visitor fn_name ( self ) } } } ) ; } // Built complete visitor implementation quote ! { pub trait Visitor { ( trait_functions ) } ( helpers ) trait Visitable { fn apply ( self visitor mut dyn Visitor ) } ( visitables ) } }
Expand the visitor model into its implementation
Expand the visitor model into its implementation
&self