/// Generates the code for writing the struct to bytes
///
/// # Params
///
/// * `struct_name` - Name of the struct
/// * `data`        - Information about the struct
///
/// # Returns
///
/// [proc_macro2::TokenStream] with the implementation of the write function.
///
Generates the code for writing the struct to bytes
Generates the code for writing the struct to bytes
fn generate_fn_write(struct_name: &Ident, data: &Data) -> TokenStream {
    match *data {
        Data::Struct(ref data) => {
            match data.fields.clone() {
                Fields::Named(ref fields) => {
                    let recurse = fields
                        .named
                        .iter()
                        .map(|f| {
                        let field_name = &f.ident;
                        match &f.ty {
                            Type::Path(_) => {
                                quote! {
                                    self.#field_name.write(buf).await?;
                                }
                            },
                            _ => {
                                crate::utils::error(
                                    f.ty.span(),
                                    "The given type is not supported.".into()
                                )
                            }
                        }
                    });
                    quote! {
                        #(#recurse)*
                    }
                },
                Fields::Unnamed(fields) => {
                    let datatype = fields
                        .unnamed
                        .iter()
                        .map(|f| {
                        match &f.ty {
                            Type::Path(_) => {
                                quote! {
                                    self.0.write(buf).await?;
                                }
                            },
                            _ => {
                                crate::utils::error(
                                    f.ty.span(),
                                    "The given type is not supported.".into()
                                )
                            }
                        }
                    });
                    quote! {
                        #(#datatype)*
                    }
                },
                Fields::Unit => quote! {
                    0u8.write(buf).await?;
                }
            }
        },
        Data::Enum(ref data) => {
            let fields = data.variants.iter().enumerate().map(|(i, v)| {
                let i = i as u8;
                let field_name = &v.ident;
                match v.fields {
                    Fields::Unnamed(ref fields) => {
                        let datatype = fields
                            .unnamed
                            .iter()
                            .map(|f| {
                            match &f.ty {
                                Type::Path(_) => {
                                    quote! {
                                        Self::#field_name(x) => {
                                            #i.write(buf).await?;
                                            x.write(buf).await?;
                                        }
                                    }
                                },
                                _ => {
                                    crate::utils::error(
                                        f.ty.span(),
                                        "The given type is not supported.".into()
                                    )
                                }
                            }
                        });
                        quote! {
                            #(#datatype)*
                        }
                    },
                    Fields::Unit => {
                        quote! {
                            Self::#field_name => {
                                #i.write(buf).await?;
                                cachem::EmptyMsg::default().write(buf).await?;
                            }
                        }
                    }
                    _ => {
                        crate::utils::error(
                            v.fields.span(),
                            "Only unnamed fields are supported.".into()
                        )
                    }
                }
            });
            quote! {
                match self {
                    #(#fields),*
                };
            }
        },
        _ => {
            struct_name.span().unwrap().error("Only structs and enums are supported.").emit();
            TokenStream::new()
        }
    }
}
fn generate_fn_write ( struct_name : & Ident , data : & Data ) -> TokenStream { match * data { Data :: Struct ( ref data ) => { match data . fields . clone ( ) { Fields :: Named ( ref fields ) => { let recurse = fields . named . iter ( ) . map ( | f | { let field_name = & f . ident ; match & f . ty { Type :: Path ( _ ) => { quote ! { self field_name write ( buf ) await } } , _ => { crate :: utils :: error ( f . ty . span ( ) , " " . into ( ) ) } } } ) ; quote ! { ( recurse ) } } , Fields :: Unnamed ( fields ) => { let datatype = fields . unnamed . iter ( ) . map ( | f | { match & f . ty { Type :: Path ( _ ) => { quote ! { self 0 write ( buf ) await } } , _ => { crate :: utils :: error ( f . ty . span ( ) , " " . into ( ) ) } } } ) ; quote ! { ( datatype ) } } , Fields :: Unit => quote ! { 0u8 write ( buf ) await } } } , Data :: Enum ( ref data ) => { let fields = data . variants . iter ( ) . enumerate ( ) . map ( | ( i , v ) | { let i = i as u8 ; let field_name = & v . ident ; match v . fields { Fields :: Unnamed ( ref fields ) => { let datatype = fields . unnamed . iter ( ) . map ( | f | { match & f . ty { Type :: Path ( _ ) => { quote ! { Self field_name ( x ) { i write ( buf ) await x write ( buf ) await } } } , _ => { crate :: utils :: error ( f . ty . span ( ) , " " . into ( ) ) } } } ) ; quote ! { ( datatype ) } } , Fields :: Unit => { quote ! { Self field_name { i write ( buf ) await cachem EmptyMsg default ( ) write ( buf ) await } } } _ => { crate :: utils :: error ( v . fields . span ( ) , " " . into ( ) ) } } } ) ; quote ! { match self { ( fields ) } } } , _ => { struct_name . span ( ) . unwrap ( ) . error ( " " ) . emit ( ) ; TokenStream :: new ( ) } } }
Generates the code for writing the struct to bytes
Generates the code for writing the struct to bytes
[proc_macro2::TokenStream] with the implementation of the write function.
[ proc_macro2 :: TokenStream ] with the implementation of the write function .
struct_name
&Ident
data
&Data
params
`struct_name` - Name of the struct
`data`        - Information about the struct
` struct_name ` - Name of the struct ` data ` - Information about the struct