aboutsummaryrefslogtreecommitdiff
path: root/rossweisse/src/lib.rs
blob: 2c38fe270a12ed21592c2957b50d75eaab3cae9c (plain) (blame)
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
#![deny(
  clippy::all,
  clippy::nursery,
  clippy::pedantic,
  future_incompatible,
  nonstandard_style,
  rust_2018_idioms,
  unsafe_code,
  unused,
  warnings
)]
#![recursion_limit = "128"]

mod implementations;

use proc_macro::TokenStream;
use syn::Item;

/// Marks a `struct` as a router or marks an `impl` block as a router
/// implementation
///
/// # Panics
///
/// Panics if used on an item that is not a `struct` or `impl` block.
///
/// # Examples
///
/// ```rust
/// use rossweisse::route;
/// use windmark::response::Response;
///
/// #[rossweisse::router]
/// struct Router {
///   _phantom: (),
/// }
///
/// #[rossweisse::router]
/// impl Router {
///   #[route]
///   pub fn index(_context: windmark::context::RouteContext) -> Response {
///     Response::success("Hello, World!")
///   }
/// }
/// ```
#[proc_macro_attribute]
pub fn router(arguments: TokenStream, item: TokenStream) -> TokenStream {
  match syn::parse::<Item>(item) {
    Ok(Item::Struct(item)) => implementations::fields(arguments, item),
    Ok(Item::Impl(item)) => implementations::methods(arguments, item),
    _ => panic!("`#[rossweisse::router]` can only be used on `struct`s"),
  }
}

/// Marks a method of a router implementation as a route to mount
///
/// # Panics
///
/// Panics if used on an item that is not a function.
///
/// # Examples
///
/// ```rust
/// use rossweisse::route;
/// use windmark::response::Response;
///
/// #[rossweisse::router]
/// impl Router {
///   #[route]
///   pub fn index(_context: windmark::context::RouteContext) -> Response {
///     Response::success("Hello, World!")
///   }
/// }
/// ```
#[proc_macro_attribute]
pub fn route(arguments: TokenStream, item: TokenStream) -> TokenStream {
  match syn::parse::<Item>(item) {
    Ok(Item::Fn(ref item)) => implementations::route(arguments, item),
    _ => panic!("`#[rossweisse::route]` can only be used on `fn`s"),
  }
}