diff --git a/teloxide-macros/src/attr.rs b/teloxide-macros/src/attr.rs index 281baa37..f57c1783 100644 --- a/teloxide-macros/src/attr.rs +++ b/teloxide-macros/src/attr.rs @@ -1,11 +1,12 @@ -use syn::parse::{Parse, ParseStream}; -use syn::{LitStr, Token}; - +use syn::{ + parse::{Parse, ParseStream}, + LitStr, Token, +}; pub enum BotCommandAttribute { Prefix, Description, - RenameRule + RenameRule, } impl Parse for BotCommandAttribute { @@ -15,27 +16,23 @@ impl Parse for BotCommandAttribute { "prefix" => Ok(BotCommandAttribute::Prefix), "description" => Ok(BotCommandAttribute::Description), "rename" => Ok(BotCommandAttribute::RenameRule), - _ => Err(syn::Error::new(name_arg.span(), "unexpected argument")) + _ => Err(syn::Error::new(name_arg.span(), "unexpected argument")), } } } pub struct Attr { name: BotCommandAttribute, - value: String + value: String, } -impl Parse for Attr -{ +impl Parse for Attr { fn parse(input: ParseStream) -> Result { let name = input.parse::()?; input.parse::()?; let value = input.parse::()?.value(); - Ok(Self { - name, - value - }) + Ok(Self { name, value }) } } @@ -50,7 +47,7 @@ impl Attr { } pub struct VecAttrs { - pub data: Vec + pub data: Vec, } impl Parse for VecAttrs { @@ -62,8 +59,6 @@ impl Parse for VecAttrs { input.parse::()?; } } - Ok(Self { - data - }) + Ok(Self { data }) } } diff --git a/teloxide-macros/src/command.rs b/teloxide-macros/src/command.rs index 1569b5c8..69be6e58 100644 --- a/teloxide-macros/src/command.rs +++ b/teloxide-macros/src/command.rs @@ -1,5 +1,7 @@ -use crate::attr::{Attr, BotCommandAttribute}; -use crate::rename_rules::rename_by_rule; +use crate::{ + attr::{Attr, BotCommandAttribute}, + rename_rules::rename_by_rule, +}; pub struct Command { pub prefix: Option, @@ -33,7 +35,7 @@ impl Command { struct CommandAttrs { prefix: Option, description: Option, - rename: Option + rename: Option, } fn parse_attrs(attrs: &[Attr]) -> Result { @@ -44,7 +46,9 @@ fn parse_attrs(attrs: &[Attr]) -> Result { for attr in attrs { match attr.name() { BotCommandAttribute::Prefix => prefix = Some(attr.value()), - BotCommandAttribute::Description => description = Some(attr.value()), + BotCommandAttribute::Description => { + description = Some(attr.value()) + } BotCommandAttribute::RenameRule => rename_rule = Some(attr.value()), #[allow(unreachable_patterns)] _ => return Err("unexpected attribute".to_owned()), @@ -54,6 +58,6 @@ fn parse_attrs(attrs: &[Attr]) -> Result { Ok(CommandAttrs { prefix, description, - rename: rename_rule + rename: rename_rule, }) -} \ No newline at end of file +} diff --git a/teloxide-macros/src/enum_attributes.rs b/teloxide-macros/src/enum_attributes.rs index 08a0c021..b54dc9a6 100644 --- a/teloxide-macros/src/enum_attributes.rs +++ b/teloxide-macros/src/enum_attributes.rs @@ -15,14 +15,14 @@ impl CommandEnum { let rename = attrs.rename; if let Some(rename_rule) = &rename { match rename_rule.as_str() { - "lowercase" => {}, + "lowercase" => {} _ => return Err("disallowed value".to_owned()), } } Ok(Self { prefix, description, - rename_rule: rename + rename_rule: rename, }) } } @@ -30,7 +30,7 @@ impl CommandEnum { struct CommandAttrs { prefix: Option, description: Option, - rename: Option + rename: Option, } fn parse_attrs(attrs: &[Attr]) -> Result { @@ -41,7 +41,9 @@ fn parse_attrs(attrs: &[Attr]) -> Result { for attr in attrs { match attr.name() { BotCommandAttribute::Prefix => prefix = Some(attr.value()), - BotCommandAttribute::Description => description = Some(attr.value()), + BotCommandAttribute::Description => { + description = Some(attr.value()) + } BotCommandAttribute::RenameRule => rename_rule = Some(attr.value()), #[allow(unreachable_patterns)] _ => return Err("unexpected attribute".to_owned()), @@ -51,6 +53,6 @@ fn parse_attrs(attrs: &[Attr]) -> Result { Ok(CommandAttrs { prefix, description, - rename: rename_rule + rename: rename_rule, }) -} \ No newline at end of file +} diff --git a/teloxide-macros/src/lib.rs b/teloxide-macros/src/lib.rs index c0aa0842..b15f3492 100644 --- a/teloxide-macros/src/lib.rs +++ b/teloxide-macros/src/lib.rs @@ -5,13 +5,15 @@ mod rename_rules; extern crate proc_macro; extern crate syn; +use crate::{ + attr::{Attr, VecAttrs}, + command::Command, + enum_attributes::CommandEnum, + rename_rules::rename_by_rule, +}; use proc_macro::TokenStream; use quote::{quote, ToTokens}; -use syn::{DeriveInput, parse_macro_input}; -use crate::command::Command; -use crate::attr::{Attr, VecAttrs}; -use crate::enum_attributes::CommandEnum; -use crate::rename_rules::rename_by_rule; +use syn::{parse_macro_input, DeriveInput}; macro_rules! get_or_return { ($($some:tt)*) => { @@ -35,7 +37,8 @@ pub fn derive_telegram_command_enum(tokens: TokenStream) -> TokenStream { Err(e) => return compile_error(e), }; - let variants: Vec<&syn::Variant> = data_enum.variants.iter().map(|attr| attr).collect(); + let variants: Vec<&syn::Variant> = + data_enum.variants.iter().map(|attr| attr).collect(); let mut variant_infos = vec![]; for variant in variants.iter() { @@ -44,10 +47,10 @@ pub fn derive_telegram_command_enum(tokens: TokenStream) -> TokenStream { match attr.parse_args::() { Ok(mut attrs_) => { attrs.append(attrs_.data.as_mut()); - }, + } Err(e) => { return compile_error(e.to_compile_error()); - }, + } } } match Command::try_from(attrs.as_slice(), &variant.ident.to_string()) { @@ -60,35 +63,34 @@ pub fn derive_telegram_command_enum(tokens: TokenStream) -> TokenStream { let variant_name = variant_infos.iter().map(|info| { if info.renamed { info.name.clone() - } - else if let Some(rename_rule) = &command_enum.rename_rule { + } else if let Some(rename_rule) = &command_enum.rename_rule { rename_by_rule(&info.name, rename_rule) - } - else { + } else { info.name.clone() } }); let variant_prefixes = variant_infos.iter().map(|info| { - if let Some(prefix) = &info.prefix { - prefix - } - else if let Some(prefix) = &command_enum.prefix { - prefix - } - else { - "/" - } + if let Some(prefix) = &info.prefix { + prefix + } else if let Some(prefix) = &command_enum.prefix { + prefix + } else { + "/" + } }); - let variant_str1 = variant_prefixes.zip(variant_name).map(|(prefix, command)| prefix.to_string() + command.as_str()); + let variant_str1 = variant_prefixes + .zip(variant_name) + .map(|(prefix, command)| prefix.to_string() + command.as_str()); let variant_str2 = variant_str1.clone(); - let variant_description = variant_infos.iter().map(|info| info.description.as_deref().unwrap_or("")); + let variant_description = variant_infos + .iter() + .map(|info| info.description.as_deref().unwrap_or("")); let ident = &input.ident; let global_description = if let Some(s) = &command_enum.description { quote! { #s, "\n", } - } - else { + } else { quote! {} }; @@ -120,20 +122,22 @@ pub fn derive_telegram_command_enum(tokens: TokenStream) -> TokenStream { fn get_enum_data(input: &DeriveInput) -> Result<&syn::DataEnum, TokenStream> { match &input.data { syn::Data::Enum(data) => Ok(data), - _ => Err(compile_error("TelegramBotCommand allowed only for enums")) + _ => Err(compile_error("TelegramBotCommand allowed only for enums")), } } -fn parse_attributes(input: &[syn::Attribute]) -> Result, TokenStream> { +fn parse_attributes( + input: &[syn::Attribute], +) -> Result, TokenStream> { let mut enum_attrs = Vec::new(); for attr in input.iter() { match attr.parse_args::() { Ok(mut attrs_) => { enum_attrs.append(attrs_.data.as_mut()); - }, + } Err(e) => { return Err(compile_error(e.to_compile_error())); - }, + } } } Ok(enum_attrs) @@ -141,7 +145,7 @@ fn parse_attributes(input: &[syn::Attribute]) -> Result, TokenStream> fn compile_error(data: T) -> TokenStream where - T: ToTokens + T: ToTokens, { TokenStream::from(quote! { compile_error!(#data) }) }