mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-26 00:17:08 +01:00
Fix fmtcheck
This commit is contained in:
parent
4b0dea21f1
commit
14561e437f
4 changed files with 63 additions and 58 deletions
|
@ -1,11 +1,12 @@
|
||||||
use syn::parse::{Parse, ParseStream};
|
use syn::{
|
||||||
use syn::{LitStr, Token};
|
parse::{Parse, ParseStream},
|
||||||
|
LitStr, Token,
|
||||||
|
};
|
||||||
|
|
||||||
pub enum BotCommandAttribute {
|
pub enum BotCommandAttribute {
|
||||||
Prefix,
|
Prefix,
|
||||||
Description,
|
Description,
|
||||||
RenameRule
|
RenameRule,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for BotCommandAttribute {
|
impl Parse for BotCommandAttribute {
|
||||||
|
@ -15,27 +16,23 @@ impl Parse for BotCommandAttribute {
|
||||||
"prefix" => Ok(BotCommandAttribute::Prefix),
|
"prefix" => Ok(BotCommandAttribute::Prefix),
|
||||||
"description" => Ok(BotCommandAttribute::Description),
|
"description" => Ok(BotCommandAttribute::Description),
|
||||||
"rename" => Ok(BotCommandAttribute::RenameRule),
|
"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 {
|
pub struct Attr {
|
||||||
name: BotCommandAttribute,
|
name: BotCommandAttribute,
|
||||||
value: String
|
value: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for Attr
|
impl Parse for Attr {
|
||||||
{
|
|
||||||
fn parse(input: ParseStream) -> Result<Self, syn::Error> {
|
fn parse(input: ParseStream) -> Result<Self, syn::Error> {
|
||||||
let name = input.parse::<BotCommandAttribute>()?;
|
let name = input.parse::<BotCommandAttribute>()?;
|
||||||
input.parse::<Token![=]>()?;
|
input.parse::<Token![=]>()?;
|
||||||
let value = input.parse::<LitStr>()?.value();
|
let value = input.parse::<LitStr>()?.value();
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self { name, value })
|
||||||
name,
|
|
||||||
value
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +47,7 @@ impl Attr {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct VecAttrs {
|
pub struct VecAttrs {
|
||||||
pub data: Vec<Attr>
|
pub data: Vec<Attr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for VecAttrs {
|
impl Parse for VecAttrs {
|
||||||
|
@ -62,8 +59,6 @@ impl Parse for VecAttrs {
|
||||||
input.parse::<Token![,]>()?;
|
input.parse::<Token![,]>()?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Self {
|
Ok(Self { data })
|
||||||
data
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
use crate::attr::{Attr, BotCommandAttribute};
|
use crate::{
|
||||||
use crate::rename_rules::rename_by_rule;
|
attr::{Attr, BotCommandAttribute},
|
||||||
|
rename_rules::rename_by_rule,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct Command {
|
pub struct Command {
|
||||||
pub prefix: Option<String>,
|
pub prefix: Option<String>,
|
||||||
|
@ -33,7 +35,7 @@ impl Command {
|
||||||
struct CommandAttrs {
|
struct CommandAttrs {
|
||||||
prefix: Option<String>,
|
prefix: Option<String>,
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
rename: Option<String>
|
rename: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_attrs(attrs: &[Attr]) -> Result<CommandAttrs, String> {
|
fn parse_attrs(attrs: &[Attr]) -> Result<CommandAttrs, String> {
|
||||||
|
@ -44,7 +46,9 @@ fn parse_attrs(attrs: &[Attr]) -> Result<CommandAttrs, String> {
|
||||||
for attr in attrs {
|
for attr in attrs {
|
||||||
match attr.name() {
|
match attr.name() {
|
||||||
BotCommandAttribute::Prefix => prefix = Some(attr.value()),
|
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()),
|
BotCommandAttribute::RenameRule => rename_rule = Some(attr.value()),
|
||||||
#[allow(unreachable_patterns)]
|
#[allow(unreachable_patterns)]
|
||||||
_ => return Err("unexpected attribute".to_owned()),
|
_ => return Err("unexpected attribute".to_owned()),
|
||||||
|
@ -54,6 +58,6 @@ fn parse_attrs(attrs: &[Attr]) -> Result<CommandAttrs, String> {
|
||||||
Ok(CommandAttrs {
|
Ok(CommandAttrs {
|
||||||
prefix,
|
prefix,
|
||||||
description,
|
description,
|
||||||
rename: rename_rule
|
rename: rename_rule,
|
||||||
})
|
})
|
||||||
}
|
}
|
|
@ -15,14 +15,14 @@ impl CommandEnum {
|
||||||
let rename = attrs.rename;
|
let rename = attrs.rename;
|
||||||
if let Some(rename_rule) = &rename {
|
if let Some(rename_rule) = &rename {
|
||||||
match rename_rule.as_str() {
|
match rename_rule.as_str() {
|
||||||
"lowercase" => {},
|
"lowercase" => {}
|
||||||
_ => return Err("disallowed value".to_owned()),
|
_ => return Err("disallowed value".to_owned()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
prefix,
|
prefix,
|
||||||
description,
|
description,
|
||||||
rename_rule: rename
|
rename_rule: rename,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ impl CommandEnum {
|
||||||
struct CommandAttrs {
|
struct CommandAttrs {
|
||||||
prefix: Option<String>,
|
prefix: Option<String>,
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
rename: Option<String>
|
rename: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_attrs(attrs: &[Attr]) -> Result<CommandAttrs, String> {
|
fn parse_attrs(attrs: &[Attr]) -> Result<CommandAttrs, String> {
|
||||||
|
@ -41,7 +41,9 @@ fn parse_attrs(attrs: &[Attr]) -> Result<CommandAttrs, String> {
|
||||||
for attr in attrs {
|
for attr in attrs {
|
||||||
match attr.name() {
|
match attr.name() {
|
||||||
BotCommandAttribute::Prefix => prefix = Some(attr.value()),
|
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()),
|
BotCommandAttribute::RenameRule => rename_rule = Some(attr.value()),
|
||||||
#[allow(unreachable_patterns)]
|
#[allow(unreachable_patterns)]
|
||||||
_ => return Err("unexpected attribute".to_owned()),
|
_ => return Err("unexpected attribute".to_owned()),
|
||||||
|
@ -51,6 +53,6 @@ fn parse_attrs(attrs: &[Attr]) -> Result<CommandAttrs, String> {
|
||||||
Ok(CommandAttrs {
|
Ok(CommandAttrs {
|
||||||
prefix,
|
prefix,
|
||||||
description,
|
description,
|
||||||
rename: rename_rule
|
rename: rename_rule,
|
||||||
})
|
})
|
||||||
}
|
}
|
|
@ -5,13 +5,15 @@ mod rename_rules;
|
||||||
|
|
||||||
extern crate proc_macro;
|
extern crate proc_macro;
|
||||||
extern crate syn;
|
extern crate syn;
|
||||||
|
use crate::{
|
||||||
|
attr::{Attr, VecAttrs},
|
||||||
|
command::Command,
|
||||||
|
enum_attributes::CommandEnum,
|
||||||
|
rename_rules::rename_by_rule,
|
||||||
|
};
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use quote::{quote, ToTokens};
|
use quote::{quote, ToTokens};
|
||||||
use syn::{DeriveInput, parse_macro_input};
|
use syn::{parse_macro_input, DeriveInput};
|
||||||
use crate::command::Command;
|
|
||||||
use crate::attr::{Attr, VecAttrs};
|
|
||||||
use crate::enum_attributes::CommandEnum;
|
|
||||||
use crate::rename_rules::rename_by_rule;
|
|
||||||
|
|
||||||
macro_rules! get_or_return {
|
macro_rules! get_or_return {
|
||||||
($($some:tt)*) => {
|
($($some:tt)*) => {
|
||||||
|
@ -35,7 +37,8 @@ pub fn derive_telegram_command_enum(tokens: TokenStream) -> TokenStream {
|
||||||
Err(e) => return compile_error(e),
|
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![];
|
let mut variant_infos = vec![];
|
||||||
for variant in variants.iter() {
|
for variant in variants.iter() {
|
||||||
|
@ -44,10 +47,10 @@ pub fn derive_telegram_command_enum(tokens: TokenStream) -> TokenStream {
|
||||||
match attr.parse_args::<VecAttrs>() {
|
match attr.parse_args::<VecAttrs>() {
|
||||||
Ok(mut attrs_) => {
|
Ok(mut attrs_) => {
|
||||||
attrs.append(attrs_.data.as_mut());
|
attrs.append(attrs_.data.as_mut());
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return compile_error(e.to_compile_error());
|
return compile_error(e.to_compile_error());
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match Command::try_from(attrs.as_slice(), &variant.ident.to_string()) {
|
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| {
|
let variant_name = variant_infos.iter().map(|info| {
|
||||||
if info.renamed {
|
if info.renamed {
|
||||||
info.name.clone()
|
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)
|
rename_by_rule(&info.name, rename_rule)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
info.name.clone()
|
info.name.clone()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let variant_prefixes = variant_infos.iter().map(|info| {
|
let variant_prefixes = variant_infos.iter().map(|info| {
|
||||||
if let Some(prefix) = &info.prefix {
|
if let Some(prefix) = &info.prefix {
|
||||||
prefix
|
prefix
|
||||||
}
|
} else if let Some(prefix) = &command_enum.prefix {
|
||||||
else if let Some(prefix) = &command_enum.prefix {
|
prefix
|
||||||
prefix
|
} else {
|
||||||
}
|
"/"
|
||||||
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_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 ident = &input.ident;
|
||||||
|
|
||||||
let global_description = if let Some(s) = &command_enum.description {
|
let global_description = if let Some(s) = &command_enum.description {
|
||||||
quote! { #s, "\n", }
|
quote! { #s, "\n", }
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
quote! {}
|
quote! {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -120,20 +122,22 @@ pub fn derive_telegram_command_enum(tokens: TokenStream) -> TokenStream {
|
||||||
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),
|
||||||
_ => Err(compile_error("TelegramBotCommand allowed only for enums"))
|
_ => Err(compile_error("TelegramBotCommand allowed only for enums")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_attributes(input: &[syn::Attribute]) -> Result<Vec<Attr>, TokenStream> {
|
fn parse_attributes(
|
||||||
|
input: &[syn::Attribute],
|
||||||
|
) -> Result<Vec<Attr>, TokenStream> {
|
||||||
let mut enum_attrs = Vec::new();
|
let mut enum_attrs = Vec::new();
|
||||||
for attr in input.iter() {
|
for attr in input.iter() {
|
||||||
match attr.parse_args::<VecAttrs>() {
|
match attr.parse_args::<VecAttrs>() {
|
||||||
Ok(mut attrs_) => {
|
Ok(mut attrs_) => {
|
||||||
enum_attrs.append(attrs_.data.as_mut());
|
enum_attrs.append(attrs_.data.as_mut());
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return Err(compile_error(e.to_compile_error()));
|
return Err(compile_error(e.to_compile_error()));
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(enum_attrs)
|
Ok(enum_attrs)
|
||||||
|
@ -141,7 +145,7 @@ fn parse_attributes(input: &[syn::Attribute]) -> Result<Vec<Attr>, TokenStream>
|
||||||
|
|
||||||
fn compile_error<T>(data: T) -> TokenStream
|
fn compile_error<T>(data: T) -> TokenStream
|
||||||
where
|
where
|
||||||
T: ToTokens
|
T: ToTokens,
|
||||||
{
|
{
|
||||||
TokenStream::from(quote! { compile_error!(#data) })
|
TokenStream::from(quote! { compile_error!(#data) })
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue