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
use std::iter::{self, Once};

use proc_macro::{Delimiter, Group, Punct, Spacing, TokenStream, TokenTree};
use proc_macro_error::{abort, abort_call_site, proc_macro_error};

#[proc_macro]
#[proc_macro_error]
pub fn get(path: TokenStream) -> TokenStream {
    let method: TokenStream = "::solarsail::http::Method::GET".parse().unwrap();
    method_macro(method, path)
}

#[proc_macro]
#[proc_macro_error]
pub fn head(path: TokenStream) -> TokenStream {
    let method: TokenStream = "::solarsail::http::Method::HEAD".parse().unwrap();
    method_macro(method, path)
}

#[proc_macro]
#[proc_macro_error]
pub fn post(path: TokenStream) -> TokenStream {
    let method: TokenStream = "::solarsail::http::Method::POST".parse().unwrap();
    method_macro(method, path)
}

#[proc_macro]
#[proc_macro_error]
pub fn put(path: TokenStream) -> TokenStream {
    let method: TokenStream = "::solarsail::http::Method::PUT".parse().unwrap();
    method_macro(method, path)
}

#[proc_macro]
#[proc_macro_error]
pub fn delete(path: TokenStream) -> TokenStream {
    let method: TokenStream = "::solarsail::http::Method::DELETE".parse().unwrap();
    method_macro(method, path)
}

#[proc_macro]
#[proc_macro_error]
pub fn patch(path: TokenStream) -> TokenStream {
    let method: TokenStream = "::solarsail::http::Method::PATCH".parse().unwrap();
    method_macro(method, path)
}

#[proc_macro]
#[proc_macro_error]
pub fn route(path: TokenStream) -> TokenStream {
    let mut path = path.into_iter();
    let method = (&mut path)
        .take_while(|token| {
            match token {
                TokenTree::Punct(p) => {
                    match p.as_char() {
                        ',' => false,
                        '/' => abort_call_site!(
                            "route! macro must contain a pattern matching expected methods as its first argument"
                        ),
                        _ => true
                    }
                }
                _ => true
            }
        })
        .collect();
    method_macro(method, path.collect())
}

fn method_macro(method: TokenStream, path: TokenStream) -> TokenStream {
    let mut expect_slash = false;
    let path: TokenStream = punct_once('&')
        .chain(iter::once(TokenTree::Group(Group::new(
            Delimiter::Bracket,
            path.into_iter()
                .enumerate()
                .filter_map(|(i, item)| {
                    Some(match item {
                        TokenTree::Group(g) => abort!(g.span(), "unexpected group"),
                        item @ TokenTree::Ident(_) => {
                            expect_slash = true;
                            item
                        }
                        TokenTree::Punct(p) if p.as_char() == '/' => {
                            if i == 0 {
                                return None;
                            }

                            if !expect_slash {
                                abort!(p.span(), "unexpected forward slash")
                            }

                            expect_slash = false;

                            TokenTree::Punct(Punct::new(',', Spacing::Alone))
                        }
                        TokenTree::Punct(p) => {
                            if p.spacing() == Spacing::Alone {
                                expect_slash = true;
                            }
                            TokenTree::Punct(p)
                        }
                        literal @ TokenTree::Literal(_) => {
                            expect_slash = true;
                            literal
                        }
                    })
                })
                .collect(),
        ))))
        .collect();

    let result: TokenStream = TokenTree::Group(Group::new(
        Delimiter::Parenthesis,
        method
            .into_iter()
            .chain(punct_once(','))
            .chain(path)
            .collect(),
    ))
    .into();

    result
}

fn punct_once(ch: char) -> Once<TokenTree> {
    iter::once(TokenTree::Punct(Punct::new(ch, Spacing::Alone)))
}