mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-11 04:21:12 +01:00
added impl_parse function
This commit is contained in:
parent
9dc6aab3db
commit
fdf68463d9
1 changed files with 27 additions and 21 deletions
48
src/lib.rs
48
src/lib.rs
|
@ -61,33 +61,17 @@ pub fn derive_telegram_command_enum(tokens: TokenStream) -> TokenStream {
|
||||||
|
|
||||||
let fn_try_from = impl_try_parse_command(&variants, &variant_infos, &command_enum);
|
let fn_try_from = impl_try_parse_command(&variants, &variant_infos, &command_enum);
|
||||||
let fn_descriptions = impl_descriptions(&variant_infos, &command_enum);
|
let fn_descriptions = impl_descriptions(&variant_infos, &command_enum);
|
||||||
|
let fn_parse = impl_parse();
|
||||||
|
|
||||||
let expanded = quote! {
|
let trait_impl = quote! {
|
||||||
impl BotCommand for #ident {
|
impl BotCommand for #ident {
|
||||||
#fn_try_from
|
#fn_try_from
|
||||||
#fn_descriptions
|
#fn_descriptions
|
||||||
fn parse<N>(s: &str, bot_name: N) -> Option<(Self, Vec<&str>)>
|
#fn_parse
|
||||||
where
|
|
||||||
N: Into<String>
|
|
||||||
{
|
|
||||||
let mut words = s.split_whitespace();
|
|
||||||
let mut splited = words.next()?.split('@');
|
|
||||||
let command_raw = splited.next()?;
|
|
||||||
let bot = splited.next();
|
|
||||||
let bot_name = bot_name.into();
|
|
||||||
match bot {
|
|
||||||
Some(name) if name == bot_name => {}
|
|
||||||
None => {}
|
|
||||||
_ => return None,
|
|
||||||
}
|
|
||||||
let command = Self::try_from(command_raw)?;
|
|
||||||
Some((command, words.collect()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
//for debug
|
|
||||||
//println!("{}", &expanded.to_string());
|
TokenStream::from(trait_impl)
|
||||||
TokenStream::from(expanded)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn impl_try_parse_command(variants: &[&Variant], infos: &[Command], global: &CommandEnum) -> impl ToTokens {
|
fn impl_try_parse_command(variants: &[&Variant], infos: &[Command], global: &CommandEnum) -> impl ToTokens {
|
||||||
|
@ -127,6 +111,28 @@ fn impl_descriptions(infos: &[Command], global: &CommandEnum) -> impl ToTokens {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn impl_parse() -> impl ToTokens {
|
||||||
|
quote! {
|
||||||
|
fn parse<N>(s: &str, bot_name: N) -> Option<(Self, Vec<&str>)>
|
||||||
|
where
|
||||||
|
N: Into<String>
|
||||||
|
{
|
||||||
|
let mut words = s.split_whitespace();
|
||||||
|
let mut splited = words.next()?.split('@');
|
||||||
|
let command_raw = splited.next()?;
|
||||||
|
let bot = splited.next();
|
||||||
|
let bot_name = bot_name.into();
|
||||||
|
match bot {
|
||||||
|
Some(name) if name == bot_name => {}
|
||||||
|
None => {}
|
||||||
|
_ => return None,
|
||||||
|
}
|
||||||
|
let command = Self::try_from(command_raw)?;
|
||||||
|
Some((command, words.collect()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn get_enum_data(input: &DeriveInput) -> Result<&syn::DataEnum, TokenStream> {
|
fn get_enum_data(input: &DeriveInput) -> Result<&syn::DataEnum, TokenStream> {
|
||||||
match &input.data {
|
match &input.data {
|
||||||
syn::Data::Enum(data) => Ok(data),
|
syn::Data::Enum(data) => Ok(data),
|
||||||
|
|
Loading…
Reference in a new issue