ruma_macros/api/
attribute.rs
1use syn::{
4 parse::{Parse, ParseStream},
5 Ident, Token, Type,
6};
7
8mod kw {
9 syn::custom_keyword!(body);
10 syn::custom_keyword!(raw_body);
11 syn::custom_keyword!(path);
12 syn::custom_keyword!(query);
13 syn::custom_keyword!(query_all);
14 syn::custom_keyword!(header);
15 syn::custom_keyword!(error);
16 syn::custom_keyword!(manual_body_serde);
17 syn::custom_keyword!(status);
18}
19
20pub enum RequestMeta {
21 NewtypeBody,
22 RawBody,
23 Path,
24 Query,
25 QueryAll,
26 Header(Ident),
27}
28
29impl Parse for RequestMeta {
30 fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
31 let lookahead = input.lookahead1();
32 if lookahead.peek(kw::body) {
33 let _: kw::body = input.parse()?;
34 Ok(Self::NewtypeBody)
35 } else if lookahead.peek(kw::raw_body) {
36 let _: kw::raw_body = input.parse()?;
37 Ok(Self::RawBody)
38 } else if lookahead.peek(kw::path) {
39 let _: kw::path = input.parse()?;
40 Ok(Self::Path)
41 } else if lookahead.peek(kw::query) {
42 let _: kw::query = input.parse()?;
43 Ok(Self::Query)
44 } else if lookahead.peek(kw::query_all) {
45 let _: kw::query_all = input.parse()?;
46 Ok(Self::QueryAll)
47 } else if lookahead.peek(kw::header) {
48 let _: kw::header = input.parse()?;
49 let _: Token![=] = input.parse()?;
50 input.parse().map(Self::Header)
51 } else {
52 Err(lookahead.error())
53 }
54 }
55}
56
57pub enum DeriveRequestMeta {
58 Error(Type),
59}
60
61impl Parse for DeriveRequestMeta {
62 fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
63 let lookahead = input.lookahead1();
64 if lookahead.peek(kw::error) {
65 let _: kw::error = input.parse()?;
66 let _: Token![=] = input.parse()?;
67 input.parse().map(Self::Error)
68 } else {
69 Err(lookahead.error())
70 }
71 }
72}
73
74pub enum ResponseMeta {
75 NewtypeBody,
76 RawBody,
77 Header(Ident),
78}
79
80impl Parse for ResponseMeta {
81 fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
82 let lookahead = input.lookahead1();
83 if lookahead.peek(kw::body) {
84 let _: kw::body = input.parse()?;
85 Ok(Self::NewtypeBody)
86 } else if lookahead.peek(kw::raw_body) {
87 let _: kw::raw_body = input.parse()?;
88 Ok(Self::RawBody)
89 } else if lookahead.peek(kw::header) {
90 let _: kw::header = input.parse()?;
91 let _: Token![=] = input.parse()?;
92 input.parse().map(Self::Header)
93 } else {
94 Err(lookahead.error())
95 }
96 }
97}
98
99#[allow(clippy::large_enum_variant)]
100pub enum DeriveResponseMeta {
101 ManualBodySerde,
102 Error(Type),
103 Status(Ident),
104}
105
106impl Parse for DeriveResponseMeta {
107 fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
108 let lookahead = input.lookahead1();
109 if lookahead.peek(kw::manual_body_serde) {
110 let _: kw::manual_body_serde = input.parse()?;
111 Ok(Self::ManualBodySerde)
112 } else if lookahead.peek(kw::error) {
113 let _: kw::error = input.parse()?;
114 let _: Token![=] = input.parse()?;
115 input.parse().map(Self::Error)
116 } else if lookahead.peek(kw::status) {
117 let _: kw::status = input.parse()?;
118 let _: Token![=] = input.parse()?;
119 input.parse().map(Self::Status)
120 } else {
121 Err(lookahead.error())
122 }
123 }
124}