mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +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::{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<Self, syn::Error> {
|
||||
let name = input.parse::<BotCommandAttribute>()?;
|
||||
input.parse::<Token![=]>()?;
|
||||
let value = input.parse::<LitStr>()?.value();
|
||||
|
||||
Ok(Self {
|
||||
name,
|
||||
value
|
||||
})
|
||||
Ok(Self { name, value })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,7 +47,7 @@ impl Attr {
|
|||
}
|
||||
|
||||
pub struct VecAttrs {
|
||||
pub data: Vec<Attr>
|
||||
pub data: Vec<Attr>,
|
||||
}
|
||||
|
||||
impl Parse for VecAttrs {
|
||||
|
@ -62,8 +59,6 @@ impl Parse for VecAttrs {
|
|||
input.parse::<Token![,]>()?;
|
||||
}
|
||||
}
|
||||
Ok(Self {
|
||||
data
|
||||
})
|
||||
Ok(Self { data })
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String>,
|
||||
|
@ -33,7 +35,7 @@ impl Command {
|
|||
struct CommandAttrs {
|
||||
prefix: Option<String>,
|
||||
description: Option<String>,
|
||||
rename: Option<String>
|
||||
rename: Option<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 {
|
||||
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<CommandAttrs, String> {
|
|||
Ok(CommandAttrs {
|
||||
prefix,
|
||||
description,
|
||||
rename: rename_rule
|
||||
rename: rename_rule,
|
||||
})
|
||||
}
|
|
@ -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<String>,
|
||||
description: Option<String>,
|
||||
rename: Option<String>
|
||||
rename: Option<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 {
|
||||
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<CommandAttrs, String> {
|
|||
Ok(CommandAttrs {
|
||||
prefix,
|
||||
description,
|
||||
rename: rename_rule
|
||||
rename: rename_rule,
|
||||
})
|
||||
}
|
|
@ -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::<VecAttrs>() {
|
||||
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<Vec<Attr>, TokenStream> {
|
||||
fn parse_attributes(
|
||||
input: &[syn::Attribute],
|
||||
) -> Result<Vec<Attr>, TokenStream> {
|
||||
let mut enum_attrs = Vec::new();
|
||||
for attr in input.iter() {
|
||||
match attr.parse_args::<VecAttrs>() {
|
||||
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<Vec<Attr>, TokenStream>
|
|||
|
||||
fn compile_error<T>(data: T) -> TokenStream
|
||||
where
|
||||
T: ToTokens
|
||||
T: ToTokens,
|
||||
{
|
||||
TokenStream::from(quote! { compile_error!(#data) })
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue