Merge pull request #13 from t-moe/master

Generate impl for commands() -> Vec<BotCommand>
This commit is contained in:
Dmytro Polunin 2022-01-18 16:26:42 +02:00 committed by GitHub
commit e06263a659
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -192,7 +192,7 @@ macro_rules! get_or_return {
match $($some)* {
Ok(elem) => elem,
Err(e) => return e
};
}
}
}
@ -257,17 +257,40 @@ pub fn derive_telegram_command_enum(tokens: TokenStream) -> TokenStream {
let fn_descriptions = impl_descriptions(&variant_infos, &command_enum);
let fn_parse = impl_parse(&variant_infos, &command_enum, &vec_impl_create);
let fn_commands = impl_commands(&variant_infos, &command_enum);
let trait_impl = quote! {
impl BotCommand for #ident {
#fn_descriptions
#fn_parse
#fn_commands
}
};
TokenStream::from(trait_impl)
}
fn impl_commands(
infos: &[Command],
global: &CommandEnum,
) -> quote::__private::TokenStream {
let commands_to_list = infos.iter().filter_map(|command| {
if command.description == Some("off".into()) {
None
} else {
let c = command.get_matched_value(global);
let d = command.description.as_deref().unwrap_or_default();
Some(quote! { BotCommand::new(#c,#d) })
}
});
quote! {
fn bot_commands() -> Vec<teloxide::types::BotCommand> {
use teloxide::types::BotCommand;
vec![#(#commands_to_list),*]
}
}
}
fn impl_descriptions(
infos: &[Command],
global: &CommandEnum,