2022-08-28 13:07:19 +02:00
|
|
|
use crate::{
|
2023-09-25 13:39:16 +02:00
|
|
|
attr::{fold_attrs, Attr, AttrValue},
|
2022-08-28 13:07:19 +02:00
|
|
|
error::compile_error_at,
|
|
|
|
fields_parse::ParserType,
|
|
|
|
rename_rules::RenameRule,
|
|
|
|
Result,
|
|
|
|
};
|
|
|
|
|
2023-09-12 13:12:11 +02:00
|
|
|
use proc_macro::TokenStream;
|
2023-09-10 09:06:09 +02:00
|
|
|
use proc_macro2::Span;
|
2023-09-12 13:12:11 +02:00
|
|
|
use syn::{
|
|
|
|
parse::{ParseStream, Peek},
|
|
|
|
Attribute, Token,
|
|
|
|
};
|
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)>,
|
2023-09-12 17:20:49 +02:00
|
|
|
/// The bool is true if the description contains a doc comment
|
|
|
|
pub description: Option<(String, bool, Span)>,
|
2022-10-02 16:03:10 +02:00
|
|
|
pub rename_rule: Option<(RenameRule, Span)>,
|
2022-10-02 16:13:54 +02:00
|
|
|
pub rename: Option<(String, Span)>,
|
2023-09-25 13:39:16 +02:00
|
|
|
pub aliases: Option<(Vec<String>, Span)>,
|
2022-10-02 16:03:10 +02:00
|
|
|
pub parser: Option<(ParserType, Span)>,
|
|
|
|
pub separator: Option<(String, Span)>,
|
2023-09-15 13:21:52 +02:00
|
|
|
pub command_separator: Option<(String, Span)>,
|
2023-03-02 18:18:28 +01:00
|
|
|
pub hide: Option<((), Span)>,
|
2023-09-25 15:19:32 +02:00
|
|
|
pub hide_aliases: 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),
|
2023-09-12 17:20:49 +02:00
|
|
|
/// Description of the command. and if its doc comment or not
|
|
|
|
Description(String, bool),
|
2022-10-02 14:24:28 +02:00
|
|
|
RenameRule(RenameRule),
|
2022-10-02 16:13:54 +02:00
|
|
|
Rename(String),
|
2023-09-25 13:39:16 +02:00
|
|
|
Aliases(Vec<String>),
|
2022-08-28 13:07:19 +02:00
|
|
|
ParseWith(ParserType),
|
|
|
|
Separator(String),
|
2023-09-15 13:21:52 +02:00
|
|
|
CommandSeparator(String),
|
2023-03-02 18:18:28 +01:00
|
|
|
Hide,
|
2023-09-25 15:19:32 +02:00
|
|
|
HideAliases,
|
2022-08-28 13:07:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CommandAttrs {
|
|
|
|
pub fn from_attributes(attributes: &[Attribute]) -> Result<Self> {
|
|
|
|
use CommandAttrKind::*;
|
2023-08-28 23:22:16 +02:00
|
|
|
|
2022-08-28 13:07:19 +02:00
|
|
|
fold_attrs(
|
2023-09-20 02:41:44 +02:00
|
|
|
attributes,
|
2023-09-10 09:06:09 +02:00
|
|
|
|attr| is_command_attribute(attr) || is_doc_comment(attr),
|
2022-08-28 13:07:19 +02:00
|
|
|
CommandAttr::parse,
|
|
|
|
Self {
|
|
|
|
prefix: None,
|
|
|
|
description: None,
|
|
|
|
rename_rule: None,
|
2022-10-02 16:13:54 +02:00
|
|
|
rename: None,
|
2023-09-25 13:39:16 +02:00
|
|
|
aliases: None,
|
2022-08-28 13:07:19 +02:00
|
|
|
parser: None,
|
|
|
|
separator: None,
|
2023-09-15 13:21:52 +02:00
|
|
|
command_separator: None,
|
2023-03-02 18:18:28 +01:00
|
|
|
hide: None,
|
2023-09-25 15:19:32 +02:00
|
|
|
hide_aliases: 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 17:20:49 +02:00
|
|
|
fn join_string(opt: &mut Option<(String, bool, Span)>, new_str: &str, sp: Span) {
|
2023-09-10 09:06:09 +02:00
|
|
|
match opt {
|
|
|
|
slot @ None => {
|
2023-09-12 17:20:49 +02:00
|
|
|
*slot = Some((new_str.to_owned(), false, sp));
|
2023-09-10 09:06:09 +02:00
|
|
|
}
|
2023-09-12 17:20:49 +02:00
|
|
|
Some((old_str, ..)) => {
|
2023-09-10 09:06:09 +02:00
|
|
|
*old_str = format!("{old_str}\n{new_str}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-28 13:07:19 +02:00
|
|
|
match attr.kind {
|
|
|
|
Prefix(p) => insert(&mut this.prefix, p, attr.sp),
|
2023-09-12 17:20:49 +02:00
|
|
|
Description(d, is_doc) => {
|
|
|
|
join_string(
|
|
|
|
&mut this.description,
|
|
|
|
// Sometimes doc comments include a space before them, this removes it
|
|
|
|
d.strip_prefix(' ').unwrap_or(&d),
|
|
|
|
attr.sp,
|
|
|
|
);
|
2023-09-20 02:34:22 +02:00
|
|
|
if is_doc {
|
2023-09-12 17:20:49 +02:00
|
|
|
if let Some((_, is_doc, _)) = &mut this.description {
|
|
|
|
*is_doc = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
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),
|
2023-09-25 13:39:16 +02:00
|
|
|
Aliases(a) => insert(&mut this.aliases, a, 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),
|
2023-09-15 13:21:52 +02:00
|
|
|
CommandSeparator(s) => insert(&mut this.command_separator, s, attr.sp),
|
2023-03-02 18:18:28 +01:00
|
|
|
Hide => insert(&mut this.hide, (), attr.sp),
|
2023-09-25 15:19:32 +02:00
|
|
|
HideAliases => insert(&mut this.hide_aliases, (), 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();
|
2023-09-12 13:04:09 +02:00
|
|
|
let Attr { mut key, value } = attr;
|
|
|
|
|
|
|
|
let outermost_key = key.pop().unwrap(); // `Attr`'s invariants ensure `key.len() > 0`
|
|
|
|
|
|
|
|
let kind = match &*outermost_key.to_string() {
|
|
|
|
"doc" => {
|
|
|
|
if let Some(unexpected_key) = key.last() {
|
|
|
|
return Err(compile_error_at(
|
|
|
|
"`doc` can't have nested attributes",
|
|
|
|
unexpected_key.span(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2023-09-12 17:20:49 +02:00
|
|
|
Description(value.expect_string()?, true)
|
2023-09-12 13:04:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
"command" => {
|
2023-09-27 10:29:28 +02:00
|
|
|
let Some(attr) = key.pop() else {
|
|
|
|
return Err(compile_error_at(
|
|
|
|
"expected an attribute name",
|
|
|
|
outermost_key.span(),
|
|
|
|
));
|
|
|
|
};
|
2023-09-12 13:04:09 +02:00
|
|
|
|
|
|
|
if let Some(unexpected_key) = key.last() {
|
|
|
|
return Err(compile_error_at(
|
|
|
|
&format!("{attr} can't have nested attributes"),
|
|
|
|
unexpected_key.span(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
match &*attr.to_string() {
|
|
|
|
"prefix" => Prefix(value.expect_string()?),
|
2023-09-12 17:20:49 +02:00
|
|
|
"description" => Description(value.expect_string()?, false),
|
2023-09-12 13:04:09 +02:00
|
|
|
"rename_rule" => {
|
|
|
|
RenameRule(value.expect_string().and_then(|r| self::RenameRule::parse(&r))?)
|
|
|
|
}
|
|
|
|
"rename" => Rename(value.expect_string()?),
|
|
|
|
"parse_with" => ParseWith(ParserType::parse(value)?),
|
|
|
|
"separator" => Separator(value.expect_string()?),
|
2023-09-15 13:21:52 +02:00
|
|
|
"command_separator" => CommandSeparator(value.expect_string()?),
|
2023-09-12 13:04:09 +02:00
|
|
|
"hide" => value.expect_none("hide").map(|_| Hide)?,
|
2023-09-25 15:19:32 +02:00
|
|
|
"hide_aliases" => value.expect_none("hide_aliases").map(|_| HideAliases)?,
|
2023-09-25 13:39:16 +02:00
|
|
|
"alias" => Aliases(vec![value.expect_string()?]),
|
|
|
|
"aliases" => Aliases(
|
|
|
|
value
|
|
|
|
.expect_array()?
|
|
|
|
.into_iter()
|
|
|
|
.map(AttrValue::expect_string)
|
|
|
|
.collect::<Result<_>>()?,
|
|
|
|
),
|
2023-09-12 13:04:09 +02:00
|
|
|
_ => {
|
|
|
|
return Err(compile_error_at(
|
|
|
|
"unexpected attribute name (expected one of `prefix`, `description`, \
|
2023-09-25 13:39:16 +02:00
|
|
|
`rename`, `parse_with`, `separator`, `hide`, `alias` and `aliases`",
|
2023-09-12 13:04:09 +02:00
|
|
|
attr.span(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 13:13:29 +01:00
|
|
|
}
|
2023-09-12 13:04:09 +02:00
|
|
|
|
2022-08-28 13:07:19 +02:00
|
|
|
_ => {
|
|
|
|
return Err(compile_error_at(
|
2023-09-12 13:04:09 +02:00
|
|
|
"unexpected attribute (expected `command` or `doc`)",
|
|
|
|
outermost_key.span(),
|
2022-08-28 13:07:19 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Self { kind, sp })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_command_attribute(a: &Attribute) -> bool {
|
2023-09-12 13:12:11 +02:00
|
|
|
matches!(a.path.get_ident(), Some(ident) if ident == "command")
|
2022-08-28 13:07:19 +02:00
|
|
|
}
|
2023-03-03 00:07:52 +01:00
|
|
|
|
2023-09-12 13:12:11 +02:00
|
|
|
fn is_doc_comment(a: &Attribute) -> bool {
|
|
|
|
matches!(a.path.get_ident(), Some(ident) if ident == "doc" && peek_at_token_stream(a.tokens.clone().into(), Token![=]))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn peek_at_token_stream(s: TokenStream, p: impl Peek) -> bool {
|
|
|
|
// syn be fr challenge 2023 (impossible)
|
|
|
|
use syn::parse::Parser;
|
|
|
|
(|input: ParseStream<'_>| {
|
|
|
|
let r = input.peek(p);
|
|
|
|
_ = input.step(|_| Ok(((), syn::buffer::Cursor::empty())));
|
|
|
|
Ok(r)
|
|
|
|
})
|
|
|
|
.parse(s)
|
|
|
|
.unwrap()
|
2023-08-28 23:22:16 +02:00
|
|
|
}
|