rglw_api_codegen 0.0.1

Rustyglware websocket api.
Documentation
use proc_macro2::TokenStream;
use quote::{quote};
use rglw::codegen_utils::derive_attributes::{DeriveStructFields};
use rglw::codegen_utils::get_type_path_segments;
use syn::DeriveInput;

pub fn implementation(input: DeriveInput) -> TokenStream {
    let ident = input.ident;
    let mut fields: Vec<TokenStream> = Vec::new();

    for struct_field in DeriveStructFields::new(input.data.clone()).get_fields() {
        let field_name = struct_field.ident;
        if field_name.is_none() {
            panic!("Injectable is not supported with struct unnamed fields");
        }
        let field_name = field_name.unwrap();
        let field_type = get_type_path_segments(struct_field.ty).first().unwrap().ident.clone();
        fields.push(quote! {
            #field_name: #field_type::inject().await?,
        });
    }

    let expanded = quote! {
        #[async_trait::async_trait]
        impl Injectable for #ident {
            async fn inject() -> Result<#ident, RglwApiError> {
                Ok(#ident {
                    #(#fields)*
                })
            }
        }
    };

    expanded
}