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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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>,
pub autocomplete: Option<Ident>,
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<_>>();
let mut autocompletes = pat
.attrs
.iter()
.map(Self::extract_autocomplete)
.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",
));
}
if autocompletes.len() > 1 {
return Err(Error::new(
arg.span(),
"Only allowed a single autocomplete attribute",
));
}
Ok(Self {
name,
ty: type_,
description: descriptions.remove(0),
renaming: if names.is_empty() {
None
} else {
Some(names.remove(0))
},
autocomplete: if autocompletes.is_empty() {
None
} else {
Some(autocompletes.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)
}
})
}
fn extract_autocomplete(attr: &Attribute) -> Result<Option<Ident>> {
Self::exec(attr, |parsed| {
if parsed.path.is_ident("autocomplete") {
if let Ok(s) = parsed.parse_string() {
return Ok(Some(Ident::new(&s, parsed.span())));
}
Ok(Some(parsed.parse_identifier()?))
} 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(),
};
if let Some(autocomplete) = &self.autocomplete {
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(),
Some(#autocomplete())
).into())
});
} else {
let autocomplete_hook = quote::quote!(::zephyrus::hook::AutocompleteHook);
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(),
Option::<#autocomplete_hook<#tt>>::None
).into())
});
}
}
}