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
#![feature(proc_macro_diagnostic)]

#[macro_use]
extern crate syn;

use proc_macro_hack::proc_macro_hack;
use proc_macro::TokenStream;

macro_rules! compile_error {
    (err $tokens:expr, $msg:literal) => {
        return Err(::syn::Error::new_spanned($tokens, $msg)
                .to_compile_error()
                .into());
    };
    (err $msg:literal) => {
        return Err(::syn::Error::new(::proc_macro2::Span::call_site(), $msg)
                .to_compile_error()
                .into());
    };
    ($tokens:expr, $msg:literal) => {
        return ::syn::Error::new_spanned($tokens, $msg)
                .to_compile_error()
                .into();
    };
    ($msg:literal) => {
        return ::syn::Error::new(::proc_macro2::Span::call_site(), $msg)
                .to_compile_error()
                .into();
    };
}

macro_rules! unwrap {
    ($input:expr) => {
        match $input {
            Ok(v) => v,
            Err(ts) => return ts
        }
    }
}

mod attribute;
mod derive;
mod macros;
mod utils;

//doc in automae's lib.rs
#[proc_macro_derive(State)]
pub fn state(input: TokenStream) -> TokenStream {
    derive::state(input)
}

#[proc_macro_attribute]
pub fn object(metadata: TokenStream, item: TokenStream) -> TokenStream {
    attribute::object(metadata, item)
}

#[proc_macro_attribute]
pub fn payload(metadata: TokenStream, item: TokenStream) -> TokenStream {
    attribute::payload(metadata, item)
}

#[proc_macro_attribute]
pub fn convert(metadata: TokenStream, item: TokenStream) -> TokenStream {
    attribute::convert(metadata, item)
}

#[proc_macro_attribute]
pub fn stringify(metadata: TokenStream, item: TokenStream) -> TokenStream {
    attribute::stringify(metadata, item)
}

#[proc_macro_attribute]
pub fn endpoint(metadata: TokenStream, item: TokenStream) -> TokenStream {
    attribute::endpoint(metadata, item)
}

/// An event listener function.
/// The function takes two arguments, the first being the
/// session which contains data about the bot and methods
/// to send instructions to discord. The second argument
/// is the event dispatch which contains data about the
/// event.
/// The library will call this function each time it
/// receives an event of the type of the second argument.
///
/// # Example
/// ```ignore
/// use automate::{Session, Error, listener};
/// use automate::gateway::MessageCreateDispatch;
///
/// #[listener]
/// async fn hello(_: &mut Context, _: &MessageCreateDispatch) -> Result<(), Error> {
///     println!("Hello!");
///     Ok(())
/// }
/// ```
#[proc_macro_attribute]
pub fn listener(metadata: TokenStream, item: TokenStream) -> TokenStream {
    attribute::listener(metadata, item)
}

//doc in automate's lib.rs
#[proc_macro_hack]
pub fn functions(input: TokenStream) -> TokenStream {
    macros::functions(input)
}

//doc in automate's lib.rs
#[proc_macro_hack]
pub fn stateless(input: TokenStream) -> TokenStream {
    macros::functions(input)
}

//doc in automate's lib.rs
#[proc_macro_hack]
pub fn methods(input: TokenStream) -> TokenStream {
    macros::methods(input)
}