teloxide/crates/teloxide-macros/src/command_attr.rs

153 lines
5 KiB
Rust
Raw Normal View History

2022-08-28 13:07:19 +02:00
use crate::{
attr::{fold_attrs, Attr},
2022-08-28 13:07:19 +02:00
error::compile_error_at,
fields_parse::ParserType,
rename_rules::RenameRule,
Result,
};
use proc_macro2::Span;
use quote::quote_spanned;
use syn::{parse::Parser, spanned::Spanned, Attribute};
2022-08-28 13:07:19 +02:00
2022-10-02 16:03:10 +02:00
/// All attributes that can be used for `derive(BotCommands)`
2022-08-28 13:07:19 +02:00
pub(crate) struct CommandAttrs {
2022-10-02 16:03:10 +02:00
pub prefix: Option<(String, Span)>,
pub description: Option<(String, Span)>,
pub rename_rule: Option<(RenameRule, Span)>,
2022-10-02 16:13:54 +02:00
pub rename: Option<(String, Span)>,
2022-10-02 16:03:10 +02:00
pub parser: Option<(ParserType, Span)>,
pub separator: Option<(String, Span)>,
pub hide: Option<((), Span)>,
2022-08-28 13:07:19 +02:00
}
2022-10-02 16:03:10 +02:00
/// A single k/v attribute for `BotCommands` derive macro.
///
/// For example:
/// ```text
/// #[command(prefix = "!", rename_rule = "snake_case")]
/// /^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^---- CommandAttr { kind: RenameRule(SnakeCase) }
/// |
/// CommandAttr { kind: Prefix("!") }
/// ```
struct CommandAttr {
2022-08-28 13:07:19 +02:00
kind: CommandAttrKind,
sp: Span,
}
2022-10-02 16:03:10 +02:00
/// Kind of [`CommandAttr`].
enum CommandAttrKind {
2022-08-28 13:07:19 +02:00
Prefix(String),
Description(String),
2022-10-02 14:24:28 +02:00
RenameRule(RenameRule),
2022-10-02 16:13:54 +02:00
Rename(String),
2022-08-28 13:07:19 +02:00
ParseWith(ParserType),
Separator(String),
Hide,
2022-08-28 13:07:19 +02:00
}
impl CommandAttrs {
pub fn from_attributes(attributes: &[Attribute]) -> Result<Self> {
use CommandAttrKind::*;
// Convert the `doc` attribute into `command(description = "...")`
2023-03-02 13:57:43 +01:00
let attributes = attributes.iter().map(|attr| {
if attr.path.is_ident("doc") {
// Extract the token literal from the doc attribute.
2023-03-03 00:07:52 +01:00
let description = parse_doc_comment(attr).unwrap_or_default();
2023-03-02 13:57:43 +01:00
// Convert the doc attribute into a command description attribute.
let sp = attr.span();
let attr = Attribute::parse_outer
.parse2(quote_spanned!(sp => #[command(description = #description)]))
.unwrap();
attr.into_iter().next().unwrap()
} else {
attr.clone()
}
});
2022-08-28 13:07:19 +02:00
fold_attrs(
2023-03-02 13:57:43 +01:00
attributes,
2022-08-28 13:07:19 +02:00
is_command_attribute,
CommandAttr::parse,
Self {
prefix: None,
description: None,
rename_rule: None,
2022-10-02 16:13:54 +02:00
rename: None,
2022-08-28 13:07:19 +02:00
parser: None,
separator: None,
hide: None,
2022-08-28 13:07:19 +02:00
},
|mut this, attr| {
2022-11-07 13:13:29 +01:00
fn insert<T>(opt: &mut Option<(T, Span)>, x: T, sp: Span) -> Result<()> {
2022-08-28 13:07:19 +02:00
match opt {
slot @ None => {
2022-10-02 16:03:10 +02:00
*slot = Some((x, sp));
2022-08-28 13:07:19 +02:00
Ok(())
}
2022-11-07 13:13:29 +01:00
Some(_) => Err(compile_error_at("duplicate attribute", sp)),
2022-08-28 13:07:19 +02:00
}
}
match attr.kind {
Prefix(p) => insert(&mut this.prefix, p, attr.sp),
Description(d) => insert(&mut this.description, d, attr.sp),
2022-10-02 14:24:28 +02:00
RenameRule(r) => insert(&mut this.rename_rule, r, attr.sp),
2022-10-02 16:13:54 +02:00
Rename(r) => insert(&mut this.rename, r, attr.sp),
2022-08-28 13:07:19 +02:00
ParseWith(p) => insert(&mut this.parser, p, attr.sp),
Separator(s) => insert(&mut this.separator, s, attr.sp),
Hide => insert(&mut this.hide, (), attr.sp),
2022-08-28 13:07:19 +02:00
}?;
Ok(this)
},
)
}
}
impl CommandAttr {
fn parse(attr: Attr) -> Result<Self> {
use CommandAttrKind::*;
let sp = attr.span();
let Attr { key, value } = attr;
let kind = match &*key.to_string() {
"prefix" => Prefix(value.expect_string()?),
"description" => Description(value.expect_string()?),
2022-11-07 13:13:29 +01:00
"rename_rule" => {
RenameRule(value.expect_string().and_then(|r| self::RenameRule::parse(&r))?)
}
2022-10-02 16:13:54 +02:00
"rename" => Rename(value.expect_string()?),
"parse_with" => ParseWith(ParserType::parse(value)?),
2022-08-28 13:07:19 +02:00
"separator" => Separator(value.expect_string()?),
"hide" => value.expect_none("hide").map(|_| Hide)?,
2022-08-28 13:07:19 +02:00
_ => {
return Err(compile_error_at(
2022-11-07 13:13:29 +01:00
"unexpected attribute name (expected one of `prefix`, `description`, \
`rename`, `parse_with`, `separator` and `hide`",
2022-08-28 13:07:19 +02:00
key.span(),
))
}
};
Ok(Self { kind, sp })
}
}
fn is_command_attribute(a: &Attribute) -> bool {
match a.path.get_ident() {
Some(ident) => ident == "command",
_ => false,
}
}
2023-03-03 00:07:52 +01:00
fn parse_doc_comment(attr: &Attribute) -> Option<String> {
#[allow(clippy::collapsible_match)]
if let syn::Meta::NameValue(syn::MetaNameValue { lit, .. }) = attr.parse_meta().ok()? {
if let syn::Lit::Str(s) = lit {
return Some(s.value().trim().replace(r"\n", "\n"));
}
}
None
}