automate_derive/
lib.rs

1#![feature(proc_macro_diagnostic)]
2
3#[macro_use]
4extern crate syn;
5
6use proc_macro::TokenStream;
7
8macro_rules! compile_error {
9    (err $tokens:expr, $msg:literal) => {
10        return Err(::syn::Error::new_spanned($tokens, $msg)
11                .to_compile_error()
12                .into());
13    };
14    (err $msg:literal) => {
15        return Err(::syn::Error::new(::proc_macro2::Span::call_site(), $msg)
16                .to_compile_error()
17                .into());
18    };
19    ($tokens:expr, $msg:literal) => {
20        return ::syn::Error::new_spanned($tokens, $msg)
21                .to_compile_error()
22                .into();
23    };
24    ($msg:literal) => {
25        return ::syn::Error::new(::proc_macro2::Span::call_site(), $msg)
26                .to_compile_error()
27                .into();
28    };
29}
30
31macro_rules! unwrap {
32    ($input:expr) => {
33        match $input {
34            Ok(v) => v,
35            Err(ts) => return ts
36        }
37    }
38}
39
40mod attribute;
41mod derive;
42mod macros;
43mod utils;
44
45//doc in automate's lib.rs
46#[proc_macro_derive(State)]
47pub fn state(input: TokenStream) -> TokenStream {
48    derive::state(input)
49}
50
51//doc in automate's lib.rs
52#[cfg(feature = "storage")]
53#[proc_macro_derive(Stored, attributes(storage))]
54pub fn stored(input: TokenStream) -> TokenStream {
55    derive::stored(input)
56}
57
58//doc in automate's lib.rs
59#[cfg(feature = "storage")]
60#[proc_macro_derive(Storage)]
61pub fn storage(input: TokenStream) -> TokenStream {
62    derive::storage(input)
63}
64
65#[proc_macro_attribute]
66pub fn object(metadata: TokenStream, item: TokenStream) -> TokenStream {
67    attribute::object(metadata, item)
68}
69
70#[proc_macro_attribute]
71pub fn payload(metadata: TokenStream, item: TokenStream) -> TokenStream {
72    attribute::payload(metadata, item)
73}
74
75#[proc_macro_attribute]
76pub fn convert(metadata: TokenStream, item: TokenStream) -> TokenStream {
77    attribute::convert(metadata, item)
78}
79
80#[proc_macro_attribute]
81pub fn stringify(metadata: TokenStream, item: TokenStream) -> TokenStream {
82    attribute::stringify(metadata, item)
83}
84
85#[proc_macro_attribute]
86pub fn endpoint(metadata: TokenStream, item: TokenStream) -> TokenStream {
87    attribute::endpoint(metadata, item)
88}
89
90/// An event listener function.
91/// The function takes two arguments, the first being the
92/// session which contains data about the bot and methods
93/// to send instructions to discord. The second argument
94/// is the event dispatch which contains data about the
95/// event.
96/// The library will call this function each time it
97/// receives an event of the type of the second argument.
98///
99/// # Example
100/// ```ignore
101/// use automate::{Session, Error, listener};
102/// use automate::gateway::MessageCreateDispatch;
103///
104/// #[listener]
105/// async fn hello(_: &Context, _: &MessageCreateDispatch) -> Result<(), Error> {
106///     println!("Hello!");
107///     Ok(())
108/// }
109/// ```
110#[proc_macro_attribute]
111pub fn listener(metadata: TokenStream, item: TokenStream) -> TokenStream {
112    attribute::listener(metadata, item)
113}
114
115//doc in automate's lib.rs
116#[proc_macro]
117pub fn functions(input: TokenStream) -> TokenStream {
118    macros::functions(input)
119}
120
121//doc in automate's lib.rs
122#[proc_macro]
123pub fn stateless(input: TokenStream) -> TokenStream {
124    macros::functions(input)
125}
126
127//doc in automate's lib.rs
128#[proc_macro]
129pub fn methods(input: TokenStream) -> TokenStream {
130    macros::methods(input)
131}