2022-08-28 13:07:19 +02:00
|
|
|
use crate::{
|
2023-08-29 00:15:56 +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,
|
|
|
|
};
|
|
|
|
|
|
|
|
use proc_macro2::Span;
|
|
|
|
use syn::Attribute;
|
|
|
|
|
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)>,
|
2023-03-02 18:18:28 +01:00
|
|
|
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),
|
2023-03-02 18:18:28 +01:00
|
|
|
Hide,
|
2022-08-28 13:07:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CommandAttrs {
|
|
|
|
pub fn from_attributes(attributes: &[Attribute]) -> Result<Self> {
|
|
|
|
use CommandAttrKind::*;
|
|
|
|
|
|
|
|
fold_attrs(
|
|
|
|
attributes,
|
|
|
|
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,
|
2023-03-02 18:18:28 +01:00
|
|
|
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),
|
2023-03-02 18:18:28 +01:00
|
|
|
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()?),
|
2022-10-06 12:18:29 +02:00
|
|
|
"parse_with" => ParseWith(ParserType::parse(value)?),
|
2022-08-28 13:07:19 +02:00
|
|
|
"separator" => Separator(value.expect_string()?),
|
2023-08-29 00:15:56 +02:00
|
|
|
"hide" => {
|
|
|
|
if let AttrValue::None(_) = value {
|
|
|
|
Hide
|
|
|
|
} else {
|
|
|
|
return Err(compile_error_at(
|
|
|
|
"The hide attribute should not have a value, remove it ",
|
|
|
|
value.span(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
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`, \
|
2023-03-02 18:18:28 +01:00
|
|
|
`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,
|
|
|
|
}
|
|
|
|
}
|