1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::{attr, util};
use proc_macro2::{Ident, TokenStream};
use quote::ToTokens;
use syn::spanned::Spanned;
use syn::{Attribute, Error, FnArg, Result, Type};
/// A command argument, and all its details, skipping the first one, which must be an `SlashContext`
/// reference.
pub struct Argument<'a> {
/// The name of this argument at function definition.
///
/// e.g.: fn a(arg: String), being `arg` this field.
pub name: Ident,
/// The type of this argument.
///
/// e.g.: fn a(arg: String), being `String` this field.
pub ty: Box<Type>,
/// The description of this argument, this is a required field parsed with `#[description]`
/// attribute.
///
/// This macro can be used two ways:
///
/// - List way: #[description("Some description")]
///
/// - Named value way: #[description = "Some description"]
///
/// e.g.: fn a(#[description = "some here"] arg: String), being the fields inside `description`
/// this field
pub description: String,
/// The renaming of this argument, if this option is not specified, the original name will be
/// used to parse the argument and register the command in discord
pub renaming: Option<String>,
trait_type: &'a Type,
}
impl<'a> Argument<'a> {
/// Creates a new [argument](self::Argument) and parses the required fields
pub fn new(arg: FnArg, trait_type: &'a Type) -> Result<Self> {
let pat = util::get_pat(&arg)?;
let name = util::get_ident(&pat.pat)?;
let type_ = pat.ty.clone();
let mut descriptions = pat
.attrs
.iter()
.map(Self::extract_description)
.collect::<Result<Vec<_>>>()?
.into_iter()
.filter(|d| d.is_some())
.map(|d| d.unwrap())
.collect::<Vec<_>>();
let mut names = pat
.attrs
.iter()
.map(Self::extract_name)
.collect::<Result<Vec<_>>>()?
.into_iter()
.filter(|n| n.is_some())
.map(|n| n.unwrap())
.collect::<Vec<_>>();
if descriptions.len() > 1 {
// We only want a single description attribute
return Err(Error::new(
arg.span(),
"Only allowed a single description attribute",
));
} else if descriptions.is_empty() {
// Description attribute is required
return Err(Error::new(arg.span(), "Description attribute is required"));
}
if names.len() > 1 {
// While this attribute is not required, we only accept a single use of it per parameter
return Err(Error::new(
arg.span(),
"Only allowed a single name attribute",
));
}
Ok(Self {
name,
ty: type_,
description: descriptions.remove(0),
renaming: if names.is_empty() {
None
} else {
Some(names.remove(0))
},
trait_type,
})
}
/// Executes the given closure into an [attr](crate::attr::Attr)
fn exec<F, R>(attr: &Attribute, fun: F) -> Result<R>
where
F: FnOnce(attr::Attr) -> Result<R>,
{
fun(attr::parse_attribute(attr)?)
}
/// Extracts the description from the given attribute, returning `None` if this attribute does
/// not correspond to the description one
fn extract_description(attr: &Attribute) -> Result<Option<String>> {
Self::exec(attr, |parsed| {
if parsed.path.is_ident("description") {
Ok(Some(parsed.parse_string()?))
} else {
Ok(None)
}
})
}
/// Extracts the name from a given attribute, returning `None` if this attribute does not
/// correspond to the name one
fn extract_name(attr: &Attribute) -> Result<Option<String>> {
Self::exec(attr, |parsed| {
if parsed.path.is_ident("rename") {
Ok(Some(parsed.parse_string()?))
} else {
Ok(None)
}
})
}
}
impl ToTokens for Argument<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let des = &self.description;
let ty = &self.ty;
let parse_trait = crate::util::get_parse_trait();
let tt = &self.trait_type;
let name = match &self.renaming {
Some(rename) => rename.clone(),
None => self.name.to_string(),
};
tokens.extend(quote::quote! {
.add_argument((
#name,
#des,
<#ty as #parse_trait<#tt>>::is_required(),
<#ty as #parse_trait<#tt>>::option_type(),
<#ty as #parse_trait<#tt>>::add_choices()
).into())
});
}
}