Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #13 from t-moe/master
Browse files Browse the repository at this point in the history
Generate impl for commands() -> Vec<BotCommand>
  • Loading branch information
p0lunin authored Jan 18, 2022
2 parents 611891a + 219406f commit e06263a
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ macro_rules! get_or_return {
match $($some)* {
Ok(elem) => elem,
Err(e) => return e
};
}
}
}

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e06263a

Please sign in to comment.