2021-01-26 01:32:43 +00:00
|
|
|
#![deny(rust_2018_idioms)]
|
2021-06-10 20:16:16 +00:00
|
|
|
#![deny(broken_intra_doc_links)]
|
2021-01-26 01:32:43 +00:00
|
|
|
|
|
|
|
use proc_macro::TokenStream;
|
2021-06-11 09:10:48 +00:00
|
|
|
use proc_macro2::Ident;
|
2021-01-26 01:32:43 +00:00
|
|
|
use quote::quote;
|
|
|
|
use syn::{parse::Error, parse_macro_input, parse_quote, spanned::Spanned, Lit};
|
|
|
|
|
|
|
|
pub(crate) mod attributes;
|
|
|
|
pub(crate) mod consts;
|
|
|
|
pub(crate) mod structures;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
pub(crate) mod util;
|
|
|
|
|
|
|
|
use attributes::*;
|
|
|
|
use consts::*;
|
|
|
|
use structures::*;
|
|
|
|
use util::*;
|
|
|
|
|
|
|
|
macro_rules! match_options {
|
|
|
|
($v:expr, $values:ident, $options:ident, $span:expr => [$($name:ident);*]) => {
|
|
|
|
match $v {
|
|
|
|
$(
|
|
|
|
stringify!($name) => $options.$name = propagate_err!($crate::attributes::parse($values)),
|
|
|
|
)*
|
|
|
|
_ => {
|
|
|
|
return Error::new($span, format_args!("invalid attribute: {:?}", $v))
|
|
|
|
.to_compile_error()
|
|
|
|
.into();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn command(attr: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
let mut fun = parse_macro_input!(input as CommandFun);
|
|
|
|
|
2021-06-10 20:16:16 +00:00
|
|
|
let _name = if !attr.is_empty() {
|
2021-01-26 01:32:43 +00:00
|
|
|
parse_macro_input!(attr as Lit).to_str()
|
|
|
|
} else {
|
|
|
|
fun.name.to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut options = Options::new();
|
|
|
|
|
|
|
|
for attribute in &fun.attributes {
|
|
|
|
let span = attribute.span();
|
|
|
|
let values = propagate_err!(parse_values(attribute));
|
|
|
|
|
|
|
|
let name = values.name.to_string();
|
|
|
|
let name = &name[..];
|
|
|
|
|
2021-06-10 20:16:16 +00:00
|
|
|
match name {
|
2021-06-11 09:10:48 +00:00
|
|
|
"arg" => options
|
|
|
|
.cmd_args
|
|
|
|
.push(propagate_err!(attributes::parse(values))),
|
2021-06-10 20:16:16 +00:00
|
|
|
"example" => {
|
|
|
|
options
|
|
|
|
.examples
|
|
|
|
.push(propagate_err!(attributes::parse(values)));
|
|
|
|
}
|
|
|
|
"description" => {
|
|
|
|
let line: String = propagate_err!(attributes::parse(values));
|
|
|
|
util::append_line(&mut options.description, line);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
match_options!(name, values, options, span => [
|
|
|
|
aliases;
|
|
|
|
usage;
|
|
|
|
required_permissions;
|
|
|
|
allow_slash
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 01:32:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 15:54:31 +00:00
|
|
|
let Options {
|
2021-06-10 20:16:16 +00:00
|
|
|
aliases,
|
|
|
|
description,
|
|
|
|
usage,
|
|
|
|
examples,
|
|
|
|
required_permissions,
|
2021-06-09 15:54:31 +00:00
|
|
|
allow_slash,
|
2021-06-11 09:10:48 +00:00
|
|
|
mut cmd_args,
|
2021-06-09 15:54:31 +00:00
|
|
|
} = options;
|
2021-01-26 01:32:43 +00:00
|
|
|
|
2021-06-10 20:16:16 +00:00
|
|
|
propagate_err!(create_declaration_validations(&mut fun));
|
2021-01-26 01:32:43 +00:00
|
|
|
|
|
|
|
let res = parse_quote!(serenity::framework::standard::CommandResult);
|
|
|
|
create_return_type_validation(&mut fun, res);
|
|
|
|
|
|
|
|
let visibility = fun.visibility;
|
|
|
|
let name = fun.name.clone();
|
|
|
|
let body = fun.body;
|
|
|
|
let ret = fun.ret;
|
|
|
|
|
|
|
|
let n = name.with_suffix(COMMAND);
|
|
|
|
|
|
|
|
let cooked = fun.cooked.clone();
|
|
|
|
|
|
|
|
let command_path = quote!(crate::framework::Command);
|
2021-06-11 09:10:48 +00:00
|
|
|
let arg_path = quote!(crate::framework::Arg);
|
2021-01-26 01:32:43 +00:00
|
|
|
|
|
|
|
populate_fut_lifetimes_on_refs(&mut fun.args);
|
|
|
|
let args = fun.args;
|
|
|
|
|
2021-06-11 09:10:48 +00:00
|
|
|
let arg_idents = cmd_args
|
|
|
|
.iter()
|
|
|
|
.map(|arg| n.with_suffix(arg.name.as_str()).with_suffix(ARG))
|
|
|
|
.collect::<Vec<Ident>>();
|
|
|
|
|
|
|
|
let mut tokens = cmd_args
|
|
|
|
.iter_mut()
|
|
|
|
.map(|arg| {
|
|
|
|
let Arg {
|
|
|
|
name,
|
|
|
|
description,
|
|
|
|
kind,
|
|
|
|
required,
|
|
|
|
} = arg;
|
|
|
|
|
|
|
|
let an = n.with_suffix(name.as_str()).with_suffix(ARG);
|
|
|
|
|
|
|
|
quote! {
|
|
|
|
#(#cooked)*
|
|
|
|
#[allow(missing_docs)]
|
|
|
|
pub static #an: #arg_path = #arg_path {
|
|
|
|
name: #name,
|
|
|
|
description: #description,
|
|
|
|
kind: #kind,
|
2021-06-11 09:39:32 +00:00
|
|
|
required: #required,
|
2021-06-11 09:10:48 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.fold(quote! {}, |mut a, b| {
|
|
|
|
a.extend(b);
|
|
|
|
a
|
|
|
|
});
|
|
|
|
|
|
|
|
tokens.extend(quote! {
|
2021-01-26 01:32:43 +00:00
|
|
|
#(#cooked)*
|
2021-06-10 20:16:16 +00:00
|
|
|
#[allow(missing_docs)]
|
2021-01-26 01:32:43 +00:00
|
|
|
pub static #n: #command_path = #command_path {
|
2021-06-10 20:16:16 +00:00
|
|
|
fun: #name,
|
|
|
|
names: &[#_name, #(#aliases),*],
|
|
|
|
desc: #description,
|
|
|
|
usage: #usage,
|
|
|
|
examples: &[#(#examples),*],
|
|
|
|
required_permissions: #required_permissions,
|
2021-06-09 15:54:31 +00:00
|
|
|
allow_slash: #allow_slash,
|
2021-06-11 09:10:48 +00:00
|
|
|
args: &[#(&#arg_idents),*],
|
2021-01-26 01:32:43 +00:00
|
|
|
};
|
|
|
|
|
2021-06-10 20:16:16 +00:00
|
|
|
#(#cooked)*
|
|
|
|
#[allow(missing_docs)]
|
2021-01-26 01:32:43 +00:00
|
|
|
#visibility fn #name<'fut> (#(#args),*) -> ::serenity::futures::future::BoxFuture<'fut, #ret> {
|
|
|
|
use ::serenity::futures::future::FutureExt;
|
|
|
|
|
2021-06-10 20:16:16 +00:00
|
|
|
async move {
|
|
|
|
let _output: #ret = { #(#body)* };
|
|
|
|
#[allow(unreachable_code)]
|
|
|
|
_output
|
|
|
|
}.boxed()
|
2021-01-26 01:32:43 +00:00
|
|
|
}
|
2021-06-11 09:10:48 +00:00
|
|
|
});
|
2021-06-10 20:16:16 +00:00
|
|
|
|
2021-06-11 09:10:48 +00:00
|
|
|
tokens.into()
|
2021-06-10 20:16:16 +00:00
|
|
|
}
|