dremoc-macro 0.1.6

Procedural macros for dremoc
Documentation
use heck::{ToPascalCase, ToSnakeCase};
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens};
use std::fmt::Display;
use syn::{Attribute, Ident};

pub fn to_pascal_case(ident: &Ident) -> Ident {
    to_pascal_case_ext(ident, "", "")
}

pub fn to_pascal_case_ext(ident: &Ident, prefix: impl Display, postfix: impl Display) -> Ident {
    let new_ident = format!("{} {} {}", prefix, ident, postfix);

    Ident::new(&new_ident.to_pascal_case(), ident.span())
}

pub fn to_snake_case(ident: &Ident) -> Ident {
    to_snake_case_ext(ident, "", "")
}

pub fn to_snake_case_ext(ident: &Ident, prefix: impl Display, postfix: impl Display) -> Ident {
    let new_ident = format!("{} {} {}", prefix, ident, postfix);

    Ident::new(&new_ident.to_snake_case(), ident.span())
}

pub fn hide_ident(ident: &Ident) -> Ident {
    let new_ident = format!("__{}", ident);

    Ident::new(&new_ident, ident.span())
}

pub fn attribute_tokens(attrs: &[Attribute]) -> TokenStream2 {
    let mut tokens = quote! {};
    for attr in attrs {
        attr.to_tokens(&mut tokens);
    }
    tokens
}

pub fn trait_macro_attribute_tokens(send: bool) -> TokenStream2 {
    #[cfg(feature = "use-async-trait")]
    return if send {
        quote! { #[::dremoc::service::async_trait::async_trait] }
    } else {
        quote! { #[::dremoc::service::async_trait::async_trait(?Send)] }
    };

    #[cfg(feature = "use-trait-variant")]
    return if send {
        quote! { #[::dremoc::service::trait_variant::make(Send)] }
    } else {
        quote! { #[::dremoc::service::trait_variant::make] }
    };

    #[cfg(not(any(feature = "use-async-trait", feature = "use-trait-variant")))]
    return quote! {};
}

pub fn impl_macro_attribute_tokens(send: bool) -> TokenStream2 {
    #[cfg(feature = "use-async-trait")]
    return if send {
        quote! { #[::dremoc::service::async_trait::async_trait] }
    } else {
        quote! { #[::dremoc::service::async_trait::async_trait(?Send)] }
    };

    #[cfg(not(feature = "use-async-trait"))]
    return if send {
        quote! {}
    } else {
        quote! {}
    };
}