use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse2, ItemStruct, Result};
fn to_snake_case(s: &str) -> String {
let mut out = String::with_capacity(s.len() + 4);
for (i, ch) in s.chars().enumerate() {
if ch.is_ascii_uppercase() {
if i != 0 {
out.push('_');
}
out.push(ch.to_ascii_lowercase());
} else {
out.push(ch);
}
}
out
}
pub fn expand_helper(_attr: TokenStream, item: TokenStream) -> Result<TokenStream> {
let input: ItemStruct = parse2(item)?;
let ident = &input.ident;
let name = to_snake_case(&ident.to_string());
Ok(quote! {
#input
impl ::doido_controller::Helper for #ident {
fn helper_name() -> &'static str {
#name
}
}
impl #ident {
pub fn helper_name() -> &'static str {
#name
}
}
})
}