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
#![recursion_limit = "128"]
#![feature(proc_macro)]

//! This crate provides macros for making ReQL types more pleasant to work with.
//! Currently, it only exposes the `args` macro but it's possible that more will
//! be added in the future.
//!
//! In this crate we make use of a couple nightly features, specifically
//! `proc_macro` and `proc_macro_non_items` so you will need to use the latest nightly
//! compiler in order to use the macros here. Add the following to your crate:-
//!
//! ```rust,ignore
//! #![feature(proc_macro)]
//! #![feature(proc_macro_non_items)]
//! ```

#[macro_use]
extern crate quote;
#[macro_use]
extern crate syn;
extern crate proc_macro;
extern crate proc_macro2;

mod args;
mod parser;

use proc_macro::TokenStream;
use syn::punctuated::Punctuated;

#[derive(Debug, Clone)]
struct KvPair(syn::Ident, syn::Expr);

#[derive(Debug, Clone)]
struct Opt(Punctuated<KvPair, Token![,]>);

#[derive(Debug, Clone)]
struct List(Punctuated<syn::Expr, Token![,]>);

#[derive(Debug, Clone)]
struct Args(Punctuated<Arg, Token![,]>);

#[derive(Debug, Clone)]
struct ToComma(proc_macro2::TokenStream);

#[derive(Debug, Clone)]
enum Arg {
    Expr(syn::Expr),
    Opt(Opt),
    Bad(ToComma),
}

/// Splice an array of arguments into another term
///
/// A macro that’s used to splice a number of arguments into another term. This is
/// useful when you want to call a variadic term such as `branch` with a set of arguments produced at
/// runtime.
///
/// # Example
///
/// If `x` is greater than `5` return `big`, otherwise return `small`.
///
/// ```rust,ignore
/// r.branch(args!(r.expr(x).gt(5), "big", "small"));
/// ```
#[proc_macro]
pub fn args(input: TokenStream) -> TokenStream {
    let body = if input.is_empty() {
        quote! { Term::new() }
    } else {
        args::process(input)
    };

    let expanded = quote!({
        #[allow(unused_imports)]
        use reql::{Term, IntoArg, Arg};
        #body
    });

    expanded.into()
}