ripsy_macros/
lib.rs

1mod endpoint;
2mod macros;
3mod ripsy;
4
5use proc_macro2::Ident;
6use quote::ToTokens;
7use syn::parse::{Parse, ParseStream};
8
9#[derive(Debug, Clone, Copy, Eq, PartialEq)]
10enum Method {
11    Mutation,
12    Query,
13}
14
15impl Parse for Method {
16    fn parse(input: ParseStream) -> syn::Result<Self> {
17        let ident: Ident = input.parse().map_err(|e| {
18            syn::Error::new(
19                e.span(),
20                "unexpected end of input, expected `mutation` or `query`",
21            )
22        })?;
23
24        match &*ident.to_string() {
25            "mutation" => Ok(Method::Mutation),
26            "query" => Ok(Method::Query),
27            actual => Err(syn::Error::new(
28                ident.span(),
29                format!("expected `mutation` or `query`, found {}", actual),
30            )),
31        }
32    }
33}
34
35#[proc_macro]
36pub fn ripsy(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
37    let item = syn::parse_macro_input!(input as ripsy::Ripsy);
38    item.into_token_stream().into()
39}
40
41#[proc_macro_attribute]
42pub fn endpoint(
43    attr: proc_macro::TokenStream,
44    item: proc_macro::TokenStream,
45) -> proc_macro::TokenStream {
46    let attr = syn::parse_macro_input!(attr as Method);
47    let item = syn::parse_macro_input!(item as endpoint::Endpoint);
48
49    item.to_tokens(attr).into()
50}