-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute.rs
More file actions
190 lines (155 loc) · 4.88 KB
/
attribute.rs
File metadata and controls
190 lines (155 loc) · 4.88 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Attribute, Error, Expr, Result, Type, spanned::Spanned};
use crate::status::Status;
pub struct HttpErrorAttribute {
pub status: Option<Status>,
}
impl<'a> HttpErrorAttribute {
pub fn parse_slice(input: &'a [Attribute]) -> Result<Option<Self>> {
let mut result = None;
for attribute in input {
if !attribute.meta.path().is_ident("http") {
continue;
}
if result.is_some() {
return Err(Error::new(
attribute.span(),
"only a single `http` attribute is allowed",
));
}
result = Some(Self::parse(attribute)?);
}
Ok(result)
}
pub fn parse(attribute: &'a Attribute) -> Result<Self> {
let mut status = None;
attribute.parse_nested_meta(|meta| {
if meta.path.is_ident("status") {
status = Some(meta.value()?.parse()?);
Ok(())
} else {
Err(meta.error("unknown parameter"))
}
})?;
Ok(Self { status })
}
pub fn status(&self) -> TokenStream {
status(self.status.as_ref())
}
pub fn responses(&self, r#type: Option<TokenStream>) -> TokenStream {
responses(self.status.as_ref(), r#type)
}
}
pub struct HttpErrorDataAttribute {
pub status: Option<Status>,
pub base: Option<Type>,
pub hook: Option<Expr>,
pub axum: bool,
pub utoipa: bool,
}
impl<'a> HttpErrorDataAttribute {
pub fn parse_slice(input: &'a [Attribute]) -> Result<Option<Self>> {
let mut result = None;
for attribute in input {
if !attribute.meta.path().is_ident("http") {
continue;
}
if result.is_some() {
return Err(Error::new(
attribute.span(),
"only a single `http` attribute is allowed",
));
}
result = Some(Self::parse(attribute)?);
}
Ok(result)
}
pub fn parse(attribute: &'a Attribute) -> Result<Self> {
let mut status = None;
let mut base = None;
let mut hook = None;
let mut axum = false;
let mut utoipa = false;
attribute.parse_nested_meta(|meta| {
if meta.path.is_ident("status") {
status = Some(meta.value()?.parse()?);
Ok(())
} else if meta.path.is_ident("base") {
base = Some(meta.value()?.parse()?);
Ok(())
} else if meta.path.is_ident("hook") {
hook = Some(meta.value()?.parse()?);
Ok(())
} else if meta.path.is_ident("axum") {
axum = true;
Ok(())
} else if meta.path.is_ident("utoipa") {
utoipa = true;
Ok(())
} else {
Err(meta.error("unknown parameter"))
}
})?;
Ok(Self {
status,
base,
hook,
axum,
utoipa,
})
}
pub fn status(&self) -> TokenStream {
status(self.status.as_ref())
}
pub fn responses(&self, r#type: Option<TokenStream>) -> TokenStream {
responses(self.status.as_ref(), r#type)
}
pub fn hook(&self) -> TokenStream {
if let Some(hook) = &self.hook {
quote! {
#hook(&self);
}
} else {
TokenStream::new()
}
}
}
fn status(status: Option<&Status>) -> TokenStream {
if let Some(status) = status {
let status = status.as_ident();
quote!(::breach::http::StatusCode::#status)
} else {
quote!(compile_error!("missing `#[http(status = ..)]` attribute"))
}
}
fn responses(status: Option<&Status>, r#type: Option<TokenStream>) -> TokenStream {
if let Some(status) = status {
let code = status.code.as_str();
let content = r#type.map(|r#type| {
// TODO: Attempt to infer content type from schema?
quote! {
.content(
"application/json",
::utoipa::openapi::content::ContentBuilder::new()
.schema(Some(<#r#type as ::utoipa::PartialSchema>::schema()))
.build()
)
}
});
quote! {
::std::collections::BTreeMap::from_iter([
(
#code.to_owned(),
::utoipa::openapi::RefOr::T(
::utoipa::openapi::response::ResponseBuilder::new()
#content
.build()
),
),
])
}
} else {
quote!(compile_error!("missing `#[http(status = ..)]` attribute"))
}
}